<%@ include file="../../EgTrackerAJSON.jsp" %>
<%@ page import = "com.eg.*"%>
<%@ page import = "egurkha.*" %>
<%@ page import = "com.egurkha.*"%>
<%@ page import = "com.eg.supermanager.*"%>
<%@ page import = "com.egurkha.util.*"%>
<%@ page import = "java.util.*" %>
<%@ page import = "net.sf.json.JSONObject"%>
<%@ page import = "net.sf.json.JSONArray"%>
<%@ taglib prefix="egui" uri="../../../WEB-INF/eg-taglib.tld" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%
	response.setHeader("Cache-Control","no-store");

	String milliSec ="";
	String tempStr = request.getParameter("agentIP");
	String agentIP = "";
	if(tempStr!=null)
		agentIP = EgURLDecoder.decode(tempStr,"UTF-8");
	String agentMode = request.getParameter("agentMode");

	//Remote control changes - preconfigured commands
	String timeOut = null;
	String otherCmdsEnabledUsrs = null;
	String[] validUsers = null;
	IniFile iniControls = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_controls.ini", true);
	IniFile iniUi = new IniFile(EgInstallInfo.getInstallDir() + "/manager/config/eg_ui.ini", true);
	String mode = "";
	String agentOS = null;
	if(EgInstallInfo.isSuperManager)
	{
		Hashtable paramsHt = new Hashtable();
		paramsHt.put("operation","getOS");
		paramsHt.put("agentID",agentIP);
		ArrayList managerIDs = EgElementInfo.getInstance().getManagerIDs("component",agentIP);
		if(managerIDs != null && !managerIDs.isEmpty())
		{
			String controlModeOnManager = (String)managerIDs.get(0);
			paramsHt.put("managerId",controlModeOnManager);
			SMRemoteControlHelper remoteControlFromSMHelper = new SMRemoteControlHelper();
			Hashtable ht = (Hashtable)remoteControlFromSMHelper.getResult(paramsHt);
			if(ht != null && ht.containsKey("agentOS"))
			{
				agentOS = (String)ht.get("agentOS");
			}
		}
	}
	else
	{
		AgentConfig agentconfig =  AgentConfig.getAgentConfig();
		if(agentIP != null){
			agentOS = agentconfig.getAgentOS(agentIP.trim());
		}
	}
	timeOut = iniControls.getValue("CONTROL_DEFAULTS","TimeOut");

	JSONObject json = new JSONObject();
	JSONObject pageParams = new JSONObject();
	StringBuffer sbHiddenParams = new StringBuffer();


	pageParams.put("selectedAgent",agentIP);
	String agentMode1 = agentMode;
	agentMode1 = StringUtils.replace(agentMode1,'_',' ');
	pageParams.put("selectedAgentMode",agentMode1);

	sbHiddenParams.append("&agentMode="+agentMode);
	sbHiddenParams.append("&$_agentMode="+agentMode);
	sbHiddenParams.append("&txtUserName=");
	sbHiddenParams.append("&txtPassWord=");

	String subject = null;
	String[] variables = null;
	String value = null;
	String keyword = "_COMMANDS";
	if(agentOS != null){
		if(agentOS.toLowerCase().indexOf("window") != -1){
			subject = "WINDOWS"+keyword;
		}else{
			subject = agentOS.toUpperCase()+keyword;
		}
		if(subject != null){
			variables = iniUi.getVariables(subject);
		}
	}

	JSONArray commandOpts = new JSONArray();
	if(variables != null){
		JSONObject jOpt = new JSONObject();
		jOpt.put("displayField",_mapping.getTextDescription("monitor.remotecontrol.Please select a command"));
		jOpt.put("valueField","-1");
		commandOpts.add(jOpt);
		for(int inc=0; inc<variables.length;inc++){
			value = iniUi.getValue(subject,variables[inc]);
			value = StringUtils.replace(value,"'","&&&");
			if(value != null && !value.equals("")){
				jOpt = new JSONObject();
				jOpt.put("displayField",variables[inc]);
				jOpt.put("valueField",value);
				commandOpts.add(jOpt);
			}
		}
	}

	if(_user.getRCExecuteAllCommands())
	{
		JSONObject jOpt = new JSONObject();
		jOpt.put("displayField",_mapping.getTextDescription("monitor.remotecontrol.Other"));
		jOpt.put("valueField","-2");
		commandOpts.add(jOpt);
	}

	if(commandOpts!=null && commandOpts.isEmpty())
	{
		JSONObject jOpt = new JSONObject();
		jOpt.put("displayField",_mapping.getTextDescription("monitor.remotecontrol.Not Applicable"));
		jOpt.put("valueField","-1");
		commandOpts.add(jOpt);
	}
	pageParams.put("selectedCmd","-1");
	json.put("commandOpts",commandOpts);
	pageParams.put("arguments","none");
	synchronized(milliSec)
	{
		milliSec = System.currentTimeMillis() + "";
		Thread.currentThread().sleep(1);
	}
	sbHiddenParams.append("&txtTimeStamp="+String.valueOf(System.currentTimeMillis()));

	JSONArray yesNoOpts = new JSONArray();

	JSONObject yesOpt = new JSONObject();
	yesOpt.put("displayField",_mapping.getTextDescription("monitor.remotecontrol.Yes"));
	yesOpt.put("valueField","Yes");

	yesNoOpts.add(yesOpt);

	JSONObject noOpt = new JSONObject();
	noOpt.put("displayField",_mapping.getTextDescription("monitor.remotecontrol.No"));
	noOpt.put("valueField","No");

	yesNoOpts.add(noOpt);

	json.put("yesNoOpts",yesNoOpts);
	pageParams.put("outputRequired","Yes");

	if(timeOut != null && timeOut.equals(""))
		timeOut = "60";

	pageParams.put("selectedTimeOutPeriod",String.valueOf(timeOut));

	if(sbHiddenParams!=null)
	{
		json.put("hiddenQStr",sbHiddenParams.toString());
	}
	json.put("pageParams",pageParams);
	json.put("agentOS",agentOS);
	
	response.getWriter().write(json.toString());
%>




