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.PostMapping;
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.data.entity.onl.server.EAIServerService;
import com.eactive.eai.rms.onl.common.exception.EMSRuntimeException;
import com.eactive.eai.rms.onl.manage.sna.lugroup.LuGroupService;
import com.google.gson.Gson;


@Controller
public class SnaConnectionListController extends BaseController{

	
	@Autowired
	@Qualifier("snaConnectionListService")
	private SnaConnectionListService service;
	
	@Autowired
	@Qualifier("luGroupService")
	private LuGroupService groupService; 
	
	@Autowired
	private EAIServerService eaiServerService;

	@RequestMapping(value= "/onl/adapter/sna/snaConnectionListMan.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/snaConnectionListMan.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/snaConnectionListMan.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;            
	} 	
	
	@PostMapping(value= "/onl/adapter/sna/snaConnectionListMan.json", params = "cmd=LIST")
	public ModelAndView selectList(@RequestParam Map<String,Object> paramMap) {
		
		List<EAIServer> serverList = eaiServerService.selectEaiServerIpList();

		for (EAIServer eaiServer : serverList) {
			if (eaiServer.getHostname().equals(paramMap.get("hostName"))) {
				paramMap.put("eaiSvrIp", eaiServer.getEaisevrip());
			}
		}

		// SnaStatusCmd 설정
		String statusCmd = null;

		statusCmd = "SnaStatus -t " + paramMap.get("adptGrpNm") + " -gA " + paramMap.get("luGrpNm") + " -host "
				+ paramMap.get("eaiSvrIp");

		paramMap.put("snaStatusCmd", statusCmd);
		List list = service.executeSnaStatus(paramMap);

		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("rows", list);

		return new ModelAndView("jsonView", resultMap);
	}

	@RequestMapping(value = "/onl/adapter/sna/snaConnectionListMan.json", params = "cmd=TRANSACTION_CHANGE")
	public ModelAndView transactionBackUp(HttpServletRequest request, @RequestParam HashMap<String, Object> paramMap) {
		
		EAIServer eaiServer = eaiServerService
				.findFirstByHostname((String) paramMap.get("hostName"))
				.orElseThrow(() -> new EMSRuntimeException("HOST를 조회 할 수 없습니다."));

	 	// SnaStatusCmd 설정
	 	String statusCmd = null;
	 	
	 	String type = paramMap.get("type").toString();
	 	
		statusCmd = (type.startsWith("START_") ? "SnaStart -t "
				: "SnaStop -t ")
				+ paramMap.get("adptGrpNm") + " -g " + paramMap.get("luGrpNm")+ " ";

		if (type.endsWith("_LU")) {
			statusCmd += " -s " + request.getParameter("luNm") + " ";
		}
		statusCmd += "-host " + eaiServer.getEaisevrip();
	 	
		logger.info("controller statusCmd : " + statusCmd);
		paramMap.put("snaStatusCmd", statusCmd);
		List list = service.executeStartStop(paramMap);
		
		
		Map resultMap = new HashMap();
		resultMap.put("result", list); // 전체 페이지수

		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
		
	}	
	
	@RequestMapping(value= "/onl/adapter/sna/snaConnectionListMan.excel",params = "cmd=LIST_GRID_TO_EXCEL")
	public ModelAndView listGridToExcel(HttpServletRequest request, HttpServletResponse response, String gridData, String header, String headerTitle, String fileName, String merge) throws Exception {
		HashMap<String,Object> resultMap = new HashMap<String,Object>();

    	Gson gson = new Gson();
		HashMap<String, Object>[] list = gson.fromJson(gridData, HashMap[].class);
		
		String[] headers = gson.fromJson(header, String[].class);
		String[] headerTitles = gson.fromJson(headerTitle, String[].class);
		
		HashMap<String, Object>[] lmerge = null; 
		if (merge !=null){
			lmerge = gson.fromJson(merge, HashMap[].class);
		}
    	
    	resultMap.put("headers", headers);
    	resultMap.put("headerTitles", headerTitles);
    	resultMap.put("datas", list);
    	resultMap.put("fileName", fileName);
    	resultMap.put("merge", lmerge);
		ModelAndView modelAndView = new ModelAndView("excelView",resultMap);	
        
		return modelAndView;
    }	
	
}
