package egovframework.com.its.web.server.controller;

import egovframework.com.its.web.server.service.CommonService;
import egovframework.com.its.web.server.util.TrafficData;
import egovframework.com.its.web.server.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RequiredArgsConstructor
@Controller
public class CommonController {

    private final CommonService 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.getLinkVertexArr() != null) {
            return TrafficData.getLinkVertexArr();
        }
        else {
            return this.commonService.getLinkVertexArr();
        }
    }

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

    @ResponseBody
    @RequestMapping(value = "/common/getRoadVertexArr.do", method = {RequestMethod.POST, RequestMethod.GET})
    public List<RoadVertexArrVO> getRoadVertexArr(HttpServletRequest request) {
        if (TrafficData.getRoadVertexArr() != null) {
            return TrafficData.getRoadVertexArr();
        }
        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}, produces = MediaType.APPLICATION_JSON_VALUE)
//    @RequestMapping(value = "/common/getCctvCtlr.do", method = {RequestMethod.POST, RequestMethod.GET}, consumes={"application/json"})
    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/getVisitCount.do", method = {RequestMethod.POST, RequestMethod.GET})
    public VisitCountVO getVisitCount() {
        return this.commonService.getVisitCount();
    }

//    @ResponseBody
//    @RequestMapping(value = "/common/downExcel.do", method = {RequestMethod.POST})
//    public Map<String, String> downExcel(String[] ixrIds,String from, String to, String type) throws IOException {
//        return this.commonService.downExcel(ixrIds, from, to, type);
//    }

    @GetMapping(value = "/error-page")
    public String errorPage(){
        return "/error/error.empty";
    }

}
