package com.njcn.event.file.utils;

import com.njcn.event.file.pojo.bo.InstantData;
import com.njcn.event.file.pojo.bo.RmsData;
import com.njcn.event.file.pojo.bo.WaveDataDetail;
import com.njcn.event.file.pojo.dto.WaveDataDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2023年09月20日 16:14
 */
public class WaveUtil {

    /**
     * 筛选后台绘图所需的瞬时、RMS等一次值数据
     */
    public static List<WaveDataDetail> filterWaveData(WaveDataDTO waveDataDTO) {
        List<WaveDataDetail> waveDataDetails = new ArrayList<>();
        List<String> waveTitle = waveDataDTO.getWaveTitle();
        //通道名称，针对治理项目波形文件处理
        List<String> channelNames = waveDataDTO.getChannelNames();
        boolean openTri = waveDataDTO.getPtType() == 2;
        /************ 一个图形的相别决定了图形个数 Modify by yexb -----start ***********/
        Integer iPhase = waveDataDTO.getIPhasic();
        int picCounts = (waveTitle.size() - 1) / iPhase;
        /**处理相别的颜色**/
        List<String> colors = new ArrayList<>();
        //遍历图形个数
        for (int i = 0; i < picCounts; i++) {
            WaveDataDetail waveDataDetail = new WaveDataDetail();
            switch (iPhase) {
                case 1:
                    waveDataDetail.setA(waveTitle.get(i + 1).substring(1));
                    waveDataDetail.setB("");
                    waveDataDetail.setC("");
                    colors.add("#DAA520");
                    colors.add("#fff");
                    colors.add("#fff");
                    waveDataDetail.setChannelName(channelNames.get(i + 1));
                    break;
                case 2:
                    waveDataDetail.setA(waveTitle.get(i * 2 + 1).substring(1));
                    waveDataDetail.setB(waveTitle.get(i * 2 + 2).substring(1));
                    waveDataDetail.setC("");
                    colors.add("#DAA520");
                    colors.add("#2E8B57");
                    colors.add("#fff");
                    waveDataDetail.setChannelName(channelNames.get(i * 2 + 1));
                    break;
                case 3:
                    waveDataDetail.setA(waveTitle.get(i * 3 + 1).substring(1));
                    waveDataDetail.setB(waveTitle.get(i * 3 + 2).substring(1));
                    waveDataDetail.setC(waveTitle.get(i * 3 + 3).substring(1));
                    colors.add("#DAA520");
                    colors.add("#2E8B57");
                    colors.add("#A52a2a");
                    waveDataDetail.setChannelName(channelNames.get(i * 3 + 1));
                    break;
            }
            waveDataDetail.setColors(colors);

            float xishu = waveDataDTO.getPt().floatValue();
            if (waveTitle.get(iPhase * i + 1).substring(0, 1).equalsIgnoreCase("U")) {
                waveDataDetail.setUnit("kV");
                xishu = xishu / 1000;
            } else {
                waveDataDetail.setUnit("A");
                xishu = waveDataDTO.getCt().floatValue();
            }
            Float sfMax = 0f, sfMin = 0f, rfMax = 0f, rfMin = 0f;
            List<List<Float>> sAValue = new ArrayList<>(), sBValue = new ArrayList<>(), sCValue = new ArrayList<>(), rAValue = new ArrayList<>(), rBValue = new ArrayList<>(), rCValue = new ArrayList<>();
            List<List<Float>> sunData = waveDataDTO.getListWaveData();
            for (int j = 0; j < sunData.size(); j++) {
                float x = sunData.get(j).get(0);
                float shunFirstA = 0f, shunFirstB = 0f, shunFirstC = 0f;
                //根据相别来确认标题的名称
                for (int m = 0; m < iPhase; m++) {
                    if (waveTitle.get(iPhase * i + m + 1).substring(1).contains("A")) {
                        if (Objects.isNull(sunData.get(j).get(iPhase * i + m + 1))) {
                            sAValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(null);
                            }});
                        } else {
                            float tmpShunFirstA = sunData.get(j).get(iPhase * i + m + 1) * xishu;
                            shunFirstA = tmpShunFirstA;
                            sAValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(tmpShunFirstA);
                            }});
                        }
                    }
                    if (waveTitle.get(iPhase * i + m + 1).substring(1).contains("B")) {
                        if (Objects.isNull(sunData.get(j).get(iPhase * i + m + 1))) {
                            sBValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(null);
                            }});
                        } else {
                            float tmpShunFirstB = sunData.get(j).get(iPhase * i + m + 1) * xishu;
                            shunFirstB = tmpShunFirstB;
                            sBValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(tmpShunFirstB);
                            }});
                        }
                    }
                    if (waveTitle.get(iPhase * i + m + 1).substring(1).contains("C")) {
                        if (Objects.isNull(sunData.get(j).get(iPhase * i + m + 1))) {
                            sCValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(null);
                            }});
                        } else {
                            float tmpShunFirstC = sunData.get(j).get(iPhase * i + m + 1) * xishu;
                            shunFirstC = tmpShunFirstC;
                            sCValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(tmpShunFirstC);
                            }});
                        }
                    }
                }
                sfMax = getMax(sfMax, shunFirstA, shunFirstB, shunFirstC);
                if (openTri) {
                    sfMin = getMinOpen(sfMin, shunFirstA, shunFirstC);
                } else {
                    sfMin = getMin(sfMin, shunFirstA, shunFirstB, shunFirstC);
                }
            }
            InstantData instantData = new InstantData();
            instantData.setAValue(sAValue);
            instantData.setBValue(sBValue);
            instantData.setCValue(sCValue);
            instantData.setMax(sfMax);
            instantData.setMin(sfMin);
            List<List<Float>> rmsData = waveDataDTO.getListRmsData();
            for (int k = 0; k < rmsData.size(); k++) {
                float x = rmsData.get(k).get(0);
                float rmsFirstA = 0f, rmsFirstB = 0f, rmsFirstC = 0f;
                //根据相别来确认标题的名称
                for (int m = 0; m < iPhase; m++) {
                    if (waveTitle.get(iPhase * i + m + 1).substring(1).contains("A")) {
                        if (Objects.isNull(rmsData.get(k).get(iPhase * i + m + 1))) {
                            rAValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(null);
                            }});
                        } else {
                            float tmpRmsFirstA = rmsData.get(k).get(iPhase * i + m + 1) * xishu;
                            rmsFirstA = tmpRmsFirstA;
                            rAValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(tmpRmsFirstA);
                            }});
                        }
                    }
                    if (waveTitle.get(iPhase * i + m + 1).substring(1).contains("B")) {
                        if (Objects.isNull(rmsData.get(k).get(iPhase * i + m + 1))) {
                            rBValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(null);
                            }});
                        } else {
                            float tmpRmsFirstB = rmsData.get(k).get(iPhase * i + m + 1) * xishu;
                            rmsFirstB = tmpRmsFirstB;
                            rBValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(tmpRmsFirstB);
                            }});
                        }

                    }
                    if (waveTitle.get(iPhase * i + m + 1).substring(1).contains("C")) {
                        if (Objects.isNull(rmsData.get(k).get(iPhase * i + m + 1))) {
                            rCValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(null);
                            }});
                        } else {
                            float tmpRmsFirstC = rmsData.get(k).get(iPhase * i + m + 1) * xishu;
                            rmsFirstC = tmpRmsFirstC;
                            rCValue.add(new ArrayList<Float>() {{
                                add(x);
                                add(tmpRmsFirstC);
                            }});
                        }
                    }
                }
                rfMax = getMax(sfMax, rmsFirstA, rmsFirstB, rmsFirstC);
                if (openTri) {
                    rfMin = getMinOpen(sfMin, rmsFirstA, rmsFirstC);
                } else {
                    rfMin = getMin(sfMin, rmsFirstA, rmsFirstB, rmsFirstC);
                }
            }
            RmsData rmsData1 = new RmsData();
            rmsData1.setAValue(rAValue);
            rmsData1.setBValue(rBValue);
            rmsData1.setCValue(rCValue);
            rmsData1.setMax(rfMax);
            rmsData1.setMin(rfMin);

            waveDataDetail.setInstantData(instantData);
            waveDataDetail.setRmsData(rmsData1);
            waveDataDetail.setIsOpen(openTri);
            waveDataDetails.add(waveDataDetail);
        }
        /************  Modify by yexb -----end ***********/
        return waveDataDetails;
    }

    private static Float getMinOpen(Float temp, float tempA, float tempB) {
        temp = temp < tempA ? temp : tempA;
        temp = temp < tempB ? temp : tempB;
        return temp;
    }


    private static float getMin(float temp, float tempA, float tempB, float tempC) {
        temp = Math.min(temp, tempA);
        temp = Math.min(temp, tempB);
        temp = Math.min(temp, tempC);
        return temp;
    }

    private static float getMax(float temp, float tempA, float tempB, float tempC) {
        temp = Math.max(temp, tempA);
        temp = Math.max(temp, tempB);
        temp = Math.max(temp, tempC);
        return temp;
    }
}
