package com.its.web.service.statistics;

import com.its.web.dto.statistics.DaeroMngmDto;
import com.its.web.dto.statistics.TrafficStatisticsCongestDto;
import com.its.web.dto.statistics.TrafficStatisticsDto;
import com.its.web.mapper.itcs.IntersectionMapper;
import com.its.web.mapper.its.statistics.StatisticsMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Service
public class StatisticsService {

    private final StatisticsMapper mapper;
    private final IntersectionMapper ixrMapper;

    public List<TrafficStatisticsDto> findStatisticsTrafficAmount(TrafficStatisticsDto.TrafficStatisticsDtoReq paramInfo, String searchType) {
        Map<String, Object> paramMap = new HashMap<>();
        String fromDt   = paramInfo.getFromDt();
        String toDt     = paramInfo.getToDt();
        String roadNmbr = paramInfo.getRoadNmbr();
        List<TrafficStatisticsDto> result = new ArrayList<>();
        if (fromDt != null && toDt != null && roadNmbr != null && searchType != null) {
            paramMap.put("FROM_DT", fromDt);
            paramMap.put("TO_DT", toDt);
            paramMap.put("ROAD_NMBR", roadNmbr);

            if (searchType.equals("mn")) {
                result = ixrMapper.findStatisticsTrafficAmountByMonth(paramMap);
            }
            else if (searchType.equals("dd")) {
                result = ixrMapper.findStatisticsTrafficAmountByDay(paramMap);
            }
        }
        return result;
    }

    public List<DaeroMngmDto> findIntersectionAtrdName() {
        return ixrMapper.findIntersectionAtrdName();
    }

    public List<TrafficStatisticsDto> findStatisticsCommSpeed(TrafficStatisticsDto.TrafficStatisticsDtoReq paramInfo, String searchType) {
        Map<String, String> paramMap = new HashMap<>();
        String fromDt   = paramInfo.getFromDt();
        String toDt     = paramInfo.getToDt();
        String atrdNm   = paramInfo.getAtrdNm();
        List<TrafficStatisticsDto> result = new ArrayList<>();
        if (fromDt != null && toDt != null && atrdNm != null && searchType != null) {
            paramMap.put("FROM_DT", fromDt);
            paramMap.put("TO_DT", toDt);
            paramMap.put("ATRD_NM", atrdNm);

            if (searchType.equals("mn")) {
                result = this.mapper.findStatisticsCommSpeedMonth(paramMap);
            }
            else if (searchType.equals("dd")) {
                result = this.mapper.findStatisticsCommSpeedDay(paramMap);
            }
        }
        return result;
    }

    public List<TrafficStatisticsDto> findStatisticsCommGrade(TrafficStatisticsDto.TrafficStatisticsDtoReq paramInfo, String searchType) {
        Map<String, String> paramMap = new HashMap<>();
        String fromDt   = paramInfo.getFromDt();
        String toDt     = paramInfo.getToDt();
        String atrdNm   = paramInfo.getAtrdNm();
        List<TrafficStatisticsDto> result = new ArrayList<>();
        if (fromDt != null && toDt != null && atrdNm != null && searchType != null) {
            paramMap.put("FROM_DT", fromDt);
            paramMap.put("TO_DT", toDt);
            paramMap.put("ATRD_NM", atrdNm);

            if (searchType.equals("mn")) {
                result = this.mapper.findStatisticsCommGradeMonth(paramMap);
            }
            else if (searchType.equals("dd")) {
                result = this.mapper.findStatisticsCommGradeDay(paramMap);
            }
        }
        return result;
    }

    public List<TrafficStatisticsCongestDto> findStatisticsCongest(TrafficStatisticsCongestDto.TrafficStatisticsCongestDtoReq paramInfo) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("STAT_YM",paramInfo.getStatYm());
        paramMap.put("DAY",paramInfo.getDay());
        return this.mapper.findStatisticsCongest(paramMap);
    }

}
