package com.tsi.sig.server.controller;

import com.tsi.sig.server.objects.UserVO;
import com.tsi.sig.server.service.MainService;
import com.tsi.sig.server.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
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.naming.AuthenticationException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@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({"/main.do"})
    public String main(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        SecurityContext member = (SecurityContext)session.getAttribute("SPRING_SECURITY_CONTEXT");
        return member == null ? "login" : "main";
    }

    @RequestMapping({"/refreshNodeList.do"})
    @ResponseBody
    public ResultVo refreshNodeList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return mainService.refreshNodeList();
    }

    @RequestMapping({"/userEdit.do"})
    public String userEdit() throws Exception {
        return "userEdit";
    }

    @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");
        model.addAttribute("center", this.mainService.getCenterCode());
        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(@RequestParam Map<String,String> paramMap, 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);
        List<CvibNodeVO> nodes = mainService.getRegionList(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";
    }

    @RequestMapping(value = "/getCvibList.do", method = RequestMethod.POST)
    @ResponseBody
//    public List<CvibNodeStatusVo> cvibInfoList(@RequestParam Map<String, String> paramMap) throws Exception {
    public Map<String,List<?>> cvibInfoList(@RequestParam Map<String, String> paramMap) throws Exception {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal != null && principal != "anonymousUser") {
            UserVO user = (UserVO)principal;
            String[] test = new String[]{"I", "B", "N", "M", "S"};
            List<String> strList = new ArrayList<>(Arrays.asList(test));
            if ( strList.contains(user.getUserGrade())) {
                return mainService.getCvibNodeStatusList(paramMap);
            }
            else {
                throw new AuthenticationException("사용자 권한을 확인해주세요ㅕ.");
            }
        }
        throw new AuthenticationException("로그인 하지않고 API 를 사용할 수 없습니다.");
    }
    @RequestMapping(value = "/getCvibDetail.do", method = RequestMethod.POST)
    @ResponseBody
    public CvibNodeStatusVo getCvibDetail(String nodeId) throws Exception {
        return mainService.getCvibNodeDetail(nodeId);
    }

    @RequestMapping(value = "/getEvpsInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public List<EvpServiceVo> getEvpsInfoList() throws Exception{
        return mainService.getEvpServiceList();
    }

//    @RequestMapping(value = "/getEvpServiceList.do", method = RequestMethod.POST)
//    @ResponseBody
//    public List<EvpServiceVo> getEvpServiceList() throws Exception {
//        return mainService.getEvpServiceList();
//    }
    @RequestMapping(value = "/getEvpRouteList.do", method = RequestMethod.POST)
    @ResponseBody
    public List<EvpRouteVo> getEvpRouteList(@RequestParam HashMap<String,Object> paramMap) throws Exception {
        return mainService.getEvpRouteList(paramMap);
    }

    @RequestMapping(value = "/getEvpHistory.do", method = RequestMethod.POST)
    @ResponseBody
    public List<EvpServiceVo> getEvpHistory(@RequestParam HashMap<String,Object> paramMap) throws Exception {
        return mainService.getEvpHistoryList(paramMap);
    }

    @RequestMapping(value = "/getCenterCode.do", method = RequestMethod.POST)
    @ResponseBody
    public List<EvpCenterVo> getCenterCode() throws Exception {
        return mainService.getCenterCode();
    }
}
