package com.njcn.gather.script.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.njcn.gather.script.pojo.enums.ResultUnitEnum;
import com.njcn.gather.script.pojo.po.PqScriptDtls;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wr
 * @description
 * @date 2025/1/13 22:05
 */
public class ScriptDtlsDesc {
    /**
     * @Description:
     * @param scriptDtlIndex 检测脚本集合
     * @param isScript 是否为下源检测脚本（true 则为下源检测脚本里面所有单位都是值类型）
     * @param isValueType 用来检测脚本是相对类型，还是绝对类型
     * @return: java.lang.StringBuffer
     * @Author: wr
     * @Date: 2025/3/27 15:20
     */
    public static StringBuffer getStringBuffer(List<PqScriptDtls> scriptDtlIndex,Boolean isScript, Boolean isValueType) {
        if (ObjectUtil.isNull(isValueType)) {
            isValueType = true;
        }
        if (isScript) {
            isValueType = false;
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append("输入：");
        appendValue(scriptDtlIndex, buffer, "Freq", "频率", ResultUnitEnum.FREQ_UNIT.getUnit(), null);
        appendValue(scriptDtlIndex, buffer, "VOL", "U", isValueType ? ResultUnitEnum.V_RELATIVE_UNIT.getUnit() : ResultUnitEnum.V_ABSOLUTELY_UNIT.getUnit(), "相角=");
        appendValue(scriptDtlIndex, buffer, "CUR", "I", isValueType ? ResultUnitEnum.I_RELATIVE_UNIT.getUnit() : ResultUnitEnum.I_ABSOLUTELY_UNIT.getUnit(), "相角=");
        dtlsSetBuffer(scriptDtlIndex, buffer, "Harm_V", "h", ResultUnitEnum.HV_UNIT.getUnit(), true);
        dtlsSetBuffer(scriptDtlIndex, buffer, "Harm_I", "h", ResultUnitEnum.HI_UNIT.getUnit(), true);
        dtlsSetBuffer(scriptDtlIndex, buffer, "InHarm_V", "i", ResultUnitEnum.HSV_UNIT.getUnit(), false);
        dtlsSetBuffer(scriptDtlIndex, buffer, "InHarm_I", "i", ResultUnitEnum.HSI_UNIT.getUnit(), false);
        dtlsSetBufferDip(scriptDtlIndex, buffer, "Dip", "暂态", ResultUnitEnum.HSI_UNIT.getUnit());
        dtlsSetBufferFlicker(scriptDtlIndex, buffer, "Flicker", "闪变");
        return buffer;
    }

    public static void appendValue(List<PqScriptDtls> scriptDtlIndex, StringBuffer buffer, String valueType, String prefix, String unit, String suffix) {
        List<PqScriptDtls> values = scriptDtlIndex.stream()
                .filter(x -> valueType.equals(x.getValueType()))
                .sorted(Comparator.comparing(PqScriptDtls::getPhase))
                .collect(Collectors.toList());
        if (CollUtil.isNotEmpty(values)) {
            for (PqScriptDtls dtls : values) {
                if ("Freq".equals(valueType)) {
                    buffer.append(prefix + "=" + dtls.getValue() + unit);
                } else {
                    buffer.append(prefix + dtls.getPhase().toLowerCase() + "=" + dtls.getValue() + unit);
                }
                if (suffix != null) {
                    buffer.append("," + suffix + dtls.getAngle() + "° ");
                } else {
                    buffer.append(" ");
                }
            }
        }
    }


    public static void dtlsSetBuffer(List<PqScriptDtls> scriptDtlIndex, StringBuffer buffer, String valueType, String name, String unit, Boolean fly) {
        LinkedHashMap<Double, List<PqScriptDtls>> harmNumMap = scriptDtlIndex.stream()
                .filter(x -> valueType.equals(x.getValueType()))
                .sorted(Comparator.comparing(PqScriptDtls::getHarmNum))
                .collect(Collectors.groupingBy(PqScriptDtls::getHarmNum, LinkedHashMap::new, Collectors.toList()));
        harmNumMap.forEach((harmNum, value) -> {
            if (fly) {
                buffer.append(name + String.format("%.0f", harmNum) + "(");
            } else {
                buffer.append(name + harmNum + "(");
            }
            appendValues(buffer, value, unit);
        });
    }

    private static void dtlsSetBufferDip(List<PqScriptDtls> scriptDtlIndex, StringBuffer buffer, String valueType, String name, String unit) {
        List<PqScriptDtls> list = scriptDtlIndex.stream().filter(x -> valueType.equals(x.getValueType()))
                .sorted(Comparator.comparing(PqScriptDtls::getPhase))
                .collect(Collectors.toList());
        if (CollUtil.isNotEmpty(list)) {
            buffer.append(name + " ");
            for (PqScriptDtls dtls : list) {
                buffer.append(dtls.getPhase() + "相(暂态深度=" + dtls.getTransValue() + ResultUnitEnum.VOLTAGE_DUR_UNIT.getUnit()
                        + ",暂态持续时间=" + (ObjectUtil.isNull(dtls.getRetainTime()) ? "/" : dtls.getRetainTime().toString()) + ResultUnitEnum.VOLTAGE_MAG_UNIT.getUnit() + ") ");
            }
        }
    }

    private static void dtlsSetBufferFlicker(List<PqScriptDtls> scriptDtlIndex, StringBuffer buffer, String valueType, String name) {
        List<PqScriptDtls> list = scriptDtlIndex.stream().filter(x -> valueType.equals(x.getValueType()))
                .sorted(Comparator.comparing(PqScriptDtls::getPhase))
                .collect(Collectors.toList());
        if (CollUtil.isNotEmpty(list)) {
            buffer.append(name + " ");
            for (PqScriptDtls dtls : list) {
                buffer.append(dtls.getPhase() + "相(变动频度=" + dtls.getChagFre()
                        + "次/min,变动量=" + dtls.getChagValue() + ResultUnitEnum.VOLTAGE_DUR_UNIT.getUnit() + ") ");
            }
        }
    }

    private static void appendValues(StringBuffer buffer, List<PqScriptDtls> values, String unit) {
        if (CollUtil.isNotEmpty(values)) {
            for (PqScriptDtls dtls : values) {
                buffer.append(dtls.getPhase() + "相=" + dtls.getValue() + unit);
            }
            buffer.append(") ");
        }
    }
}
