package com.eactive.eai.rms.onl.adapter.sna;

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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.eactive.eai.data.entity.onl.server.EAIServer;
import com.eactive.eai.rms.common.spring.BaseController;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;
import com.eactive.eai.rms.data.entity.onl.server.EAIServerService;
import com.eactive.eai.rms.onl.common.exception.EMSRuntimeException;
import com.eactive.eai.rms.onl.manage.adapter.adapter.AdapterManService;
import com.eactive.eai.rms.onl.manage.sna.lugroup.LuGroupService;
import com.google.gson.Gson;

@Controller
public class SnaWcaConnectionListController extends BaseController{
	
	@Autowired
	@Qualifier("snaWcaConnectionListService")
	private SnaWcaConnectionListService service;
	
	@Autowired
	@Qualifier("luGroupService")
	private LuGroupService groupService; 
	
	@Autowired
	private AdapterManService adapterManService; 
	
	@Autowired
	private EAIServerService eaiServerService;
	

	@RequestMapping(value= "/onl/adapter/sna/snaWcaConnectionListMan.view")
	private String viewList(HttpServletRequest request, @RequestParam(value = "cmd", required = false ) String cmd ) throws Exception {
		if (cmd == null || "LIST".equals(cmd.toUpperCase())){
			cmd = "";
		}else {
			cmd = CamelCaseUtil.converCamelCase(cmd);
		}
		String path = request.getServletPath();
		return path.split("[.]")[0]+cmd;
	}   
	
	@RequestMapping(value= "/onl/adapter/sna/snaWcaConnectionListMan.json",params = "cmd=LIST_INIT_COMBO")
	public ModelAndView initCombo( HttpServletRequest request,HttpServletResponse response) throws Exception {
	  
		 HashMap paramMap = new HashMap();
		   List groupList  = groupService.selectSnaGroupList();
		   List adapterList  = groupService.selectSnaAdapterList();
		   List luGroupShortNameList  = groupService.selectLuGroupShortNameList();
		   List luGroupList  = groupService.selectLuGroupList(paramMap);
		   
		   Map resultMap = new HashMap();
	       resultMap.put("groupRows"  , groupList ); 
	       resultMap.put("adapterRows" , adapterList);
	       resultMap.put("luGroupShortNameRows" , luGroupShortNameList);
	       resultMap.put("luGroupList" , luGroupList);
				
		   ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		   return modelAndView;                      
	} 
	
	@RequestMapping(value= "/onl/adapter/sna/snaWcaConnectionListMan.json",params = "cmd=LIST_CHANGE_COMBO")
	public ModelAndView initCombo2( HttpServletRequest request,
	       HttpServletResponse response,String adptrGstatSevrName , String adptrGroupName) throws Exception {
	  HashMap paramMap = new HashMap();
	  paramMap.put("adptrGstatSevrName", adptrGstatSevrName);
	  paramMap.put("adptrGroupName", adptrGroupName);
	  List luGroupList  = groupService.selectLuGroupList(paramMap);
	   
	  Map resultMap = new HashMap();
      resultMap.put("luGroupList" , luGroupList);
			
	  ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
	  return modelAndView;            
	} 	
	
	@RequestMapping(value= "/onl/adapter/sna/snaWcaConnectionListMan.json", params = "cmd=LIST")
	private ModelAndView selectList(HttpServletRequest request,
			JsonPageVo pageVo ,@RequestParam HashMap<String,Object> paramMap) throws Exception {
		
		EAIServer eaiServer = eaiServerService
				.findFirstByHostname((String) paramMap.get("hostName"))
				.orElseThrow(() -> new EMSRuntimeException("HOST를 조회 할 수 없습니다."));
		
		// SnaStatusCmd 설정
		String statusCmd = null;

		statusCmd = "WcaStatus -t " + paramMap.get("adptGrpNm") + " -gA "
				+ paramMap.get("luGrpNm") + " -host " + eaiServer.getEaisevrip();
		
		paramMap.put("WcaStatusCmd", statusCmd);
		List list = service.executeWcaStatus(paramMap);
		if(logger.isInfoEnabled()) logger.info("cmd list size :" + list.size());
		//SNA 어댑터 APP_CODE 프라퍼티 MAP 담기
		List<Map<String, String>> dbMapList = adapterManService.selectAdapterPropInfo(paramMap);
		HashMap<String, Object> dbMap = new HashMap<String, Object>();
		
		for(Map<String, String> tmp : dbMapList){
			dbMap.put(tmp.get("ADPTRBZWKNAME"), tmp.get("PRPTY2VAL"));
		}
		
		//메모리 DB비교 결과
		for (int i=0; i<list.size();i++){
			HashMap<String,Object> tmp = (HashMap<String, Object>) list.get(i);
			
			if(tmp.get("appCode") != null){
				if(tmp.get("appCode").equals(dbMap.get(tmp.get("adapterName")))){
					tmp.put("compare", "true");
				}else{
					tmp.put("compare", "false");
				}
			}else{
				if(dbMap.get(tmp.get("adapterName")) == null || "".equals(dbMap.get(tmp.get("adapterName")))){
					tmp.put("compare", "true");
				}else{
					tmp.put("compare", "false");
				}
			}
		}
		
		
		if(logger.isInfoEnabled()) logger.info("메모리 DB비교 결과 list size :" + list.size());
		
		Map resultMap = new HashMap();
		resultMap.put("rows"   , list);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;
	}	
	
	@RequestMapping(value= "/onl/adapter/sna/snaWcaConnectionListMan.excel",params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam HashMap<String,Object> paramMap) throws Exception {
		
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		Gson gson = new Gson();
    	String jsonStr = (String)paramMap.get("gridData");
    	HashMap<String, Object>[] dataList = gson.fromJson(jsonStr, HashMap[].class);
    	
		String[] headers = gson.fromJson((String) paramMap.get("header"), String[].class);
		String[] headerTitles = gson.fromJson((String)paramMap.get("headerTitle"), String[].class);
    	
    	resultMap.put("headers", headers);
    	resultMap.put("headerTitles", headerTitles);
    	resultMap.put("datas", dataList);
    	resultMap.put("fileName", (String)paramMap.get("fileName"));
		ModelAndView modelAndView = new ModelAndView("excelView",resultMap);	
        return modelAndView;
    }	
	
	

}
