package com.njcn.gather.icd.mapping.component;


import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import com.njcn.gather.icd.mapping.pojo.dto.*;
import com.njcn.gather.icd.mapping.pojo.bo.*;
import com.njcn.gather.icd.mapping.pojo.param.*;
import com.njcn.gather.icd.mapping.pojo.vo.*;
import com.njcn.gather.icd.mapping.pojo.enums.GenerateStatus;
@Setter
@Getter
@Component
public class IcdToXmlMappingService {

    private IndexMappingConfig indexMapping;

    @Data
    public static class IndexMappingConfig {
        private StatIndex mmxu;
        private StatIndex mhai;
        private StatIndex interHarmonic;
        private StatIndex msqi;
        private Integer mflkShort;
        private Integer mflkLong;
        private Integer qvvr;

        @Data
        public static class StatIndex {
            private Integer average;
            private Integer maximum;
            private Integer minimum;
            private Integer percentile95;
        }
    }

    public IndexMappingConfig toIndexMapping(){
        IndexMappingConfig indexMapping = new IndexMappingConfig();
        return indexMapping;
    }

    public void setIndexMapping(IndexMappingConfig config) {
        this.indexMapping = config;
    }

    public IndexMappingConfig buildIndexMappingFromSelection(List<IndexSelectionGroupCommand> indexSelectionGroups) {
        IndexMappingConfig config = new IndexMappingConfig();
        
        if (indexSelectionGroups == null || indexSelectionGroups.isEmpty()) {
            return config;
        }

        for (IndexSelectionGroupCommand group : indexSelectionGroups) {
            String groupKey = group.getGroupKey();
            List<IndexBindingCommand> bindings = group.getBindings();
            
            if (bindings == null || bindings.isEmpty()) {
                continue;
            }

            if ("统计数据__DSSTATISTICDATA".equals(groupKey)) {
                processStatisticDataGroup(config, bindings);
            } else if ("波动闪变__DSFLICKERDATA".equals(groupKey)) {
                processFlickerDataGroup(config, bindings);
            } else if ("暂态事件__DSEVEQVVR".equals(groupKey)) {
                processQvvrGroup(config, bindings);
            }
        }

        return config;
    }

    private void processStatisticDataGroup(IndexMappingConfig config, List<IndexBindingCommand> bindings) {
        for (IndexBindingCommand binding : bindings) {
            String dataSetName = binding.getDataSetName();
            String label = binding.getLabel();
            Integer lnInst = parseLnInst(binding.getLnInst());

            if (lnInst == null) {
                continue;
            }

            if ("dsStHarm".equals(dataSetName) ) {
                if (config.getMhai() == null) {
                    config.setMhai(new IndexMappingConfig.StatIndex());
                }
                mapStatIndex(config.getMhai(), label, lnInst);
            } else if ("dsStIHarm".equals(dataSetName)) {
                if (config.getInterHarmonic() == null) {
                    config.setInterHarmonic(new IndexMappingConfig.StatIndex());
                }
                mapStatIndex(config.getInterHarmonic(), label, lnInst);
            } else if ("dsStMMXU".equals(dataSetName)) {
                if (config.getMmxu() == null) {
                    config.setMmxu(new IndexMappingConfig.StatIndex());
                }
                mapStatIndex(config.getMmxu(), label, lnInst);
            }else if ("dsStMSQI".equals(dataSetName)) {
                if (config.getMsqi() == null) {
                    config.setMsqi(new IndexMappingConfig.StatIndex());
                }
                mapStatIndex(config.getMsqi(), label, lnInst);
            } else if ("dsStatisticData".equals(dataSetName)) {
                if (label != null && label.contains("间谐波")) {
                    if (config.getInterHarmonic() == null) {
                        config.setInterHarmonic(new IndexMappingConfig.StatIndex());
                    }
                    mapStatIndex(config.getInterHarmonic(), label, lnInst);
                } else {
                    if (config.getMmxu() == null) {
                        config.setMmxu(new IndexMappingConfig.StatIndex());
                    }
                    mapStatIndex(config.getMmxu(), label, lnInst);
                    if (config.getMhai() == null) {
                        config.setMhai(new IndexMappingConfig.StatIndex());
                    }
                    mapStatIndex(config.getMhai(), label, lnInst);
                    if (config.getMsqi() == null) {
                        config.setMsqi(new IndexMappingConfig.StatIndex());
                    }
                    mapStatIndex(config.getMsqi(), label, lnInst);
                }
            }
        }
    }

    private void processFlickerDataGroup(IndexMappingConfig config, List<IndexBindingCommand> bindings) {
        for (IndexBindingCommand binding : bindings) {
            String dataSetName = binding.getDataSetName();
            String label = binding.getLabel();
            Integer lnInst = parseLnInst(binding.getLnInst());

            if (lnInst == null) {
                continue;
            }
            if ("dsPST".equals(dataSetName)) {
                if (config.getMflkShort() == null) {
                    config.setMflkShort(0);
                }
                config.setMflkShort(lnInst);
            }
            else if ("dsPLT".equals(dataSetName)) {
                if (config.getMflkLong() == null) {
                    config.setMflkLong(0);
                }
                config.setMflkLong(lnInst);
            }else {
                if (config.getMflkShort() == null) {
                    config.setMflkShort(0);
                }
                config.setMflkShort(lnInst);
                if (config.getMflkLong() == null) {
                    config.setMflkLong(0);
                }
                config.setMflkLong(lnInst);
            }
        }
    }

    private void processQvvrGroup(IndexMappingConfig config, List<IndexBindingCommand> bindings) {
        for (IndexBindingCommand binding : bindings) {
            String label = binding.getLabel();
            Integer lnInst = parseLnInst(binding.getLnInst());

            if (lnInst == null) {
                continue;
            }

            if (label != null && label.startsWith("电压变动")) {
                config.setQvvr(lnInst);
            }
        }
    }

    private void mapStatIndex(IndexMappingConfig.StatIndex statIndex, String label, Integer lnInst) {
        if (label == null) {
            return;
        }

        switch (label) {
            case "最大值":
                statIndex.setMaximum(lnInst);
                break;
            case "最小值":
                statIndex.setMinimum(lnInst);
                break;
            case "平均值":
                statIndex.setAverage(lnInst);
                break;
            case "95值":
                statIndex.setPercentile95(lnInst);
                break;
            case "间谐波最大值":
                statIndex.setMaximum(lnInst);
                break;
            case "间谐波最小值":
                statIndex.setMinimum(lnInst);
                break;
            case "间谐波平均值":
                statIndex.setAverage(lnInst);
                break;
            case "间谐波95值":
                statIndex.setPercentile95(lnInst);
                break;
            default:
                break;
        }
    }

    private Integer parseLnInst(String lnInstStr) {
        if (lnInstStr == null || lnInstStr.trim().isEmpty()) {
            return null;
        }
        try {
            return Integer.parseInt(lnInstStr.trim());
        } catch (NumberFormatException e) {
            return null;
        }
    }

}