package com.siginfo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.siginfo.common.CommonUtil;
import com.siginfo.service.CommonService;
import com.siginfo.vo.GroupListVO;
import com.siginfo.vo.IntInfo;

@Controller
public class TreeListController {
	
	@Autowired
	private CommonService commonService;
	
	@RequestMapping(value = "getTreeListFrame")
	public String getTreeListFrame(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		return "treeListFrame";
	}
	
	@RequestMapping(value = "getTreeList.do")
	public @ResponseBody List<Map<String,Object>> getTreeList(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("queryId", "getTreeList");
		
		List<Map<String,Object>> result = commonService.list(paramMap);
		
		for(Map<String,Object> map : result) {
			map.put("key", map.get("REGION_CD"));
			map.put("title", map.get("REGION_NM"));
			map.put("isLazy", true);
			map.put("isFolder", true);
		}
		
		return result;
	}
	
	@RequestMapping(value = "getSubTreeList.do")
	public @ResponseBody List<Map<String,Object>> getSubTreeList(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String regionCd = CommonUtil.checkNull(request.getParameter("key"));
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("regionCd", regionCd);
		paramMap.put("queryId", "getSubTreeList");
		
		List<Map<String,Object>> result = commonService.list(paramMap);
		
		for(Map<String,Object> map : result) {
			map.put("key", map.get("REGION_CD") + "_" + map.get("INT_NO"));
			map.put("title", map.get("INT_NM"));
			if(map.get("COMM_ON_OFF_FLAG") != null)
			{
				if(map.get("COMM_ON_OFF_FLAG").equals("0")) map.put("icon", "/signal/images/intStatusOn.png");
				else if(map.get("COMM_ON_OFF_FLAG").equals("1")) map.put("icon", "/signal/images/intStatusOff.png");
			}
			else
			{
				map.put("icon", "/signal/images/intStatusOff.png");
			}
		}
		
		return result;
	}
	
	@RequestMapping(value = "getOnlineCenterStatus.do")
	public @ResponseBody List<Map<String,Object>> getOnlineCenterStatus(HttpServletRequest request) throws Exception {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("queryId", "getOnlineCenterStatus");
		
		return commonService.list(paramMap);
	}
	
	@RequestMapping(value = "getOfflineCenterStatus.do")
	public @ResponseBody List<Map<String,Object>> getOfflineCenterStatus(HttpServletRequest request) throws Exception {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("queryId", "getOfflineCenterStatus");
		
		return commonService.list(paramMap);
	}
	
	@RequestMapping(value = "getOnlineIntStatus.do")
	public @ResponseBody List<Map<String,Object>> getOnlineIntStatus(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String[] regionCdArr = request.getParameter("regionCd").split(",");
		
		paramMap.put("regionCdArr", regionCdArr);
		paramMap.put("queryId", "getOnlineIntStatus");
		
		List<Map<String,Object>> Result = commonService.list(paramMap);
		
		for(Map<String,Object> map : Result)
		{
			String intNo = CommonUtil.clobToString(((java.sql.Clob)map.get("INT_NO")));
			String commOnOffFlag = CommonUtil.clobToString(((java.sql.Clob)map.get("COMM_ON_OFF_FLAG")));
			
			map.put("INT_NO", intNo);
			map.put("COMM_ON_OFF_FLAG", commOnOffFlag);
		}
		
		return Result;
		
	}
	
	@RequestMapping(value = "getOfflineIntStatus.do")
	public @ResponseBody List<Map<String,Object>> getOfflineIntStatus(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String[] regionCdArr = request.getParameter("regionCd").split(",");
		paramMap.put("regionCdArr", regionCdArr);
		paramMap.put("queryId", "getOfflineIntStatus");
		
		List<Map<String,Object>> Result = commonService.list(paramMap);
		
		for(Map<String,Object> map : Result)
		{
			String intNo = CommonUtil.clobToString(((java.sql.Clob)map.get("INT_NO")));
			String commOnOffFlag = CommonUtil.clobToString(((java.sql.Clob)map.get("COMM_ON_OFF_FLAG")));
			
			map.put("INT_NO", intNo);
			map.put("COMM_ON_OFF_FLAG", commOnOffFlag);
		}
		
		return Result;
		
	}
	
