package kr.co.i4way.genesys.cfgserver;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

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.CfgAgentInfo;
import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentLogin;
import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentLoginInfo;
import com.genesyslab.platform.applicationblocks.com.objects.CfgCallingList;
import com.genesyslab.platform.applicationblocks.com.objects.CfgCallingListInfo;
import com.genesyslab.platform.applicationblocks.com.objects.CfgCampaign;
import com.genesyslab.platform.applicationblocks.com.objects.CfgCampaignGroup;
import com.genesyslab.platform.applicationblocks.com.objects.CfgCampaignGroupInfo;
import com.genesyslab.platform.applicationblocks.com.objects.CfgDN;
import com.genesyslab.platform.applicationblocks.com.objects.CfgDNGroup;
import com.genesyslab.platform.applicationblocks.com.objects.CfgFolder;
import com.genesyslab.platform.applicationblocks.com.objects.CfgGroup;
import com.genesyslab.platform.applicationblocks.com.objects.CfgPerson;
import com.genesyslab.platform.applicationblocks.com.objects.CfgSkill;
import com.genesyslab.platform.applicationblocks.com.objects.CfgSkillLevel;
import com.genesyslab.platform.applicationblocks.com.objects.CfgSwitch;
import com.genesyslab.platform.applicationblocks.com.objects.CfgTransaction;
import com.genesyslab.platform.applicationblocks.com.queries.CfgAgentLoginQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgCampaignQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgDNGroupQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgDNQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgFolderQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgPersonQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgSwitchQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgTransactionQuery;
import com.genesyslab.platform.configuration.protocol.metadata.CfgTypeMask;
import com.genesyslab.platform.configuration.protocol.types.CfgDNGroupType;
import com.genesyslab.platform.configuration.protocol.types.CfgDNType;
import com.genesyslab.platform.configuration.protocol.types.CfgFlag;
import com.genesyslab.platform.configuration.protocol.types.CfgObjectState;
import com.genesyslab.platform.configuration.protocol.types.CfgRouteType;

public class ConfigFolder {
	private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
	public ConfigFolder() {
		
	}
		
