package com.siginfo.controller;

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;

@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<Map<String, Object>> 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);
		
		for(Map<String,Object> map : result)
		{
			String groupNm   = CommonUtil.clobToString(((java.sql.Clob)map.get("GROUP_NM")));
			String intNo     = CommonUtil.clobToString(((java.sql.Clob)map.get("INT_NO")));
			String intNm     = CommonUtil.clobToString(((java.sql.Clob)map.get("INT_NM")));
			String intXCoord = CommonUtil.clobToString(((java.sql.Clob)map.get("INT_X_COORD")));
			String intYCoord = CommonUtil.clobToString(((java.sql.Clob)map.get("INT_Y_COORD")));
			
			map.put("GROUP_NM", groupNm);
			map.put("INT_NO", intNo);
			map.put("INT_NM", intNm);
			map.put("INT_X_COORD", intXCoord);
			map.put("INT_Y_COORD", intYCoord);
		}
		
		return result;
	}
}
