package com.its.op.service.its.facility;

import com.its.op.dao.repository.its.vds.TbVdsCtlrSttsHsRepository;
import com.its.op.dao.repository.its.vms.TbVmsCtlrSttsHsRepository;
import com.its.op.dto.its.facility.FcltSttsErrorDto;
import com.its.op.dto.its.facility.FcltSttsErrorInf;
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 TbFcltSttsErrorHsService {

    //private final TbCctvSttsHsRepository cctvSttsHsRepo;
    private final TbVmsCtlrSttsHsRepository vmsSttsHsRepo;
    private final TbVdsCtlrSttsHsRepository vdsSttsHsRepo;

    /**
     * 시설물 장애내역 조회
     * @param type
     * @param fromDt
     * @param toDt
     * @return
     */
    @Transactional(readOnly = true)
    public List<FcltSttsErrorDto> findAllFcltSttsErrorList(String type, String fromDt, String toDt) throws NoSuchElementException {
//        if (("CCTV").equalsIgnoreCase(type)) {
//            return findAllStatisticsCommErrByDateRangeResult(this.cctvSttsHsRepo.findAllSttsErrorByDateRange(fromDt, toDt), type);
//        }
//        else
        if (("VMS").equalsIgnoreCase(type)) {
            return findAllStatisticsCommErrByDateRangeResult(this.vmsSttsHsRepo.findAllSttsErrorByDateRange(fromDt, toDt), type);
        }
        else
        if (("VDS").equalsIgnoreCase(type)) {
            return findAllStatisticsCommErrByDateRangeResult(this.vdsSttsHsRepo.findAllSttsErrorByDateRange(fromDt, toDt), type);
        }
        else
        if (("ALL").equalsIgnoreCase(type)) {
            //List<FcltSttsErrorDto> cctvList = findAllStatisticsCommErrByDateRangeResult(this.cctvSttsHsRepo.findAllSttsErrorByDateRange(fromDt, toDt), "CCTV");
            List<FcltSttsErrorDto> vmsList = findAllStatisticsCommErrByDateRangeResult(this.vmsSttsHsRepo.findAllSttsErrorByDateRange(fromDt, toDt), "VMS");
            List<FcltSttsErrorDto> vdsList = findAllStatisticsCommErrByDateRangeResult(this.vdsSttsHsRepo.findAllSttsErrorByDateRange(fromDt, toDt), "VDS");

            final List<FcltSttsErrorDto> result = new ArrayList<>();
            //result.addAll(cctvList);
            result.addAll(vmsList);
            result.addAll(vdsList);
            return result;
        }
        else {
            throw new NoSuchElementException("요청 파라미터가 잘못 됐습니다: " + type);
        }
    }

    public List<FcltSttsErrorDto> findAllStatisticsCommErrByDateRangeResult(List<FcltSttsErrorInf> data, String fcltType) {

        List<FcltSttsErrorDto> result = new ArrayList<>();
        data.forEach(obj -> {
            FcltSttsErrorDto dto = FcltSttsErrorDto.builder()
                    .fcltType(obj.getFcltType())
                    .fcltNmbr(obj.getFcltNmbr())
                    .fcltId(obj.getFcltId())
                    .fcltNm(obj.getFcltNm())
                    .commSttsCd(obj.getCommSttsCd())
                    .doorSttsCd(obj.getDoorSttsCd())
                    .cboxTmpr(obj.getCboxTmpr().intValue())
                    .crtnDt(obj.getCrtnDt())
                    .build();
            dto.makeSttsDesc(fcltType);

            result.add(dto);
        });
        return result;
    }

}