	@RequestMapping(value = "getIntTreeList.do")
	public @ResponseBody List<Map<String,Object>> getIntTreeList(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("queryId", "getIntTreeList");
	
		List<Map<String,Object>> onResult = commonService.list(paramMap);
		
//		for(Map<String,Object> map : onResult)
//		{
//			map.put("INT_NO",CommonUtil.clobToString(((java.sql.Clob)map.get("INT_NO"))));
//			map.put("INT_NM",CommonUtil.clobToString(((java.sql.Clob)map.get("INT_NM"))));
//			map.put("INT_X_COORD",CommonUtil.clobToString(((java.sql.Clob)map.get("INT_X_COORD"))));
//			map.put("INT_Y_COORD",CommonUtil.clobToString(((java.sql.Clob)map.get("INT_Y_COORD"))));
//			map.put("NETWORK_TYPE",CommonUtil.clobToString(((java.sql.Clob)map.get("NETWORK_TYPE"))));
//			map.put("NODE_ID",CommonUtil.clobToString(((java.sql.Clob)map.get("NODE_ID"))));
//		}
		
		return onResult;
	}
	
	@RequestMapping(value = "getGroupTreeList.do")
	public @ResponseBody List<GroupListVO> getGroupTreeList(Locale locale, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("regionCd", CommonUtil.checkNull(request.getParameter("regionCd")));
		
		paramMap.put("queryId", "getGroupTreeList");
		
		List<Map<String,Object>> result = commonService.list(paramMap);
		Map<String, GroupListVO> checktMap = new HashMap<String, GroupListVO>();
		List<GroupListVO> resultList = new ArrayList<GroupListVO>();
		for(Map<String,Object> map : result)
		{
			String regionCd  = (String) map.get("REGION_CD");
			String sigunguCd = String.valueOf(map.get("SIGUNGU_CD"));
			String groupNm   = (String) map.get("GROUP_NM");
			if (checktMap.get(regionCd + "_" + String.valueOf(sigunguCd) + "_" + groupNm) == null) {
				String regionNm  = (String) map.get("REGION_NM");
				Double xCoord = null;
				Double yCoord = null;
				Double sigunguX = null;
				Double sigunguY = null;
				if (map.get("X_COORD") != null) {
					xCoord = Double.parseDouble(String.valueOf(map.get("X_COORD")));
				}
				if (map.get("Y_COORD") != null) {
					yCoord = Double.parseDouble(String.valueOf(map.get("Y_COORD")));
				}
				String sigunguNm = (String) map.get("SIGUNGU_NM");
	             
	            if (map.get("SIGUNGU_X") != null) {
	            	sigunguX  = Double.parseDouble(String.valueOf(map.get("SIGUNGU_X")));
				}
				if (map.get("SIGUNGU_Y") != null) {
					sigunguY  = Double.parseDouble(String.valueOf(map.get("SIGUNGU_Y")));
				}
				GroupListVO vo = new GroupListVO(regionCd,regionNm, xCoord, yCoord, sigunguCd, sigunguNm, sigunguX, sigunguY, groupNm);
				checktMap.put(regionCd + "_" + sigunguCd + "_" + groupNm, vo);
				resultList.add(vo);
			}
			Long intNo 	 = Long.parseLong(String.valueOf(map.get("INT_NO")));
			String intNm = (String) map.get("INT_NM");
			Double intXCoord = null; 
			Double intYCoord = null;
			
			if (map.get("INT_X_COORD") != null) {
				intXCoord = Double.parseDouble(String.valueOf(map.get("INT_X_COORD")));
			}
			if (map.get("INT_Y_COORD") != null) {
				intYCoord = Double.parseDouble(String.valueOf(map.get("INT_Y_COORD")));
			}
			IntInfo info = new IntInfo(intNo, intNm, intXCoord, intYCoord);
			checktMap.get(regionCd + "_" + sigunguCd + "_" + groupNm).getGroupList().add(info);
		}
		
//		List<GroupListVO> resultList = new ArrayList<GroupListVO>(checktMap.values());
		return resultList;
	}
}
