package com.its.app.webapp.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.its.app.config.ServerConfig;
import com.its.app.repository.AppRepository;
import com.its.app.vo.voIcmpCtlr;
import com.its.app.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 javax.servlet.http.HttpServletRequest;
import java.io.IOException;
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 = {"/cctv"})
    public String cctv(Model model, HttpServletRequest request) {
        String result = "cctv";
        SortedMap<Integer, voIcmpCtlr> ctlrMap = new TreeMap<>();
        for (Map.Entry<String, voIcmpCtlr> e : AppRepository.getInstance().getCctvCtlrMap().entrySet()) {
            voIcmpCtlr obj = e.getValue();
            ctlrMap.put(Integer.valueOf(obj.getCTLR_NMBR()), obj);
        }
        model.addAttribute("ServerConfig", this.serverConfig);
        //model.addAttribute("list", AppRepository.getInstance().getCtlrMap().entrySet());
        model.addAttribute("list", ctlrMap);
        model.addAttribute("ServerTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        ObjectMapper mapper = new ObjectMapper();
        String jsonList = "";
        try {
            jsonList = mapper.writeValueAsString(AppRepository.getInstance().getCctvCtlrMap());
        }
        catch (IOException e) {
            log.error("WebAppController.logFiles: Exception: {}", e.toString());
        }
        model.addAttribute("objList", jsonList);
        return result;
    }

    @RequestMapping(value = {"/wcam"})
    public String wcam(Model model, HttpServletRequest request) {
        String result = "wcam";
        SortedMap<Integer, voIcmpCtlr> ctlrMap = new TreeMap<>();
        for (Map.Entry<String, voIcmpCtlr> e : AppRepository.getInstance().getWcamCtlrMap().entrySet()) {
            voIcmpCtlr obj = e.getValue();
            ctlrMap.put(Integer.valueOf(obj.getCTLR_NMBR()), obj);
        }
        model.addAttribute("ServerConfig", this.serverConfig);
        //model.addAttribute("list", AppRepository.getInstance().getCtlrMap().entrySet());
        model.addAttribute("list", ctlrMap);
        model.addAttribute("ServerTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        ObjectMapper mapper = new ObjectMapper();
        String jsonList = "";
        try {
            jsonList = mapper.writeValueAsString(AppRepository.getInstance().getWcamCtlrMap());
        }
        catch (IOException e) {
            log.error("WebAppController.logFiles: Exception: {}", e.toString());
        }
        model.addAttribute("objList", jsonList);
        return result;
    }

}
