package egovframework.controller;

import egovframework.service.impl.TrafficStatsServiceImpl;
import egovframework.vo.VdsStatsVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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 javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Controller
public class TrafficStatsController {

    private final TrafficStatsServiceImpl trafficStatsService;

    @RequestMapping(value = "/trafficStats/getVertexHistoryText.do", method = RequestMethod.POST)
    public @ResponseBody List<Map<String, Object>> getVertexHistoryText(Model model, HttpServletRequest request) {

        String atrdNm = request.getParameter("atrdNm");
        String yyyymmddhh = request.getParameter("yyyymmddhh");
        String yyyymmddhh2 = request.getParameter("yyyymmddhh2");
        String sTimeTypeVal = request.getParameter("sTimeTypeVal");

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("atrdNm", atrdNm);
        paramMap.put("yyyymmddhh", yyyymmddhh);
        paramMap.put("yyyymmddhh2", yyyymmddhh2);
        paramMap.put("sTimeTypeVal", sTimeTypeVal);
        return this.trafficStatsService.getVertexHistoryByRoadText(paramMap);
    }

    @RequestMapping(value = "/trafficStats/info.do", method = RequestMethod.GET)
    public String info(Model model, HttpServletRequest request, HttpServletResponse response) {

        Calendar cal = Calendar.getInstance();
        int thisYear = cal.get(Calendar.YEAR);
        int thisMonth = cal.get(Calendar.MONTH) + 1;
        int thisDate = cal.get(Calendar.DATE);
        int thisHour = cal.get(Calendar.HOUR_OF_DAY);
        //int lastDate = cal.getActualMaximum(Calendar.DATE);
        int lastDate = 31;

        model.addAttribute("thisYear", thisYear);
        model.addAttribute("thisMonth", thisMonth);
        model.addAttribute("thisDate", thisDate);
        model.addAttribute("thisHour", thisHour);
        model.addAttribute("lastDate", lastDate);

        //model.addAttribute("ifscTrafInfo", mainService.getIfscTrafInfo(atrdId));
        model.addAttribute("atrdNmList", this.trafficStatsService.getAtrdNm());
        return "trafficStats/infoNew.tiles";
    }

    @RequestMapping(value = "/trafficStats/trafficStats.do", method = RequestMethod.GET)
    public String trafficStats(Model model) {
        model.addAttribute("subTitle", "교통량 통계정보");
        List<Map<String, Object>> vdsCtlrList = this.trafficStatsService.getVdsCtlrList();

        Calendar cal = Calendar.getInstance();
        int thisYear = cal.get(Calendar.YEAR);
        int thisMonth = cal.get(Calendar.MONTH) + 1;
        int thisDate = cal.get(Calendar.DATE);

        cal.set(thisYear, thisMonth - 1, thisDate);
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        model.addAttribute("vdsCtlrList", vdsCtlrList);
        model.addAttribute("thisYear", thisYear);
        model.addAttribute("thisMonth", thisMonth);
        model.addAttribute("thisDate", thisDate);
        model.addAttribute("lastDay", lastDay);

        return "trafficStats/trafficStats.tiles";
    }

    @RequestMapping(value = "/common/getVdsStatsInfo.do", method = RequestMethod.GET)
    public @ResponseBody List<VdsStatsVO> getVdsStatsInfo(@RequestParam HashMap<String, String> paramMap) {
        return this.trafficStatsService.getVdsStatsInfo(paramMap);
    }
}
