package com.huatek.busi.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huatek.busi.vo.BusiParamTransformerVo;
import com.huatek.busi.vo.EvaluationNameVo;
import com.huatek.torch.frame.exception.OwlException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import javax.json.JsonObject;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @author plus_wang
 */
@Slf4j
public class EvaluationNameUtil {
    private static List<EvaluationNameVo> evaluationNameVoList = new ArrayList<>();

    /**
     * 计算步骤
     *
     * @param stepId          步骤编号->第几步
     * @param tmp             每个步骤名称
     * @param tmp             入参
     * @param calculateResult 计算结果
     */
    public static void setStep(int stepId, BusiParamTransformerVo tmp, String calculateResult, String testValue) {
        EvaluationNameVo vo = new EvaluationNameVo();
        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        if (testValue != null) {
            if (StringUtils.isNotBlank(testValue)) {
                log.info("-- >> 变压器-- {} 输入值: [{}]", tmp.getDescription(), tmp.getTestValue());
                log.info("-- >> 变压器-- {} 权重: [{}]", tmp.getDescription(), tmp.getValue());
                jsonObject.put("输入值", new BigDecimal(testValue).setScale(4, RoundingMode.HALF_UP).toString());
            }
        }
        if ("1".equals(tmp.getAbValueFlag())) {
            if (tmp.getAValue() != null) {
                if (StringUtils.isNotBlank(tmp.getAValue().toString())) {
                    log.info("-- >> 变压器-- {} A值: [{}]", tmp.getDescription(), tmp.getAValue());
                    jsonObject.put("a值", tmp.getAValue().setScale(4, RoundingMode.HALF_UP).toString());
                }
            }
            if (tmp.getBValue() != null) {
                if (StringUtils.isNotBlank(tmp.getBValue().toString())) {
                    log.info("-- >> 变压器-- {} B值: [{}]", tmp.getDescription(), tmp.getBValue());
                    jsonObject.put("b值", tmp.getBValue().setScale(4, RoundingMode.HALF_UP).toString());
                }
            }
        }
        if (tmp.getValue() != null) {
            if (StringUtils.isNotBlank(tmp.getValue().toString())) {
                log.info("-- >> 变压器--- {} 权重: [{}]", tmp.getDescription(), tmp.getValue());
                jsonObject.put("权重", tmp.getValue().setScale(4, RoundingMode.HALF_UP).toString());
            }
        }
        vo.setWeightLevel(tmp.getWeightLevel());
        vo.setInputParam(jsonObject);
        vo.setStepId(stepId);
        vo.setPname(tmp.getPname());
        vo.setName(tmp.getName());
        vo.setStepName(tmp.getDescription());
        vo.setCalculateResult(calculateResult);
        evaluationNameVoList.add(vo);
    }

    public static void setSpecialStep(int stepId, BusiParamTransformerVo tmp, String calculateResult, JSONObject jsonObject, String stepName) {
        EvaluationNameVo vo = new EvaluationNameVo();
        vo.setInputParam(jsonObject);
        vo.setStepId(stepId);
        if (StringUtils.isEmpty(stepName)) {
            vo.setPname(tmp.getPname());
            vo.setName(tmp.getName());
            vo.setStepName(tmp.getDescription());
            vo.setWeightLevel(tmp.getWeightLevel());
        } else {
            vo.setStepName(stepName);
            vo.setName("final");
            vo.setWeightLevel("1");
        }
        vo.setCalculateResult(calculateResult);
        evaluationNameVoList.add(vo);
    }

    public static void cleanList() {
        evaluationNameVoList.clear();
    }

    public static String listToJson() {
        return JSONArray.toJSONString(evaluationNameVoList);
    }

    public static void throwErrorMsg(BusiParamTransformerVo parentVo, BusiParamTransformerVo paramTransformerVo) {
        throw new OwlException("缺少GIS-" + parentVo.getDescription() + "-" + paramTransformerVo.getDescription() + "-相关参数");
    }
}
