package kr.co.i4way.genesys.cfgserver;

import java.util.Collection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.genesyslab.platform.applicationblocks.com.ConfigException;
import com.genesyslab.platform.applicationblocks.com.IConfService;
import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentLogin;
import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentLoginInfo;
import com.genesyslab.platform.applicationblocks.com.objects.CfgPerson;
import com.genesyslab.platform.applicationblocks.com.queries.CfgAgentLoginQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgPersonQuery;
import com.genesyslab.platform.commons.collections.KeyValueCollection;
import com.genesyslab.platform.commons.collections.KeyValuePair;
import com.genesyslab.platform.configuration.protocol.types.CfgObjectState;

public class ConfigAgentLogin {
	private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
	public ConfigAgentLogin() {
		
	}

	/**
	 * employee_id에 해당하는 Person에 Assign된 AgentLogin정보를 조회한다.
	 * @param service
	 * @param tenant_dbid
	 * @param employee_id
	 * @return
	 */
	public Collection<CfgAgentLoginInfo> getAgentLoginInfo(final IConfService service, int tenant_dbid, String employee_id){
		CfgPerson tmpCfgPerson = null;
		Collection<CfgAgentLoginInfo> rtnAgentLoginInfo = null;
		CfgPersonQuery personquery = new CfgPersonQuery();
		personquery.setTenantDbid(tenant_dbid);
		personquery.setEmployeeId(employee_id);
		try {
			tmpCfgPerson = service.retrieveObject(CfgPerson.class, personquery);
			
			if(tmpCfgPerson != null) {
				rtnAgentLoginInfo = tmpCfgPerson.getAgentInfo().getAgentLogins();
			}
		}catch (ConfigException e) {
			e.printStackTrace();
		}
		
		return rtnAgentLoginInfo;
	}

	/**
	 * AgentLogin을 조회한다.(multi)
	 * @param service
	 * @param tenant_dbid
	 * @param switch_dbid
	 * @param assignable
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public Collection<CfgAgentLogin> getAgentLogins(
			final IConfService service, int tenant_dbid, int switch_dbid, String assignable)
					throws ConfigException, InterruptedException {
		// Read configuration objects:
		CfgAgentLoginQuery agentloginquery = new CfgAgentLoginQuery();
		agentloginquery.setTenantDbid(tenant_dbid);
		agentloginquery.setSwitchDbid(switch_dbid);
		if(assignable.equals("yes")) {
			agentloginquery.setNoPersonDbid(2);
		}
		
		Collection<CfgAgentLogin> agentlogin = null;
		agentlogin = service.retrieveMultipleObjects(CfgAgentLogin.class,
				agentloginquery);
		return agentlogin;
	}

	/**
	 * AgentLogin을 조회한다.
	 * @param service
	 * @param tenant_dbid
	 * @param switch_dbid
	 * @param login_code
	 * @return CfgAgentLogin
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentLogin getAgentLogin(
			final IConfService service, int tenant_dbid, int switch_dbid, String login_code
			)
					throws ConfigException, InterruptedException {
		// Read configuration objects:
		CfgAgentLogin agentlogin = null;
		CfgAgentLoginQuery agentloginquery = new CfgAgentLoginQuery();
		agentloginquery.setTenantDbid(tenant_dbid);
		agentloginquery.setSwitchDbid(switch_dbid);
		agentloginquery.setLoginCode(login_code);

		agentlogin = service.retrieveObject(CfgAgentLogin.class, agentloginquery);
		return agentlogin;
	}	

	/**
	 * AgentLogin을 조회한다.
	 * @param service
	 * @param tenant_dbid
	 * @param switch_dbid
	 * @param agentLogin_dbid
	 * @return CfgAgentLogin
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentLogin getAgentLogin(
			final IConfService service,
			int tenant_dbid,
			int switch_dbid,
			int agentLogin_dbid
			)
					throws ConfigException, InterruptedException {
		// Read configuration objects:
		CfgAgentLogin agentlogin = null;
		CfgAgentLoginQuery agentloginquery = new CfgAgentLoginQuery();
		agentloginquery.setTenantDbid(tenant_dbid);
		agentloginquery.setSwitchDbid(switch_dbid);
		agentloginquery.setDbid(agentLogin_dbid);

		agentlogin = service.retrieveObject(CfgAgentLogin.class, agentloginquery);
		return agentlogin;
	}	
	
	/**
	 * AgentLogin의 Assign여부를 확인한다.
	 * @param loginDBID
	 * @param service
	 * @return true : Assign 가능, false : 이미 Assign중.
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public boolean checkAssignYn(IConfService service, 
			int tenant_dbid,
			int switch_dbid,
			String login_code
			)
					throws ConfigException, InterruptedException {
		boolean rtnval = false;

		String warpuptm = "";

		CfgAgentLoginQuery loginquery = new CfgAgentLoginQuery();
		loginquery.setTenantDbid(tenant_dbid);
		loginquery.setSwitchDbid(switch_dbid);
		loginquery.setLoginCode(login_code);
		loginquery.setNoPersonDbid(2);
		
		CfgAgentLogin agentlogin = null;
		agentlogin = service.retrieveObject(CfgAgentLogin.class,loginquery);
		
		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();
		            	//logger.info("wrap-up-time=" + warpuptm);
		            }
		        }
			}
			if(warpuptm.equals("")){
				rtnval = true;
			}else{
				rtnval = false;
			}
		}
		return rtnval;
	}

	/**
	 * AgentLogin을 생성한다.
	 * @param service
	 * @param tenant_dbid
	 * @param switch_dbid
	 * @param login_code
	 * @param switch_folder_dbid
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentLogin createAgentLogin(
			final IConfService service,
			int tenant_dbid,
			int switch_dbid,
			String login_code
			)
					throws ConfigException, InterruptedException {
		// Read configuration objects:
		CfgAgentLogin agentlogin = new CfgAgentLogin(service);
		agentlogin.setTenantDBID(tenant_dbid);
		agentlogin.setSwitchDBID(switch_dbid);
		agentlogin.setLoginCode(login_code);
		agentlogin.setSwitchSpecificType(1);
		
		agentlogin.save();
		
		return agentlogin;
	}
	
	/**
	 * AgentLogin을 삭제한다.
	 * @param service
	 * @param tenant_dbid
	 * @param switch_dbid
	 * @param login_code
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public boolean deleteAgentLogins(
			final IConfService service,
			int tenant_dbid,
			int switch_dbid,
			String login_code
			)
					throws ConfigException, InterruptedException {
		// Read configuration objects:
		CfgAgentLogin agentlogin = null;
		boolean rtnbool = false;
		try{
			CfgAgentLoginQuery agentloginquery = new CfgAgentLoginQuery();
			agentloginquery.setTenantDbid(tenant_dbid);
			agentloginquery.setSwitchDbid(switch_dbid);
			agentloginquery.setLoginCode(login_code);

			agentlogin = service.retrieveObject(CfgAgentLogin.class,agentloginquery);
			agentlogin.delete();		
			rtnbool = true;
		}catch(Exception e){
			rtnbool = false;
		}
		return rtnbool;
	}	

}
