package com.lideeyunji.core.framework.config.aspect.enhance.strategy.java;

import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceRespModel;
import com.lideeyunji.core.framework.config.aspect.enhance.model.EnhanceResult;
import com.lideeyunji.core.framework.config.aspect.enhance.model.JavaEnhanceEventState;
import com.lideeyunji.core.framework.entity.EnhanceJavaEntity;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.exception.lideeYunJiException;
import com.lideeyunji.tool.framework.yunji.model.ExecuteEnhanceModel;
import com.lideeyunji.tool.framework.yunji.model.ResultDataModel;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.HashMap;
import java.util.Map;

/**
 * HTTP方式增强
 */
@Slf4j
public class HttpEnhanceJavaStrategy extends BaseEnhanceJavaStrategy implements EnhanceJavaStrategy {

    //执行
    @Override
    public void execute(EnhanceJavaEntity enhanceJavaEntity, EnhanceContext context, String type, JoinPoint joinPoint) throws Throwable {
        //必须是环绕增强
        if (!FuncBase.equals(type, JavaEnhanceEventState.AROUND.getName())) {
            return;
        }
        this.executeHttpEnhance(enhanceJavaEntity, context, joinPoint);
    }
    private void executeHttpEnhance(EnhanceJavaEntity enhanceJavaEntity,  EnhanceContext context, JoinPoint joinPoint) {
        context.setBefore(true);
        //前置置增强发送
        this.sendHttp(enhanceJavaEntity.getJavaClassUrl(), context);
        if (context.getOnlyBefore()) {
            return;
        }
        try {
            ProceedingJoinPoint proceedingJointpoint = (ProceedingJoinPoint) joinPoint;
            Object proceed = proceedingJointpoint.proceed();
            if (proceed instanceof ResultDataModel) {
                EnhanceResult enhanceResult = context.getResult();
                enhanceResult.setRecords(((ResultDataModel) proceed).getRecords());
                enhanceResult.setTotal(((ResultDataModel) proceed).getTotal());
            } else if (proceed instanceof ExecuteEnhanceModel) {
                EnhanceResult enhanceResult = context.getResult();
                enhanceResult.setId(((ExecuteEnhanceModel) proceed).getId());
            }
        } catch (Throwable e) {
            throw new lideeYunJiException("发送HTTP增强失败：" + e.getMessage());
        }

        //后置增强发送
        this.sendHttp(enhanceJavaEntity.getJavaClassUrl(), context);
    }

    //发送请求
    private void sendHttp(String httpUrl,EnhanceContext context){
        for (int i = 0; i < 3; i++) {
            try {
                String accept = Func.sendPost(httpUrl, context);

                EnhanceRespModel respBody = Func.json2Bean(accept, EnhanceRespModel.class);
                if (respBody.checkStatus()) {
                    EnhanceContext bodyData = respBody.getData();
                    context.setParam(bodyData.getParam());
                    context.setResult(bodyData.getResult());
                    break;
                }
                Map<String, Object> errorMap = new HashMap<>();
                errorMap.put("body", Func.json2Str(context));
                errorMap.put("url", httpUrl);
                errorMap.put("respBody", Func.json2Str(respBody));

                log.error("发送HTTP增强失败：" + Func.json2Str(errorMap));
                Thread.sleep(2000);//休息2秒
            } catch (Exception e) {
                log.error("发送HTTP增强失败-e：" + e.getMessage());
            }
        }
    }
}
