package com.tsi.monitoring.service;

import com.tsi.monitoring.mapper.section.SectionMapper;
import com.tsi.monitoring.repository.GlobalStatusInfoRepository;
import com.tsi.monitoring.vo.*;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
@Slf4j
public class SectionService {
    private final SectionMapper sectionMapper;
    private final GlobalStatusInfoRepository repository;

    public List<LocalCenterLinkVo> getCenterInfo() {
       return this.repository.getLocalCenterValue();
    }

    public List<CenterVo> getCenterLabelInfo() {
        return this.sectionMapper.getCenterInfo();
    }

    public List<CenterVo> getCenterList() {
        List<CenterVo> vo = this.sectionMapper.getCenterInfo();
        List<String> priority = Arrays.asList(
                "서울교통정보센터", "인천교통정보센터", "대전교통정보센터", "대구교통정보센터",
                "부산시교통정보센터", "광주교통정보센터", "울산교통정보센터"
        );
        Comparator<CenterVo> customComparator = (a, b) -> {
            int indexA = priority.indexOf(a.getCenterInfo());
            int indexB = priority.indexOf(b.getCenterInfo());

            if (indexA != -1 && indexB != -1) {
                // 둘 다 우선순위 리스트에 있으면 리스트 순서대로
                return Integer.compare(indexA, indexB);
            } else if (indexA != -1) {
                // a만 우선순위 리스트에 있으면 a가 앞
                return -1;
            } else if (indexB != -1) {
                // b만 우선순위 리스트에 있으면 b가 앞
                return 1;
            } else {
                // 둘 다 리스트에 없으면 이름순 정렬
                return a.getCenterInfo().compareTo(b.getCenterInfo());
            }
        };

        // 정렬 적용
        vo.sort(customComparator);
        return vo;
    }
    public List<StatusVo> getSigStatusInfo() { return sectionMapper.getSigStatusInfo(); }

    public LogHistoryVo getLogHistory(Map<String, Object> paramMap) {
        LogHistoryVo vo = new LogHistoryVo();
        vo.setIntStat(this.sectionMapper.getIntStatusHistory(paramMap));
        vo.setMoct(this.sectionMapper.getMoctHistory(paramMap));
        vo.setRcv(this.sectionMapper.getRcvHistory(paramMap));
        vo.setSend(this.sectionMapper.getSendHistory(paramMap));
        return vo;
    }

    public void setLocalCenterLinkInfo() {
        List<LocalCenterLinkVo> list = this.sectionMapper.getLocalCenterLinkInfo();
        for (LocalCenterLinkVo vo : list) {
            String commState = "2";
            String rComm = vo.getRCommState();
            String sComm = vo.getSCommState();
            String mComm = vo.getMCommState();
            if (rComm == null && sComm == null && mComm == null) {
                commState = "-";
            }

            if (vo.getRYn() != null && "N".equals(vo.getRYn())) {
                vo.setRCommState("0");
                rComm = "0";
            }

            if (vo.getMYn() != null && "N".equals(vo.getMYn())) {
                vo.setMCommState("0");
                mComm = "0";
            }

            if (vo.getSYn() != null && "N".equals(vo.getSYn())) {
                vo.setSCommState("0");
                sComm = "0";
            }

            String commTot = rComm + sComm + mComm;
            if (commTot.equals("111")) {
                commState = "1";
            }
            else if (commTot.equals("000")) {
                commState = "0";
            }
            vo.setCommState(commState);
        }
        this.repository.setLocalCenterValue(list);
    }

    public List<LocalCenterLinkVo> refreshCenterInfos() {
        this.setLocalCenterLinkInfo();
        return this.getCenterInfo();
    }

    public LocalCenterLinkVo getCenterInfoWithId(String centerId) {
        List<LocalCenterLinkVo> list = this.getCenterInfo();
        LocalCenterLinkVo centerInfo = new LocalCenterLinkVo();
        if (list != null && list.size() > 0) {
            for (LocalCenterLinkVo vo : list) {
                if (vo.getCenterId().equals(centerId)) {
                    centerInfo = vo;
                }
            }
        }
        return centerInfo;
    }

    public List<StatisticsVo> getTrafficSndStatistics(String centerId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("centerId", centerId);
        paramMap.put("infoKind", "T");
        return this.sectionMapper.getSndStatistics(paramMap);
    }

    public List<StatisticsVo> getIncidentSndStatistics(String centerId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("centerId", centerId);
        paramMap.put("infoKind", "I");
        return this.sectionMapper.getSndStatistics(paramMap);
    }

    public List<StatisticsVo> getControlSndStatistics(String centerId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("centerId", centerId);
        paramMap.put("infoKind", "C");
        return this.sectionMapper.getSndStatistics(paramMap);
    }

    public List<StatisticsVo> getTrafficRcvStatistics(String centerId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("centerId", centerId);
        paramMap.put("infoKind", "T");
        return this.sectionMapper.getRcvStatistics(paramMap);
    }

    public List<StatisticsVo> getIncidentRcvStatistics(String centerId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("centerId", centerId);
        paramMap.put("infoKind", "I");
        return this.sectionMapper.getRcvStatistics(paramMap);
    }

    public List<StatisticsVo> getControlRcvStatistics(String centerId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("centerId", centerId);
        paramMap.put("infoKind", "C");
        return this.sectionMapper.getRcvStatistics(paramMap);
    }

    public List<StatisticsVo> getSendMoctStatistics(String centerId) {
        return this.sectionMapper.getSndMoctStatistics(centerId);
    }


    public AllStatisticsVo getAllStatistics(String centerId) {
        AllStatisticsVo vo = new AllStatisticsVo();
        vo.setRcvC(this.getControlRcvStatistics(centerId));
        vo.setRcvI(this.getIncidentRcvStatistics(centerId));
        vo.setRcvT(this.getTrafficRcvStatistics(centerId));
        vo.setSndC(this.getControlSndStatistics(centerId));
        vo.setSndI(this.getIncidentSndStatistics(centerId));
        vo.setSndT(this.getTrafficSndStatistics(centerId));
        vo.setSndM(this.getSendMoctStatistics(centerId));
        return vo;
    }

    public List<PeriodCenterRcvSendStatisticsVo> getPeriodCenterRcvSendStatistics(Map<String, Object> paramMap) {
        return this.sectionMapper.getPeriodCenterRcvSendStatistics(paramMap);
    }

    public List<CenterVo> getSigRegionInfo() {
        return this.sectionMapper.getSigRegionInfo();
    }

}
