package kr.co.i4way.genesys.config;

import java.util.Collection;

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.queries.CfgAgentLoginQuery;
import com.genesyslab.platform.commons.collections.KeyValueCollection;
import com.genesyslab.platform.commons.collections.KeyValuePair;

public class AgentLogin {

	public AgentLogin() {
	}

	public Collection<CfgAgentLogin> SelectAgentLogins(
			final IConfService service) throws ConfigException,
			InterruptedException {
		// Read configuration objects:
		Collection<CfgAgentLogin> agentlogin = null; 
		agentlogin = service.retrieveMultipleObjects(CfgAgentLogin.class,
				new CfgAgentLoginQuery());
		return agentlogin;
	}

	public Collection<CfgAgentLogin> SelectAgentLogins(int iTenantDBID,
			String sLoginCode, final IConfService service)
			throws ConfigException, InterruptedException {
		// Read configuration objects:
		Collection<CfgAgentLogin> agentlogin = null;
		CfgAgentLoginQuery agentloginquery = new CfgAgentLoginQuery();
		agentloginquery.setTenantDbid(iTenantDBID);
		agentloginquery.setLoginCode(sLoginCode);

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

	public Collection<CfgAgentLogin> SelectAgentLogins(int iTenantDBID,
			int iAgentLoginDBID, final IConfService service)
			throws ConfigException, InterruptedException {
		// Read configuration objects:
		Collection<CfgAgentLogin> agentlogin = null;
		CfgAgentLoginQuery agentloginquery = new CfgAgentLoginQuery();
		agentloginquery.setTenantDbid(iTenantDBID);
		agentloginquery.setDbid(iAgentLoginDBID);

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

	public boolean checkAssignYn(int loginDBID, IConfService service)
			throws ConfigException, InterruptedException {
		boolean rtnval = false;

		String warpuptm = "";

		CfgAgentLoginQuery loginquery = new CfgAgentLoginQuery();
		loginquery.setDbid(loginDBID);
		loginquery.setNoPersonDbid(2);

		Collection<CfgAgentLogin> agentlogins = null;
		agentlogins = service.retrieveMultipleObjects(CfgAgentLogin.class,
				loginquery);

		if (agentlogins != null) {
			for (CfgAgentLogin agentlogin : agentlogins) {

				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("")) {
			rtnval = true;
		} else {
			rtnval = false;
		}

		return rtnval;
	}

	/**
	 * AgentLogin을 생성한다.
	 * 
	 * @param iTenantDBID
	 * @param iSwitchDBID
	 * @param sCode
	 * @param service
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentLogin createAgentLogin(int iTenantDBID, int iSwitchDBID,
			String sCode, final IConfService service) throws ConfigException,
			InterruptedException {
		// Read configuration objects:
		CfgAgentLogin agentlogin = new CfgAgentLogin(service);
		agentlogin.setTenantDBID(iTenantDBID);
		agentlogin.setSwitchDBID(iSwitchDBID);
		agentlogin.setLoginCode(sCode);
		agentlogin.setSwitchSpecificType(1);

		agentlogin.save();

		return agentlogin;
	}
}
