package com.example.fuzzControll.aop;

import com.example.fuzzControll.annotion.NeedCutBefore;
import com.example.fuzzControll.exception.mysqlException.MysqlException;
import com.example.fuzzControll.exception.testException.AflnetException;
import com.example.fuzzControll.tools.system.GlobalClass;
import com.example.fuzzControll.tools.system.SystemRunningParams;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Slf4j
public class afterAop implements Ordered {
    @Pointcut(value = "@annotation(com.example.fuzzControll.annotion.NeedCutAfter)")
    private void afterCut() {
    }
    @Around("afterCut()")
    public void after(JoinPoint point) throws Throwable {

        Signature signature = point.getSignature();
        MethodSignature methodSignature = null;
        if (!(signature instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        methodSignature = (MethodSignature) signature;
        Object target = point.getTarget();
        Method currentMethod = null;
        try {
            currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
        } catch (NoSuchMethodException e) {
            log.error("NoSuchMethod!");
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        NeedCutBefore logotype = currentMethod.getAnnotation(NeedCutBefore.class);
        /*相关处理逻辑,负责数据存入*/
        switch (logotype.name()) {
            case "aflnet":
//                aflnet(logotype.function());
                break;
            case "kitty":
//                kittyAfter(logotype.function());
                break;
            default:
                throw new AflnetException("Cut error: There is no name of anotation!");
        }
    }

    @Override
    public int getOrder() {
        return 3;
    }
}