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

import egovframework.com.its.web.server.service.AdminService;
import egovframework.com.its.web.server.service.TrafficStatsService;
import egovframework.com.its.web.server.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
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.awt.*;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Controller
@RequestMapping("/statistics")
@Slf4j
public class TrafficStatsController {

    private final TrafficStatsService trafficStatsService;
    private final AdminService adminService;

    @RequestMapping(value = "/getAtrdStat.do", method = RequestMethod.POST, produces = {"application/json; charset=utf8"})
    public @ResponseBody Map<String, Object> getAtrdStat(String[] atrdIds, String from, String to, String type) throws IOException {
        return this.trafficStatsService.getAtrdStat(atrdIds, from, to, type);
    }

    @RequestMapping(value = "/info.do", method = RequestMethod.GET)
    public String info(Model model, HttpServletRequest request, HttpServletResponse response,  @Value("${apiKey}") String apiKey,
                       @Value("${statistics.max-records}") String statLimit) {

        this.trafficStatsService.setDateParam(model);

        model.addAttribute("atrdNmList", this.trafficStatsService.getAtrdNm());
        model.addAttribute("subTitle", "소통 통계");
        model.addAttribute("siteSubInfo", "statistics");
        model.addAttribute("key", apiKey);
        model.addAttribute("statLimit", statLimit);
        return "statistics/info.tiles";
    }

    @RequestMapping(value = "/ixrTrafficStats.do", method = RequestMethod.GET)
    public String trafficStats(Model model,  @Value("${apiKey}") String apiKey, @Value("${statistics.max-records}") String statLimit) {
        model.addAttribute("subTitle", "교차로 교통량 통계");
        model.addAttribute("siteSubInfo", "statistics");
        this.trafficStatsService.setDateParam(model);
        model.addAttribute("list", this.trafficStatsService.getIxrList());
        model.addAttribute("key", apiKey);
        model.addAttribute("statLimit", statLimit);
        return "statistics/trafficStats.tiles";
    }

    @RequestMapping(value = "/connStat.do", method = RequestMethod.GET)
    public String connectionStat(Model model) {
        model.addAttribute("siteSubInfo", "sub ptatms");
        model.addAttribute("subTitle", "접속통계");
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumIntegerDigits(2);

        Calendar cal = Calendar.getInstance();

        model.addAttribute("thisYear", Integer.toString(cal.get(Calendar.YEAR)));
        model.addAttribute("thisMonth", nf.format(cal.get(Calendar.MONTH) + 1));
        model.addAttribute("thisDate", nf.format(cal.get(Calendar.DATE)));
        model.addAttribute("lastDate", "31");
        model.addAttribute("thisHour", nf.format(cal.get(Calendar.HOUR_OF_DAY)));
        return "statistics/connStat.tiles";
    }

    @RequestMapping(value = "/getIxrList.do", method = RequestMethod.POST)
    public @ResponseBody List<ScIxrVO> getIxrList() {
        return this.trafficStatsService.getIxrList();
    }

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

    @RequestMapping(value = "/getStatsList.do", method = RequestMethod.POST)
    public @ResponseBody List<StatsVO> getStatsList(@RequestParam Map<String, Object> paramMap) {
        List<StatsVO> result = null;
        if (paramMap.get("date") != null) {
            String date     = (String)paramMap.get("date");
            paramMap.put("statDate", date);
            result = this.adminService.getHHStatus(paramMap);
        }
        return result;
    }

    @RequestMapping(value = "/getYearStatsList.do", method = RequestMethod.POST)
    public @ResponseBody List<PeriodStatVO> getYearStatsList(@RequestParam Map<String, Object> paramMap) {
        return this.adminService.getYearStatsList(paramMap);
    }

    @RequestMapping(value = "/getMonthStatsList.do", method = RequestMethod.POST)
    public @ResponseBody List<PeriodStatVO> getMonthStatsList(@RequestParam Map<String, Object> paramMap) {
        return this.adminService.getMonthStatsList(paramMap);
    }

    @RequestMapping(value = "/getHourStatsList.do", method = RequestMethod.POST)
    public @ResponseBody List<PeriodStatVO> getHourStatsList(@RequestParam Map<String, Object> paramMap) {
        return this.adminService.getHourStatsList(paramMap);
    }

}
