package com.its.op.service.its.statistics;

import com.its.op.dao.repository.its.cctv.TbCctvCtlrSttsHsRepository;
import com.its.op.dao.repository.its.rse.TbRseCtlrSttsHsRepository;
import com.its.op.dao.repository.its.vds.TbVdsCtlrSttsHsRepository;
import com.its.op.dao.repository.its.vms.TbVmsCtlrSttsHsRepository;
import com.its.op.dto.its.statistics.FcltSttsStatErrorDto;
import com.its.op.dto.its.statistics.FcltSttsStatErrorInf;
import com.its.op.dto.its.statistics.FcltSttsStatTypeDto;
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.List;
import java.util.NoSuchElementException;

@Slf4j
@RequiredArgsConstructor
@Service
public class FcltSttsStatisticsService {

    private final TbCctvCtlrSttsHsRepository cctvSttsHsRepo;
    private final TbVmsCtlrSttsHsRepository vmsSttsHsRepo;
    private final TbVdsCtlrSttsHsRepository vdsSttsHsRepo;
    private final TbRseCtlrSttsHsRepository rseSttsHsRepo;

    /**
     * 통계 - 시설물 통신단절 통계 - 시설물 구분 목록
     * @return
     */
    public List<FcltSttsStatTypeDto> findAllFcltSttsType(boolean isAll) {

        List<FcltSttsStatTypeDto> result = new ArrayList<>();

        if (isAll) {
            result.add(FcltSttsStatTypeDto.builder().fcltSeq(0).fcltCd("ALL").fcltNm("전 체").build());
        }
        result.add(FcltSttsStatTypeDto.builder().fcltSeq(1).fcltCd("CCTV").fcltNm("CCTV").build());
        result.add(FcltSttsStatTypeDto.builder().fcltSeq(2).fcltCd("VMS").fcltNm("VMS").build());
        result.add(FcltSttsStatTypeDto.builder().fcltSeq(3).fcltCd("VDS").fcltNm("VDS").build());
        result.add(FcltSttsStatTypeDto.builder().fcltSeq(4).fcltCd("DSRC").fcltNm("DSRC").build());

        return result;
    }

    public List<FcltSttsStatErrorDto> findAllStatisticsCommErrByDateRangeResult(List<FcltSttsStatErrorInf> data) {

        List<FcltSttsStatErrorDto> result = new ArrayList<>();
        data.forEach(obj -> {
            result.add(FcltSttsStatErrorDto.builder()
                    .fcltNmbr(obj.getFcltNmbr())
                    .fcltId(obj.getFcltId())
                    .fcltNm(obj.getFcltNm())
                    .totCnt(obj.getTotCnt().intValue())
                    .cms1Cnt(obj.getCms1Cnt().intValue())
                    .cms2Cnt(obj.getCms2Cnt().intValue())
                    .build());
        });
        return result;
    }

    @Transactional(readOnly = true)
    public List<FcltSttsStatErrorDto> findAllFcltStts(String fcltTypeCd, String fromDt, String toDt) {

        if (("CCTV").equalsIgnoreCase(fcltTypeCd)) {
            return findAllStatisticsCommErrByDateRangeResult(this.cctvSttsHsRepo.findAllStatisticsCommErrByDateRange(fromDt, toDt));
        }
        else
        if (("VMS").equalsIgnoreCase(fcltTypeCd)) {
            return findAllStatisticsCommErrByDateRangeResult(this.vmsSttsHsRepo.findAllStatisticsCommErrByDateRange(fromDt, toDt));
        }
        else
        if (("VDS").equalsIgnoreCase(fcltTypeCd)) {
            return findAllStatisticsCommErrByDateRangeResult(this.vdsSttsHsRepo.findAllStatisticsCommErrByDateRange(fromDt, toDt));
        }
        else
        if (("DSRC").equalsIgnoreCase(fcltTypeCd)) {
            return findAllStatisticsCommErrByDateRangeResult(this.rseSttsHsRepo.findAllStatisticsCommErrByDateRange(fromDt, toDt));
        }
        else {
            throw new NoSuchElementException("요청 파라미터가 잘못 됐습니다: " + fcltTypeCd);
        }
    }

}
