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

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.apache.commons.httpclient.NameValuePair;
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.combo.ComboService;
import com.eactive.eai.rms.common.combo.ComboVo;
import com.eactive.eai.rms.common.login.SessionManager;
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.util.CallAgentUtil;
import com.eactive.eai.rms.onl.common.util.DataSetUtil;
import com.eactive.eai.rms.onl.common.vo.HostInfomationForm;

import edu.emory.mathcs.backport.java.util.Collections;

/**
 * <pre>
 * 1. 기능 : Socket 상태 정보
 * 2. 처리 개요 :
 *     -
 * 3. 주의사항 :
 * </pre>
 *
 * @author : vs.net
 * @version : $Revision: 1.3 $
 * @since : rts 1.0
 */
@Controller
public class SocketTrackingController extends BaseController{
	private final String AGENT_PAGE_STATUS    = "adapter_socket2_trace_agent.jsp";
	private final String AGENT_PAGE_STARTSTOP = "adapter_socket2_trace_level.jsp";

	
    @Autowired
    @Qualifier("comboService")
    private ComboService comboService;
    
    @Autowired
    private EAIServerService eaiServerService;
	
	@RequestMapping(value= "/onl/adapter/socket/socketTracking.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/socket/socketTracking.json", params = "cmd=LIST")
	private ModelAndView selectList(HttpServletRequest request,
			JsonPageVo pageVo ,String searchEaiSevrInstncName 
			) throws Exception {
		
		EAIServer selectedServer = eaiServerService.getByEaiSevrInstncName(searchEaiSevrInstncName);

		String url = "http://" + selectedServer.getEaisevrip() + ":"
					+ selectedServer.getSevrlsnportname()
					+ HostInfomationForm.WEB_AGENT_CONTEXT_PATH
					+ AGENT_PAGE_STATUS ;

		DataSetUtil gUtil = new DataSetUtil();
		
		List<HashMap<String, Object>> list = gUtil.flushXmlToClient(url);
		pageVo.setTotalCount(list.size());
		//어댑터 업무그룹별 정렬
		String[] compareParam = {"AdptrBzwkGroupName"};
		MapComparator comp = new MapComparator(compareParam);
		Collections.sort(list,comp);
		
		
		Map resultMap = new HashMap();
		resultMap.put("total"  , pageVo.getTotal());  //전체 페이지수
		resultMap.put("page"   , pageVo.getPage());   //현재 페이지수
		resultMap.put("records", pageVo.getTotalCount());//전체 레코드건수	
		resultMap.put("rows"   , list);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;
		
	}	
	@RequestMapping(value= "/onl/adapter/socket/socketTracking.json", params = "cmd=TRANSACTION_STARTSTOP")
	public ModelAndView transactionStartStop(HttpServletRequest request,
			String searchEaiSevrInstncName, String adptrBzwkGroupName,
			String adptrBzwkName, String level) throws Exception {
		
		EAIServer selectedServer = eaiServerService.getByEaiSevrInstncName(searchEaiSevrInstncName);
		String url = "http://" + selectedServer.getEaisevrip() + ":"
				+ selectedServer.getSevrlsnportname()
				+ HostInfomationForm.WEB_AGENT_CONTEXT_PATH
				+ AGENT_PAGE_STARTSTOP;

		NameValuePair[] postParameters = new NameValuePair[] {
				new NameValuePair("adapterGroupName", adptrBzwkGroupName ),
				new NameValuePair("adapterName"     , adptrBzwkName),
				new NameValuePair("level"           , level)
				};

		// Agent를 호출한다.
		CallAgentUtil util = new CallAgentUtil();
		String message = util.getAgentDataToString(url, postParameters);

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

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

	@RequestMapping(value = "/onl/adapter/socket/socketTracking.json", params = "cmd=LIST_INIT_COMBO")
	public ModelAndView initCombo(HttpServletRequest request, HttpServletResponse response) {
		List<ComboVo> instanceList = comboService.getFromEAIServerWithoutAll();

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

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

	

}

