package com.its.wthr.webapp.controller;

import com.its.app.AppUtils;
import com.its.wthr.config.ServerConfig;
import com.its.wthr.repository.AppRepository;
import com.its.wthr.service.AtmpService;
import com.its.wthr.service.FrcsService;
import com.its.wthr.vo.AtmpPltnVo;
import com.its.wthr.vo.FrcsZoneVo;
import com.its.wthr.webapp.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

@Slf4j
@Controller
public class WebAppController {

    private final ResourceLoader resourceLoader;
    private final ServerConfig serverConfig;
    private final FileService fileService;

    public WebAppController(ResourceLoader resourceLoader, ServerConfig serverConfig, FileService fileService) {
        this.resourceLoader = resourceLoader;
        this.serverConfig = serverConfig;
        this.fileService = fileService;
    }

    @RequestMapping(value = {"/atmp"})
    public String atmp(Model model, HttpServletRequest request) {
        String result = "atmp";
        SortedMap<Integer, AtmpPltnVo> ctlrMap = new TreeMap<>();
        for (Map.Entry<String, AtmpPltnVo> e : AppRepository.getInstance().getAtmpPltnMap().entrySet()) {
            AtmpPltnVo obj = e.getValue();
            if (obj.getDEL_YN().equals("N")) {
                ctlrMap.put(Integer.valueOf(obj.getATMP_STTN_NMBR()), obj);
            }
        }
        model.addAttribute("ServerConfig", this.serverConfig);
        //model.addAttribute("list", AppRepository.getInstance().getAtmpPltnMap());
        model.addAttribute("list", ctlrMap);
        model.addAttribute("ServerTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return result;
    }

    @RequestMapping(value = ("/requestAtmp"))
    @ResponseBody
    public String requestAtmp() {
        String result = "대기오염 실시간현황을 요청 하였습니다.";
        AtmpService atmpService = (AtmpService) AppUtils.getBean(AtmpService.class);
        try {
            atmpService.doJob();
        }
        catch (Exception e) {
            result = "대기오염 실시간현황을 요청 중 오류가 발생 하였습니다.";
            return result;
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("requestAtmp thread sleep exception");
        }

        return result;
    }

    @RequestMapping(value = {"/frcs"})
    public String frcs(Model model, HttpServletRequest request) {
        String result = "frcs";
        SortedMap<Long, FrcsZoneVo> ctlrMap = new TreeMap<>();
        for (Map.Entry<String, FrcsZoneVo> e : AppRepository.getInstance().getFrcsZoneMap().entrySet()) {
            FrcsZoneVo obj = e.getValue();
            if (obj.getDEL_YN().equals("N")) {
                ctlrMap.put(Long.valueOf(obj.getVILG_FRCS_ZONE_CD()), obj);
            }
        }

        model.addAttribute("ServerConfig", this.serverConfig);
        //model.addAttribute("list", AppRepository.getInstance().getFrcsZoneMap());
        model.addAttribute("list", ctlrMap);
        model.addAttribute("ServerTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return result;
    }

    @RequestMapping(value = ("/requestFrcs"))
    @ResponseBody
    public String requestFrcs() {
        String result = "동네예보 정보를 요청 하였습니다.";
        FrcsService frcsService = (FrcsService)AppUtils.getBean(FrcsService.class);
        try {
            frcsService.doJob();
        }
        catch (Exception e) {
            result = "동네예보 정보 요청 중 오류가 발생 하였습니다.";
            log.error("동네예보 정보 요청 중 Exception 오류가 발생 하였습니다.");
            return result;
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("requestFrcs thread sleep exception");
        }

        return result;
    }

}
