package com.tsi.monitoring.service;

import com.tsi.monitoring.mapper.utis.UtisMapper;
import com.tsi.monitoring.repository.GlobalStatusInfoRepository;
import com.tsi.monitoring.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
public class UtisService {
    private final UtisMapper utisMapper;
    private final GlobalStatusInfoRepository repository;

    public void setProcessStatusValue() {
        List<ProcessStatusVo> processLinkInfo = this.utisMapper.getProcessLinkInfo();
        if (processLinkInfo != null && processLinkInfo.size() > 0) {
            List<SendRcvHistVo> policeOccurHistory = this.utisMapper.getProcessLinkedCountHistory();
            Map<String, List<SendRcvHistVo>> map = new HashMap<>();
            for (SendRcvHistVo vo : policeOccurHistory) {
                map.computeIfAbsent(vo.getProcessId(), k -> new ArrayList<>());
                map.get(vo.getProcessId()).add(vo);
            }

            for (ProcessStatusVo vo : processLinkInfo) {
                if (map.get(vo.getProcessId()) != null) {
                    vo.setLinkHistory(map.get(vo.getProcessId()));
                }
            }
        }
        this.repository.setProcessStatusValue(processLinkInfo);
    }

    public List<ProcessStatusVo> getProcessStatusValue() {
        return this.repository.getProcessStatusValue();
    }

    public List<ProcessStatusVo> getRefreshStatusValue() {
        this.setProcessStatusValue();
        return this.getProcessStatusValue();
    }

    public AllStatisticsVo getAllStatistics() {
        AllStatisticsVo vo = new AllStatisticsVo();
        vo.setRcvM(this.utisMapper.getRcvPoliceMoctTrafficHistory()); //국토부 민간 교통정보 수집
//        vo.setSndT(this.utisMapper.getSndPoliceTrafficHistory()); //국토부 교통 제공
//        vo.setRcvT(this.utisMapper.getRcvPoliceTrafficHistory()); //국토부 교통 수집
//        vo.setRcvI(this.utisMapper.getRcvIncidentHistory()); //도로 공사 돌발정보
//        vo.setSndM(this.utisMapper.getSndMoctTrafficHistory()); //민간 정보 제공
//        vo.setRcvN(this.utisMapper.getRcvNtsTrafficHistory()); //NTS 정보 수집
        vo.setRcvPi(this.utisMapper.getRcvPoliceIncidentHistory());//경찰 돌발 수집
        return vo;
    }

    public List<RcvIncdMoctVo> getPeriodRcvIncdHistory(Map<String, Object> paramMap) {
        return this.utisMapper.getPeriodRecIncdHistory(paramMap);
    }

    public List<SendRcvHistVo> getPeriodRcvNtsStatistics(Map<String, Object> paramMap) {
        return this.utisMapper.getPeriodRcvNtsStatistics(paramMap);
    }

    public List<SndMoctVo> getPeriodSndMoctStatistics(Map<String, Object> paramMap) {
        return this.utisMapper.getPeriodSndMoctStatistics(paramMap);
    }

    public List<SndMolitTraffVo> getPeriodSndMolitTraffStatistics(Map<String, Object> paramMap) {
        String type = (String)paramMap.get("type");
        String end = "'";
        if (type.equals("hh")) {
            end = " HH24'";
        }

        paramMap.put("format", "'YYYY-MM-DD" + end);
        return this.utisMapper.getPeriodSndMolitTraffStatistics(paramMap);
    }

    public List<SndMolitTraffVo> getPeriodRcvMolitTraffStatistics(Map<String, Object> paramMap) {

        String type = (String)paramMap.get("type");
        if (type.equals("hist")) {
            return this.utisMapper.getPeriodRcvMolitTraffHist(paramMap);
        }

        String end = "'";
        if (type.equals("hh")) {
            end = " HH24'";
        }

        paramMap.put("format", "'YYYY-MM-DD" + end);
        return this.utisMapper.getPeriodRcvMolitTraffStatistics(paramMap);
    }

    public List<SndMolitTraffVo> getPeriodRcvMoctTraffStatistics(Map<String, Object> paramMap) {
        String type = (String)paramMap.get("type");
        if (type.equals("hist")) {
            return this.utisMapper.getPeriodRcvMoctTraffHist(paramMap);
        }

        String end = "'";
        if (type.equals("hh")) {
            end = " HH24'";
        }
        else if (type.equals("mm")) {
            end = " HH24:MI:SS'";
        }

        paramMap.put("format", "'YYYY-MM-DD" + end);
        return this.utisMapper.getPeriodRcvMoctTraffStatistics(paramMap);
    }

    public void setAllProcessStatusValue() {
        List<ProcessStatusVo> processLinkInfo = this.utisMapper.getAllProcessLinkInfo();
        this.repository.setAllProcessStatusValue(processLinkInfo);
    }

    public List<ProcessStatusVo> getAllProcessStatusValue() {
        return this.repository.getAllProcessStatusValue();
    }

    public List<TrafficWeightConfVo> getTrafficWeightInfo() {
        return this.utisMapper.getTrafficWeightInfos();
    }

    @Transactional(transactionManager = "utisTransactionManager", rollbackFor = Exception.class)
    public ResultVo updateTrafficWeightInfo(TrafficWeightVo param) throws RuntimeException {
        ResultVo vo = new ResultVo();
        int affectedRow = 0;
        int affectedRow3 = 0;
        if (param.getTrafficWeightConfVo() != null) {
            affectedRow = this.utisMapper.updateTrafficWeightInfo(param.getTrafficWeightConfVo());
        }
        if (param.getCodeValue() != null) {
            affectedRow3 = this.utisMapper.setDwAnalysisType(param.getCodeValue());
        }

        if (affectedRow > 0 || affectedRow3 > 0) {
            vo.setMsg("요청하신 수집원별 가중치 정보가 설정 되었습니다.");
            vo.setSuccess("S");
            return vo;
        }
        else {
            throw new RuntimeException("요청하신 수집원별 가중치 정보 설정 중 오류가 발생하였습니다.");
        }
    }

    public List<SigCommStatisticsVo> getPeriodSigCommStatistics(Map<String, Object> paramMap) {
        return this.utisMapper.getPeriodSigCommStatistics(paramMap);
    }
    public Integer getAnalysisType() {
        return this.utisMapper.getAnalysisType();
    }


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

    public  List<CenterSendRecCountVo> getCenterSendRecCount(Map<String, Object> paramMap) {
        return this.utisMapper.getCenterSendRecCount(paramMap);
    }
}
