package com.eactive.eai.rms.bap.adaptor.socket;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

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.agent.command.CommonCommand;
import com.eactive.eai.rms.common.base.BapBaseAnnotationController;
import com.eactive.eai.rms.common.util.CamelCaseUtil;

@Controller
public class BapServerSocketController extends BapBaseAnnotationController  {

	@RequestMapping(value= "/bap/adapter/socket/serverStatus.view")
	public 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= "/bap/adapter/socket/serverStatus.json", params = "cmd=LIST")
	public ModelAndView selectList(HttpServletRequest request) throws Exception {
		
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.adapter.socket.SocketServerCommand","");
		Map<String, Object> returnMap = agentUtilService.broadcast(command);
		Iterator<String> iter = returnMap.keySet().iterator();
		List<Map<String, Serializable>> listAll = new ArrayList<Map<String, Serializable>>();
		while ( iter.hasNext() ) {
			String svrName = iter.next();
			try {
				@SuppressWarnings("unchecked")
				List<Map<String, Serializable>> list = (List<Map<String, Serializable>>)returnMap.get(svrName);
				listAll.addAll(list);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
		 
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("rows"   , listAll);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",resultMap);		
		return modelAndView;
		
	}

	@RequestMapping(value= "/bap/adapter/socket/serverStatus.json", params = "cmd=TRANSACTION")
	public ModelAndView control(HttpServletRequest request, String startStop, String group, String adapter) throws Exception {
		
		String[] args = new String[3];
		args[0] = startStop;
		args[1] = group;
		args[2] = adapter;
		CommonCommand command = new CommonCommand("com.eactive.eai.agent.adapter.socket.SocketStartStopCommand",args);
		HashMap<String, Object> resultMap = agentUtilService.broadcast(command);
		Iterator<String> iter = resultMap.keySet().iterator();
		String svrName = iter.next();
		String message = (String)resultMap.get(svrName);
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		returnMap.put("result"   , message);
		
		ModelAndView modelAndView = new ModelAndView("jsonView",returnMap);		
		return modelAndView;
	}
}