package com.huatek.busi.task.transformer.utils.accessories;

import com.google.common.collect.Maps;
import com.huatek.busi.task.transformer.utils.CommonUtils;
import com.huatek.busi.util.EvaluateConstant;
import com.huatek.busi.vo.BusiParamTransformerVo;
import com.huatek.busi.vo.TransformerEvaluateDataVo;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;

import java.util.Map;

/**
 * @ClassName: TapChanger
 * @Description: 变压器附件-分接开关.
 * @Author: Arno
 * @Date:  2023/10/10 15:10
 * @version V1.0
 */
@Slf4j
public class TapChanger {

    public static String DATA_ERROR_MSG = "-->变压器附件-分接开关-{}未设置评估值";
    public static String PARAMS_ERROR_MSG = "-->>变压器附件-分接开关-{}未设置评估参数";
    public static String RESULT_MSG = "-->>变压器附件-分接开关-{}计算结果: [{}]";


    public static Map<String, Object> calcTapChangerWeight(TransformerEvaluateDataVo transformerEvaluateDataVo, Map<String, BusiParamTransformerVo> secondLevelMap,
                                                                Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step) {
        BusiParamTransformerVo secondVo = CommonUtils.getEvaluateObject(EvaluateConstant.BYQ_FJKG, secondLevelMap);
        if (null != secondVo) {
            if (CommonUtils.checkAttend(secondVo)) {
                Map<String, Object> threeResultMap = calcThree(transformerEvaluateDataVo, threeLevelMap, jsonArray, step);
                return CommonUtils.calcSecond(secondVo, threeResultMap, jsonArray, "变压器附件-分接开关权重计算");
            }
        }
        log.error("-->变压器附件-分接开关未设置评估参数");
        return null;
    }


    public static Map<String, Object> calcThree(TransformerEvaluateDataVo vo, Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step) {
        //油击穿电压变化率（Kv，%）
        String oilPuncture = vo.getOilPuncture();
        //传动机构有无问题(Y/N)
        String transmissionGear = vo.getTransmissionGear();

        Double calcWeight = calc(oilPuncture, EvaluateConstant.BYQ_FJKG_YJCDYBHL, "油击穿电压变化率（Kv，%）", threeLevelMap, jsonArray, step, "变压器附件-分接开关-油击穿电压变化率（Kv，%）权量计算结果");
        Double calcWeight2 = calc(transmissionGear, EvaluateConstant.BYQ_FJKG_CDJGYWWT, "传动机构有无问题(Y/N)权重", threeLevelMap, jsonArray, step, "变压器附件-分接开关-传动机构有无问题(Y/N)权重计算结果");

        Double total = calcWeight + calcWeight2 ;
        log.info("-->>变压器附件-分接开关-分项和计算结果: [{}]", total);
        step += 1;
        return CommonUtils.buildResult(step, total);
    }


    public static Double calc(String value, String type, String typeDesc, Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step, String stepName) {
        Double aDouble = CommonUtils.calcItemResult(value, type, typeDesc, threeLevelMap, jsonArray, step, stepName);
        log.info("-->>变压器附件-分接开关-{}计算结果{}", typeDesc, aDouble);
        return aDouble;
    }

    public static Double calc2(String value, String type, String typeDesc, Map<String, BusiParamTransformerVo> threeLevelMap, JSONArray jsonArray, int step, String stepName) {
        Double calcWeight = 0d;
        BusiParamTransformerVo tmp1 = CommonUtils.getEvaluateObject(type, threeLevelMap);
        if (CommonUtils.checkParams(tmp1)) {
            log.error(PARAMS_ERROR_MSG, typeDesc);
            return calcWeight;
        }
        if (CommonUtils.checkValue(value)) {
            log.error(DATA_ERROR_MSG, typeDesc);
            return tmp1.getValue().doubleValue();
        }
        calcWeight = CommonUtils.calcWeight(new Double(value), tmp1);
        log.info(RESULT_MSG, typeDesc, calcWeight);
        step += 1;
        CommonUtils.setStep(jsonArray, step, stepName, CommonUtils.calculateModel(value, tmp1), String.valueOf(calcWeight));
        return calcWeight;
    }
}
