package kr.co.i4way.genesys.controller;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentLogin;
import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentLoginInfo;
import com.genesyslab.platform.commons.collections.KeyValueCollection;
import com.genesyslab.platform.commons.collections.KeyValuePair;
import com.genesyslab.platform.commons.protocol.ChannelState;

import kr.co.i4way.genesys.cfgserver.ConfigAgentLogin;
import kr.co.i4way.genesys.cfgserver.initConfigService;
import kr.co.i4way.genesys.model.GenesysInfoVo;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;

/**
 * AgentLogin용 Controller
 * @author jkhong
 *
 */
@Controller
@CrossOrigin(origins = "*")
public class AgentLoginController {
	@Autowired
	private GenesysInfoVo genesysinfovo;
	initConfigService initconfigservice = initConfigService.getInstance();
	ConfigAgentLogin cfgagentlogin = null;
	
	/**
	 * CfgPerson에 Assign되어있는 AgentLogin정보를 조회한다.
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/agentLogin/getPersonAgentLoginInfo")
	public JSONObject getPersonAgentLoginInfo(HttpServletRequest request) throws Exception{
		Collection<CfgAgentLoginInfo> agentlogininfo = null;
		JSONObject rtnobj = new JSONObject();
		//Map<String, Object> map = new HashMap<>(); 

		try {
			String employee_id = request.getParameter("employee_id") != null ? request.getParameter("employee_id") : "";
			if(initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgagentlogin = new ConfigAgentLogin();
				agentlogininfo = cfgagentlogin.getAgentLoginInfo(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), employee_id);
				rtnobj = convertAgentLoginInfos_jsonObj(agentlogininfo);
				//map = JsonUtil.getMapFromJsonObject(rtnobj);
			}
			rtnobj.put("command", "getPersonAgentLoginInfo");
			rtnobj.put("employee_id", employee_id);
		}catch(Exception e) {
		}finally {
			//initconfigservice.closeConfigService();
		}
		return rtnobj;
	}	
	
	/**
	 * 전체CfgAgentLogin 정보를 조회한다.
	 * @param request("assignable") : Assign여부 yes : assign된 Object
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/agentLogin/getAgentLogins")
	public JSONObject getAgentLogins(HttpServletRequest request) throws Exception{
		Collection<CfgAgentLogin> agentlogininfo = null;
		JSONObject rtnobj = new JSONObject();
		//Map<String, Object> map = new HashMap<>(); 

		try {
			String Assignable = request.getParameter("assignable") != null ? request.getParameter("assignable") : "";
			if(initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgagentlogin = new ConfigAgentLogin();
				agentlogininfo = cfgagentlogin.getAgentLogins(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), genesysinfovo.getCfg_switch_dbid(), Assignable);
				rtnobj = convertAgentLogins_jsonObj(agentlogininfo);
				//map = JsonUtil.getMapFromJsonObject(rtnobj);
			}		
			rtnobj.put("command", "getAgentLogins");
			rtnobj.put("assignable", Assignable);
		}catch(Exception e) {
		}finally {
			//initconfigservice.closeConfigService();
		}
		return rtnobj;
	}	
	
	/**
	 * CfgAgentLogin 정보를 조회한다.
	 * @param request("qry_type") : 쿼리타입 loginCode : loginCode로 쿼리, dbid : dbid로 쿼리
	 * @param request("qry_str") : Assign여부 yes : assign된 Object
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/agentLogin/getAgentLogin")
	public JSONObject getAgentLogin(HttpServletRequest request) throws Exception{
		CfgAgentLogin agentlogininfo = null;
		JSONObject rtnobj = new JSONObject();
		//Map<String, Object> map = new HashMap<>(); 

		try {
			String qry_type = request.getParameter("qry_type") != null ? request.getParameter("qry_type") : "";
			String qry_str = request.getParameter("qry_str") != null ? request.getParameter("qry_str") : "";
			if(initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgagentlogin = new ConfigAgentLogin();
				if(qry_type.equals("loginCode")) {
					agentlogininfo = cfgagentlogin.getAgentLogin(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), genesysinfovo.getCfg_switch_dbid(), qry_str);
				}else if(qry_type.equals("dbid")) {
					agentlogininfo = cfgagentlogin.getAgentLogin(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), genesysinfovo.getCfg_switch_dbid(), Integer.parseInt(qry_str));
				}
				rtnobj = convertAgentLogins_jsonObj(agentlogininfo);
				rtnobj.put("command", "getAgentLogin");
				rtnobj.put("qry_type", qry_type);
				rtnobj.put("qry_str", qry_str);
			}			
		}catch(Exception e) {
		}finally {
			//initconfigservice.closeConfigService();
		}
		return rtnobj;
	}	
	
	@ResponseBody
	@RequestMapping("/agentLogin/checkAgentLogin")
	public JSONObject checkAgentLogin(HttpServletRequest request) throws Exception{
		CfgAgentLogin agentlogininfo = null;
		JSONObject tmpobj = new JSONObject();
		JSONObject rtnobj = new JSONObject();
		//Map<String, Object> map = new HashMap<>(); 
		String warpuptm = "";

		try {
			String login_code = request.getParameter("login_code") != null ? request.getParameter("login_code") : "";
			if(initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgagentlogin = new ConfigAgentLogin();
				agentlogininfo = cfgagentlogin.getAgentLogin(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), genesysinfovo.getCfg_switch_dbid(), login_code);
				rtnobj.put("command", "checkAgentLogin");
				if(agentlogininfo != null){
					KeyValueCollection appOptions = agentlogininfo.getUserProperties();
						
					for(Object selectionObj : appOptions){
						KeyValuePair sectionKvp = (KeyValuePair) selectionObj;
						
				        for (Object recordObj : sectionKvp.getTKVValue()) {
				            KeyValuePair recordKvp = (KeyValuePair) recordObj;
				            if(recordKvp.getStringKey().equals("wrap-up-time")){
				            	warpuptm = recordKvp.getStringValue();
				            }
				        }
					}
					if(warpuptm.equals("")){
						rtnobj.put("result", false);
				  	}else{
				  		rtnobj.put("result", true);
				  	}
					rtnobj.put("loginCode", agentlogininfo.getLoginCode());
				}else {
					rtnobj.put("result", false);
					rtnobj.put("loginCode", null);
				}
				
			}			
		}catch(Exception e) {
		}finally {
			//initconfigservice.closeConfigService();
		}
		return rtnobj;
	}	
	
	@ResponseBody
	@RequestMapping("/agentLogin/createAgentLogin")
	public JSONObject createAgentLogin(HttpServletRequest request) throws Exception{
		CfgAgentLogin agentlogininfo = null;
		JSONObject rtnobj = new JSONObject();

		try {
			String login_code = request.getParameter("login_code") != null ? request.getParameter("login_code") : "";
			if(initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgagentlogin = new ConfigAgentLogin();
				agentlogininfo = cfgagentlogin.createAgentLogin(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), genesysinfovo.getCfg_switch_dbid(), login_code);
				rtnobj = convertAgentLogins_jsonObj(agentlogininfo);				
			}			
			rtnobj.put("command", "checkAgentLogin");
			rtnobj.put("login_code", login_code);
		}catch(Exception e) {
		}finally {
			//initconfigservice.closeConfigService();
		}
		return rtnobj;
	}	
	
	@ResponseBody
	@RequestMapping("/agentLogin/deleteAgentLogin")
	public JSONObject deleteAgentLogin(HttpServletRequest request) throws Exception{
		JSONObject rtnobj = new JSONObject();
		boolean result = false;
		try {
			String login_code = request.getParameter("login_code") != null ? request.getParameter("login_code") : "";
			if(initconfigservice.openConfigService(genesysinfovo) == ChannelState.Opened) {
				cfgagentlogin = new ConfigAgentLogin();
				result = cfgagentlogin.deleteAgentLogins(initconfigservice.service, genesysinfovo.getCfg_tenant_dbid(), genesysinfovo.getCfg_switch_dbid(), login_code);
				rtnobj.put("result", result);
				rtnobj.put("loginCode", login_code);
				rtnobj.put("command", "deleteAgentLogin");
			}			
		}catch(Exception e) {
		}finally {
			//initconfigservice.closeConfigService();
		}
		return rtnobj;
	}	
	
	private JSONObject convertAgentLogins_jsonObj(Collection<CfgAgentLogin> agentlogins) {
		JSONObject jsonObj = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		JSONObject finalJsonObj = new JSONObject();      
		String warpuptm = "";
		
		if(agentlogins != null) {
			for (CfgAgentLogin agentlogin : agentlogins) {
				warpuptm = "";
				jsonObj = new JSONObject();
				jsonObj.put("dbid", agentlogin.getDBID());
				jsonObj.put("loginCode", agentlogin.getLoginCode());
				if(agentlogin != null){
					KeyValueCollection appOptions = agentlogin.getUserProperties();
						
					for(Object selectionObj : appOptions){
						KeyValuePair sectionKvp = (KeyValuePair) selectionObj;
						
				        for (Object recordObj : sectionKvp.getTKVValue()) {
				            KeyValuePair recordKvp = (KeyValuePair) recordObj;
				            if(recordKvp.getStringKey().equals("wrap-up-time")){
				            	warpuptm = recordKvp.getStringValue();
				            }
				        }
					}
				}
				
				if(warpuptm.equals("")){
					jsonObj.put("isAssign", false);
			  	 }else{
			  		jsonObj.put("isAssign", true);
			  	 }
				jsonArray.add(jsonObj);
			}
			finalJsonObj.put("agentLogins", jsonArray);
		}else {
			finalJsonObj.put("agentLogins", null);
		}
		return finalJsonObj;
	}
	
	private JSONObject convertAgentLogins_jsonObj(CfgAgentLogin agentlogin) {
		JSONObject jsonObj = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		JSONObject finalJsonObj = new JSONObject();      
		String warpuptm = "";
		
		if(agentlogin != null) {
			jsonObj = new JSONObject();
			jsonObj.put("dbid", agentlogin.getDBID());
			jsonObj.put("loginCode", agentlogin.getLoginCode());
			if(agentlogin != null){
				KeyValueCollection appOptions = agentlogin.getUserProperties();
					
				for(Object selectionObj : appOptions){
					KeyValuePair sectionKvp = (KeyValuePair) selectionObj;
					
			        for (Object recordObj : sectionKvp.getTKVValue()) {
			            KeyValuePair recordKvp = (KeyValuePair) recordObj;
			            if(recordKvp.getStringKey().equals("wrap-up-time")){
			            	warpuptm = recordKvp.getStringValue();
			            }
			        }
				}
			}
			
			if(warpuptm.equals("")){
				jsonObj.put("isAssign", false);
		  	 }else{
		  		jsonObj.put("isAssign", true);
		  	 }
			jsonArray.add(jsonObj);
			finalJsonObj.put("agentLogins", jsonArray);
		}else {
			finalJsonObj.put("agentLogins", null);
		}
		return finalJsonObj;
	}
	
	private JSONObject convertAgentLoginInfos_jsonObj(Collection<CfgAgentLoginInfo> agentlogininfos) {
		JSONObject jsonObj = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		JSONObject finalJsonObj = new JSONObject();      
		
		if(agentlogininfos != null) {
			for (CfgAgentLoginInfo agentlogininfo : agentlogininfos) {
				jsonObj = new JSONObject();
				jsonObj.put("dbid", agentlogininfo.getAgentLogin().getDBID());
				jsonObj.put("loginCode", agentlogininfo.getAgentLogin().getLoginCode());
				jsonArray.add(jsonObj);
			}
			finalJsonObj.put("agentLogins", jsonArray);
		}else {
			finalJsonObj.put("agentLogins", null);
		}
		return finalJsonObj;
	}

}
