package egovframework.controller;

import egovframework.service.impl.TrafficStatsServiceImpl;
import egovframework.vo.VdsStatsVO;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
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, @RequestParam Map<String, String> paramMap) {
        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);

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

        model.addAttribute("atrdNmList", this.trafficStatsService.getAtrdNm());
        model.addAttribute("siteSubInfo", "subWrap");
        model.addAttribute("subTitle", "소통통계");
        return "trafficStats/info.tiles";
    }

    @RequestMapping(value = "/trafficStats/trafficStats.do", method = RequestMethod.GET)
    public String trafficStats(Model model) {
        model.addAttribute("siteSubInfo", "subWrap");
        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);
        int thisHour = cal.get(Calendar.HOUR_OF_DAY);
        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);
        model.addAttribute("thisHour", thisHour);

        return "trafficStats/trafficStatistics.tiles";
    }

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

    @RequestMapping(value="/trafficStats/exportExcel.do", method= RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Resource> exportExcel(HttpServletRequest req, HttpServletResponse res, @RequestParam Map<String, String> paramMap) {
        return this.trafficStatsService.exportExcel(paramMap);
    }
}