	/**
	 * CfgFolder을 쿼리한다.
	 * @param service
	 * @param owner_type
	 * @param folder_dbid
	 * @return
	 */
	public CfgFolder getFolderInfo(final IConfService service, int owner_type, int folder_dbid) {
		CfgFolder rtnfolder = null;
		CfgFolderQuery folderquery = new CfgFolderQuery();
		folderquery.setOwnerType(owner_type);
		folderquery.setDbid(folder_dbid);
		try {
			rtnfolder = service.retrieveObject(CfgFolder.class, folderquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return rtnfolder;
	}
		
	/**
	 * DN을 생성한다.
	 * @param service
	 * @param tenant_dbid
	 * @param switch_dbid
	 * @param number
	 * @param dn_type
	 * @param route_type
	 * @param switch_spec_type
	 * @return
	 */
	public CfgDN createDN(
			final IConfService service,
			int tenant_dbid,
			int switch_dbid,
			String number,
			String dn_type,
			String route_type,
			int switch_spec_type
			) {
		// Read configuration objects:
		CfgDN new_dn = new CfgDN(service);
		try {
			new_dn.setTenantDBID(tenant_dbid);
			new_dn.setNumber(number);
			new_dn.setSwitchDBID(switch_dbid);
			new_dn.setState(CfgObjectState.CFGEnabled);
			new_dn.setType(CfgDNType.valueOf(dn_type));
			new_dn.setRouteType(CfgRouteType.valueOf(route_type));
			new_dn.setSwitchSpecificType(switch_spec_type);
			new_dn.save();
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		
		return new_dn;
	}
	
	/**
	 * AgentLogin을 삭제한다.
	 * @param service
	 * @param tenant_dbid
	 * @param switch_dbid
	 * @param login_code
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public boolean deleteDN(
			final IConfService service,
			int tenant_dbid,
			int switch_dbid,
			String dn_name
			) {
		CfgDN rtndn = null;
		boolean rtnval = false;
		CfgDNQuery dnquery = new CfgDNQuery();
		dnquery.setTenantDbid(tenant_dbid);
		dnquery.setSwitchDbid(switch_dbid);
		dnquery.setDnNumber(dn_name);
		try {
			rtndn = service.retrieveObject(CfgDN.class, dnquery);
			if(rtndn != null) {
				rtndn.delete();
				rtnval = true;
			}
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return rtnval;
	}	
	

	/**
	 * AgentLogin을 삭제한다.
	 * @param service
	 * @param tenant_dbid
	 * @param switch_dbid
	 * @param login_code
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public boolean deleteDN(
			final IConfService service,
			int tenant_dbid,
			int switch_dbid,
			String dn_name,
			String dn_type
			) {
		CfgDN rtndn = null;
		boolean rtnval = false;
		CfgDNQuery dnquery = new CfgDNQuery();
		dnquery.setTenantDbid(tenant_dbid);
		dnquery.setDnNumber(dn_name);
		dnquery.setDnType(CfgDNType.valueOf(dn_type));
		try {
			rtndn = service.retrieveObject(CfgDN.class, dnquery);
			if(rtndn != null) {
				rtndn.delete();
				rtnval = false;
			}
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return rtnval;
	}	

	
	
	/**
	 * CfgDNGroup을 쿼리한다.
	 * @param service
	 * @param tenant_dbid
	 * @return
	 */
	public Collection<CfgDNGroup> getDnGroup(final IConfService service, int tenant_dbid) {
		Collection<CfgDNGroup> rtndn = null;
		CfgDNGroupQuery dnquery = new CfgDNGroupQuery();
		dnquery.setTenantDbid(tenant_dbid);
		try {
			rtndn = service.retrieveMultipleObjects(CfgDNGroup.class, dnquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		} catch (InterruptedException e) {
			logger.error("InterruptedException", e);
		}
		return rtndn;
	}
	
	/**
	 * CfgDNGroup을 쿼리한다.
	 * @param service
	 * @param tenant_dbid
	 * @param gorup_name
	 * @return
	 */
	public CfgDNGroup getDnGroup(final IConfService service, int tenant_dbid, String gorup_name) {
		CfgDNGroup rtndn = null;
		CfgDNGroupQuery dnquery = new CfgDNGroupQuery();
		dnquery.setTenantDbid(tenant_dbid);
		dnquery.setName(gorup_name);
		try {
			rtndn = service.retrieveObject(CfgDNGroup.class, dnquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return rtndn;
	}
		
	/**
	 * CfgDNGroup을 쿼리한다.
	 * @param service
	 * @param tenant_dbid
	 * @param group_dbid
	 * @return
	 */
	public CfgDNGroup getDnGroup(final IConfService service, int tenant_dbid, int group_dbid) {
		CfgDNGroup rtndn = null;
		CfgDNGroupQuery dnquery = new CfgDNGroupQuery();
		dnquery.setTenantDbid(tenant_dbid);
		dnquery.setDbid(group_dbid);
		try {
			rtndn = service.retrieveObject(CfgDNGroup.class, dnquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return rtndn;
	}
	
	/**
	 * CfgDNGroup을 생성한다.
	 * @param service
	 * @param tenant_dbid
	 * @param gorup_name
	 * @param dngroup_type
	 * @return
	 */
	public CfgDNGroup createDNGroup(
			final IConfService service,
			int tenant_dbid,
			String gorup_name,
			String dngroup_type
			) {
		// Read configuration objects:
		CfgDNGroup new_group = new CfgDNGroup(service);
		try {
			CfgGroup groupinfo = new CfgGroup(service, null);
			groupinfo.setName(gorup_name);
			groupinfo.setTenantDBID(tenant_dbid);
			new_group.setGroupInfo(groupinfo);
			new_group.setType(CfgDNGroupType.valueOf(dngroup_type));
			new_group.save();
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}		
		return new_group;
	}
	
	/**
	 * CfgDNGroup을 삭제한다.
	 * @param service
	 * @param tenant_dbid
	 * @param gorup_name
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public boolean deleteDNGroup(
			final IConfService service,
			int tenant_dbid,
			String gorup_name
			) {
		CfgDNGroup group = null;
		boolean rtnval = false;
		
		try {
			group = getDnGroup(service, tenant_dbid, gorup_name);
			if(group != null) {
				group.delete();
				rtnval = true;
			}
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return rtnval;
	}	
}
