package com.its.op.controller.its.analysis;

import com.its.op.dto.its.analysis.TbTrafStat15mCountDto;
import com.its.op.dto.its.analysis.TbTrafStat15mListDto;
import com.its.op.service.its.ifsc.TbIfsc15mStatService;
import com.its.op.service.its.link.TbLink15mStatService;
import com.its.op.service.its.road.TbRoad15mStatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.NoSuchElementException;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/analysis/traf-hs")
@Api(tags = "04.분석-4.소통정보이력분석")
public class TbTrafStat15mAnalController {

    private final TbLink15mStatService linkService;
    private final TbIfsc15mStatService ifscService;
    private final TbRoad15mStatService roadService;

    @ApiOperation(value = "0.이력목록조회(TB_IFSC_STAT_15M)", response = TbTrafStat15mCountDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/stat", produces = {"application/json; charset=utf8"})
    public List<TbTrafStat15mCountDto> findAllCountByDateRange(
            @ApiParam(name = "HIST_TYPE", value = "LINK(링크), IFSC(서비스링크), ROAD(도로)", example = "IFSC", required = true)
            @RequestParam String HIST_TYPE,
            @ApiParam(name = "FROM_DT", value = "조회시작시각(YYYYMMDDHH24MI00)", example = "20210202000000", required = true)
            @RequestParam String FROM_DT,
            @ApiParam(name = "TO_DT", value = "조회종료시각(YYYYMMDDHH24MI59)", example = "20210202235959", required = true)
            @RequestParam String TO_DT
    ) throws NoSuchElementException {

        if (("LINK").equalsIgnoreCase(HIST_TYPE)) {
            return this.linkService.findAllCountByDateRange(FROM_DT, TO_DT);
        }
        else if (("IFSC").equalsIgnoreCase(HIST_TYPE)) {
            return this.ifscService.findAllCountByDateRange(FROM_DT, TO_DT);
        }
        else if (("ROAD").equalsIgnoreCase(HIST_TYPE)) {
            return this.roadService.findAllCountByDateRange(FROM_DT, TO_DT);
        }
        else {
            throw new NoSuchElementException("요청 파라미터가 잘못 됐습니다: " + HIST_TYPE);
        }
    }

    @ApiOperation(value = "1.이력데이터조회(TB_IFSC_STAT_15M)", response = TbTrafStat15mListDto.class, responseContainer = "ArrayList")
    @GetMapping(value = "/hist", produces = {"application/json; charset=utf8"})
    public List<TbTrafStat15mListDto> findAllDataByDateRange(
            @ApiParam(name = "HIST_TYPE", value = "LINK(링크), IFSC(서비스링크), ROAD(도로)", example = "IFSC", required = true)
            @RequestParam String HIST_TYPE,
            @ApiParam(name = "STAT_DT", value = "이력시각(YYYYMMDDHH24MI00)", example = "20210202000000", required = true)
            @RequestParam String STAT_DT
    ) throws NoSuchElementException {

        if (("LINK").equalsIgnoreCase(HIST_TYPE)) {
            return this.linkService.findAllDataByDateRange(STAT_DT);
        }
        else if (("IFSC").equalsIgnoreCase(HIST_TYPE)) {
            return this.ifscService.findAllDataByDateRange(STAT_DT);
        }
        else if (("ROAD").equalsIgnoreCase(HIST_TYPE)) {
            return this.roadService.findAllDataByDateRange(STAT_DT);
        }
        else {
            throw new NoSuchElementException("요청 파라미터가 잘못 됐습니다: " + HIST_TYPE);
        }
    }

}
