package com.njcn.gather.steady.datavie.component;

import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendIndicatorDefinitionBO;
import com.njcn.gather.steady.datavie.pojo.bo.SteadyTrendSeriesFieldBO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 稳态趋势指标目录。
 */
@Component
public class SteadyTrendIndicatorCatalog {

    private static final List<String> FULL_STATS = Collections.unmodifiableList(Arrays.asList("AVG", "MAX", "MIN", "CP95"));
    private static final List<String> AVG_ONLY = Collections.unmodifiableList(Collections.singletonList("AVG"));
    private static final List<String> ABC_PHASES = Collections.unmodifiableList(Arrays.asList("A", "B", "C"));
    private static final List<String> T_PHASE = Collections.unmodifiableList(Collections.singletonList("T"));

    private final List<SteadyTrendIndicatorDefinitionBO> indicators;
    private final Map<String, SteadyTrendIndicatorDefinitionBO> indicatorMap;

    public SteadyTrendIndicatorCatalog() {
        List<SteadyTrendIndicatorDefinitionBO> result = new ArrayList<SteadyTrendIndicatorDefinitionBO>();
        result.add(indicator("V_RMS", "相电压有效值", "VOLTAGE", "电压趋势", "data_v", ABC_PHASES,
                fields(field("rms", "相电压有效值")), FULL_STATS, "V"));
        result.add(indicator("V_LINE_RMS", "线电压有效值", "VOLTAGE", "电压趋势", "data_v", ABC_PHASES,
                fields(field("rms_lvr", "线电压有效值")),
                FULL_STATS, "V"));
        result.add(indicator("FREQ", "频率", "FREQUENCY", "频率趋势", "data_v", T_PHASE,
                fields(field("freq", "频率")), FULL_STATS, "Hz"));
        result.add(indicator("V_THD", "电压总谐波畸变率", "HARMONIC", "谐波趋势", "data_v", ABC_PHASES,
                fields(field("v_thd", "电压总谐波畸变率")), FULL_STATS, "%"));
        result.add(indicator("I_RMS", "电流有效值", "CURRENT", "电流趋势", "data_i", ABC_PHASES,
                fields(field("rms", "电流有效值")), FULL_STATS, "A"));
        result.add(indicator("I_THD", "电流总谐波畸变率", "HARMONIC", "谐波趋势", "data_i", ABC_PHASES,
                fields(field("i_thd", "电流总谐波畸变率")), FULL_STATS, "%"));
        result.add(harmonic("V_HARMONIC", "电压谐波幅值", "data_harmphasic_v", "v", "V"));
        result.add(harmonic("I_HARMONIC", "电流谐波幅值", "data_harmphasic_i", "i", "A"));
        result.add(harmonic("V_HARMONIC_RATE", "电压谐波含有率", "data_harmrate_v", "v", "%"));
        result.add(harmonic("I_INTER_HARMONIC", "间谐波电流", "data_inharm_i", "i", "A"));
        result.add(harmonicPower("P_HARMONIC_POWER", "有功谐波功率", "data_harmpower_p", "p", "kW"));
        result.add(harmonicPower("Q_HARMONIC_POWER", "无功谐波功率", "data_harmpower_q", "q", "kvar"));
        result.add(harmonicPower("S_HARMONIC_POWER", "视在谐波功率", "data_harmpower_s", "s", "kVA"));
        result.add(indicator("FLUC", "电压波动", "FLICKER", "闪变趋势", "data_fluc", T_PHASE,
                fields(field("fluc", "电压波动")), AVG_ONLY, "%"));
        result.add(indicator("PST", "短时闪变", "FLICKER", "闪变趋势", "data_flicker", T_PHASE,
                fields(field("pst", "短时闪变")), AVG_ONLY, ""));
        result.add(indicator("PLT", "长时闪变", "FLICKER", "闪变趋势", "data_plt", T_PHASE,
                fields(field("plt", "长时闪变")), AVG_ONLY, ""));
        indicators = Collections.unmodifiableList(result);

        Map<String, SteadyTrendIndicatorDefinitionBO> map = new LinkedHashMap<String, SteadyTrendIndicatorDefinitionBO>();
        for (SteadyTrendIndicatorDefinitionBO indicator : indicators) {
            map.put(indicator.getIndicatorCode(), indicator);
        }
        indicatorMap = Collections.unmodifiableMap(map);
    }

    public List<SteadyTrendIndicatorDefinitionBO> listIndicators() {
        return indicators;
    }

    public SteadyTrendIndicatorDefinitionBO getIndicator(String indicatorCode) {
        return indicatorMap.get(indicatorCode);
    }

    private SteadyTrendIndicatorDefinitionBO harmonic(String code, String name, String tableName, String prefix, String unit) {
        SteadyTrendIndicatorDefinitionBO indicator = indicator(code, name, "HARMONIC", "谐波趋势", tableName, ABC_PHASES,
                new ArrayList<SteadyTrendSeriesFieldBO>(), FULL_STATS, unit);
        indicator.setHarmonic(true);
        indicator.setHarmonicFieldPrefix(prefix);
        indicator.setHarmonicOrderStart(2);
        indicator.setHarmonicOrderEnd(50);
        return indicator;
    }

    private SteadyTrendIndicatorDefinitionBO harmonicPower(String code, String name, String tableName, String prefix, String unit) {
        return harmonic(code, name, tableName, prefix, unit);
    }

    private SteadyTrendIndicatorDefinitionBO indicator(String code, String name, String groupCode, String groupName,
                                                       String tableName, List<String> phaseCodes,
                                                       List<SteadyTrendSeriesFieldBO> seriesFields,
                                                       List<String> supportStats, String unit) {
        SteadyTrendIndicatorDefinitionBO indicator = new SteadyTrendIndicatorDefinitionBO();
        indicator.setIndicatorCode(code);
        indicator.setName(name);
        indicator.setGroupCode(groupCode);
        indicator.setGroupName(groupName);
        indicator.setTableName(tableName);
        indicator.setPhaseCodes(new ArrayList<String>(phaseCodes));
        indicator.setSeriesFields(new ArrayList<SteadyTrendSeriesFieldBO>(seriesFields));
        indicator.setSupportStats(new ArrayList<String>(supportStats));
        indicator.setUnit(unit);
        return indicator;
    }

    private List<SteadyTrendSeriesFieldBO> fields(SteadyTrendSeriesFieldBO... fields) {
        return new ArrayList<SteadyTrendSeriesFieldBO>(Arrays.asList(fields));
    }

    private SteadyTrendSeriesFieldBO field(String field, String name) {
        return new SteadyTrendSeriesFieldBO(field, name);
    }
}
