package egovframework.controller;

import egovframework.service.impl.CommonServiceImpl;
import egovframework.utill.TrafficData;
import egovframework.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Controller
public class CommonController {

    private final CommonServiceImpl commonService;

    @ResponseBody
    @RequestMapping(value = "/common/getWeatherInfo.do", method = {RequestMethod.POST, RequestMethod.GET})
    public WeatherInfoVO getWeather() {
        return this.commonService.getWeatherInfo();
    }

    @ResponseBody
    @RequestMapping(value = "/common/getLinkVertexArr.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<RoadVertexArrVO> getLinkVertexArr(HttpServletRequest request) {
        if (TrafficData.getLink() != null) {
            return TrafficData.getLink();
        }
        else {
            return this.commonService.getLinkVertexArr();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/common/getIfscVertexArr.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<RoadVertexArrVO> getIfscVertexArr(HttpServletRequest request) {
        if (TrafficData.getIfsc() != null) {
            return TrafficData.getIfsc();
        }
        else {
            return this.commonService.getIfscVertexArr();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/common/getRoadVertexArr.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<RoadVertexArrVO> getRoadVertexArr(HttpServletRequest request) {
        if (TrafficData.getRoad() != null) {
            return TrafficData.getRoad();
        }
        else {
            return this.commonService.getRoadVertexArr();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/common/getVmsCtlr.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<VmsCtlrVO> getVmsCtlr() {
        return this.commonService.getVmsCtlr();
    }

    @ResponseBody
    @RequestMapping(value = "/common/getVmsPhaseImg.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<VmsCtlrVO> getVmsPhasImg(@RequestParam HashMap<String, String> paramMap) {
        return this.commonService.getVmsImgTxt(paramMap.get("vmsCtlrNmbr"));
    }

    @ResponseBody
    @RequestMapping(value = "/common/getCctvCtlr.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<CctvCtlrVO> getCctvCtlr() {
        return this.commonService.getCctvCtlr();
    }

    @ResponseBody
    @RequestMapping(value = "/common/getWcamCtlr.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<WcamCtlrVO> getWcamCtlr() {
        return this.commonService.getWcamCtlr();
    }

    @ResponseBody
    @RequestMapping(value = "/common/getIncdOcrr.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<IncdVO> getIncdOcrr() {
        return this.commonService.getIncdOcrr();
    }

    @ResponseBody
    @RequestMapping(value = "/common/getParkingInfo.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<ParkingInfoVO> getParkingInfo() {
        return this.commonService.getParkingInfo();
    }

    @ResponseBody
    @RequestMapping(value = "/parking/parking2.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<ParkingInfoVO> rtParking() {
        return this.commonService.rtParking();
    }

    @ResponseBody
    @RequestMapping(value = "/common/getNewParkingInfo.do", method = {RequestMethod.POST, RequestMethod.GET})
    public NewParkinginfoVO getNewParkingInfo(@RequestParam HashMap<String, String> paramMap) {
        return this.commonService.getNewParkingInfo(paramMap.get("parkId"));
    }

    @ResponseBody
    @RequestMapping(value = "/common/getNewParkingOcc.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<NewParkingOccVO> getNewParkingOcc(@RequestParam HashMap<String, String> paramMap) {
        return this.commonService.getNewParkingOcc(paramMap.get("parkId"));
    }

    @ResponseBody
    @RequestMapping(value = "/common/getTaasInfo.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<TaasVO> getTaasInfo() {
        List<TaasVO> tassList = this.commonService.getTaasInfo();
        SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyy년 MM월 dd일");
        for (TaasVO vo : tassList) {
            Date date;
            try {
                date = transFormat.parse(vo.getReport_date());
                vo.setReport_date(format.format(date));
            } catch (ParseException e) {
                throw new RuntimeException();
            }
        }
        return tassList;
    }

    @ResponseBody
    @RequestMapping(value = "/common/getVisitCount.do", method = {RequestMethod.POST, RequestMethod.GET})
    public VisitCountVO getVisitCount() {
        return this.commonService.getVisitCount();
    }

}
