package com.tsi.sig.server.controller;

import com.tsi.sig.server.service.MainService;
import com.tsi.sig.server.utils.CommonUtil;
import com.tsi.sig.server.vo.CvibNodeVO;
import lombok.extern.slf4j.Slf4j;
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.HashMap;
import java.util.List;
import java.util.Locale;

@Slf4j
@Controller
public class MainController {
    private final MainService mainService;

    public MainController(MainService mainService) {
        this.mainService = mainService;
    }

    @RequestMapping(value="/login.do")
    public String page() throws Exception {
        System.out.println("=======================================login.do====================");
        return "login";
    }

    @RequestMapping(value="/error.do")
    public String error() throws Exception{
        //log.info("CALL.........................................: error");
        return "error";
    }

    @RequestMapping(value = "/main.do")
    public String main() throws Exception {
        //log.info("CALL.........................................: main");
        return "main";
    }

    @RequestMapping(value = "getBottomListFrame.do")
    public String getBottomListFrame() throws Exception {
        //log.info("CALL.........................................: getBottomListFrame");
        return "bottomListFrame";
    }

    @RequestMapping(value = "getTreeListFrame.do")
    public String getTreeListFrame(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //log.info("CALL.........................................: getTreeListFrame");
        return "treeListFrame";
    }

    @RequestMapping(value = "/getIntTreeList.do", method = RequestMethod.POST)
    public @ResponseBody
    List<CvibNodeVO> getIntTreeList(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //log.info("CALL.........................................: getIntTreeList");
        List<CvibNodeVO> nodes = mainService.getLodeList();
        //log.info("getIntTreeList: {}", nodes.toString());
        return nodes;
    }

    @RequestMapping(value = "/getSignalInfo.do", method = RequestMethod.POST)
    public @ResponseBody
    List<CvibNodeVO> getSignalInfo(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //log.info("CALL.........................................: getSignalInfo");
/*
        //log.info("minX: {}", request.getParameter("minX"));
        //log.info("minY: {}", request.getParameter("minY"));
        //log.info("maxX: {}", request.getParameter("maxX"));
        //log.info("maxY: {}", request.getParameter("maxY"));
*/
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("minX", CommonUtil.checkNull(request.getParameter("minX")));
        paramMap.put("minY", CommonUtil.checkNull(request.getParameter("minY")));
        paramMap.put("maxX", CommonUtil.checkNull(request.getParameter("maxX")));
        paramMap.put("maxY", CommonUtil.checkNull(request.getParameter("maxY")));
        List<CvibNodeVO> nodes = mainService.getCvibMapNodeList(paramMap);
        //log.info("{} EA. {}", nodes.size(), nodes.toString());
        response.setContentType("application/json; charset=UTF-8");

        return nodes;
    }

    @RequestMapping(value = "cvibInfoDetail.do", method = RequestMethod.GET)
    public String cvibInfoDetail(@RequestParam("nodeId") String nodeId, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //log.info("CALL.........................................: cvibInfoDetail");
        model.addAttribute("nodeId", nodeId);
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("nodeId", nodeId);
        List<CvibNodeVO> nodes = mainService.getCvibNodeInfo(paramMap);
        CvibNodeVO node = null;
        if (nodes != null && nodes.size() > 0) {
            node = nodes.get(0);
        }
        model.addAttribute("node", node);
        return "cvibInfoDetail";
    }

}
