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.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.BizException;
import com.eactive.eai.rms.onl.common.exception.EMSRuntimeException;
import com.eactive.eai.rms.onl.common.util.CallAgentUtil;
import com.eactive.eai.rms.onl.common.util.DataSetUtil;
import com.eactive.eai.rms.onl.common.vo.HostInfomationForm;
import com.google.gson.Gson;


@Controller
public class SnaWcaStatusController extends BaseController{
	
	private final String AGENT_PAGE_STATUS		= "adapter_sna_status_agent.jsp";
	private final String AGENT_PAGE_STARTSTOP	= "adapter_startstop_agent.jsp";
	private final String AGENT_PAGE_BACKUP		= "adapter_sna_backup_change_agent.jsp";
	private final String ADAPTER_TYPE			= "SNA";
	
	@Autowired
	private EAIServerService eaiServerService;
	
	
	@RequestMapping(value= "/onl/adapter/sna/snaWcaStatusMan.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/snaWcaStatusMan.json", params = "cmd=LIST")
	private ModelAndView selectList(HttpServletRequest request,
			JsonPageVo pageVo ,@RequestParam HashMap<String,Object> paramMap
			) {
		
		
		List<EAIServer> serverList = eaiServerService.selectEaiServerIpList();
		if (serverList.isEmpty()) {
			throw new BizException("서버리스트가 없습니다.");
		}

		List<String> urls = new ArrayList<>();
		
		for (EAIServer serverMap : serverList)
		{
			String url = "http://" + serverMap.getEaisevrip() + ":"
				+ serverMap.getSevrlsnportname()
				+ HostInfomationForm.WEB_AGENT_CONTEXT_PATH
				+ AGENT_PAGE_STATUS + "?eaiSvrInstNm="
				+ serverMap.getEaisevrinstncname() + "&adapterType="
				+ ADAPTER_TYPE;
			urls.add(url);
			logger.info(url);
		}
		
		String searchAdapterGroup = (String)paramMap.get("searchAdapterGroup");
		String searchAdapter = (String)paramMap.get("searchAdapter");
		
		HashMap<String,String> filter = new HashMap<>();
		if (searchAdapterGroup !=null && !"".equals(searchAdapterGroup)){
			filter.put("AdapterGroup",searchAdapterGroup);
		}
		if (searchAdapter !=null && !"".equals(searchAdapter)){
			filter.put("Adapter",searchAdapter);
		}
		
		DataSetUtil gUtil = new DataSetUtil();
		gUtil.setFilter(filter);
		List<HashMap<String, Object>> list = gUtil.flushXmlToClient(urls);
		
		Map resultMap = new HashMap();
		resultMap.put("rows"   , list);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;
	}	
	
	@RequestMapping(value= "/onl/adapter/sna/snaWcaStatusMan.json", params = "cmd=TRANSACTION_STARTSTOP")
	private ModelAndView transactionStartStop(HttpServletRequest request,String eaiInstanceName, String adapterGroupNm,String adapterNm, String adapterStartStop) throws Exception {
		
	 	String result = null;
		
		EAIServer selectedServer = eaiServerService
				.getByEaiSevrInstncName(eaiInstanceName);

		// 호출할 Agent Page
		String url = "http://" + selectedServer.getEaisevrip() + ":"
				+ selectedServer.getSevrlsnportname()
			+ HostInfomationForm.WEB_AGENT_CONTEXT_PATH
			+ AGENT_PAGE_STARTSTOP
			+ "?adapterType=" + ADAPTER_TYPE
			+ "&adapterGroupNm=" + adapterGroupNm
			+ "&adapterNm=" + adapterNm
			+ "&adapterStartStop=" + adapterStartStop;

		logger.info(url);
		// Agent를 호출한다.
		CallAgentUtil agent = new CallAgentUtil();
		
		result = agent.getAgentDataToString(url);

		Map resultMap = new HashMap();
		resultMap.put("message", result); // 전체 페이지수

		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
		
	}	
	
	@RequestMapping(value= "/onl/adapter/sna/snaWcaStatusMan.json", params = "cmd=TRANSACTION_BACKUP")
	public ModelAndView transactionBackUp(HttpServletRequest request,String eaiInstanceName, String adapterGroupNm,String luGroupNm, String backupMode) throws Exception {
		
	 	String result = null;
		EAIServer selectedServer = eaiServerService
				.getByEaiSevrInstncName(eaiInstanceName);
		// 호출할 Agent Page
		String url = "http://" + selectedServer.getEaisevrip() + ":"
				+ selectedServer.getSevrlsnportname()
			+ HostInfomationForm.WEB_AGENT_CONTEXT_PATH
			+ AGENT_PAGE_BACKUP + "?"
			+ "adapterType=" + ADAPTER_TYPE
			+ "&adapterGroupNm=" + adapterGroupNm
			+ "&luGroupNm=" + luGroupNm
			+ "&backupMode=" + backupMode;

		logger.info(url);
		// Agent를 호출한다.
		CallAgentUtil agent = new CallAgentUtil();
		result = agent.getAgentDataToString(url);

		Map resultMap = new HashMap();
		resultMap.put("message", result); // 전체 페이지수

		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
		
	}	
	
	@RequestMapping(value= "/onl/adapter/sna/snaWcaStatusMan.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;
    }	

}
