package kr.co.i4way.genesys.cfgserver;

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

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.CfgAgentGroup;
import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentLogin;
import com.genesyslab.platform.applicationblocks.com.objects.CfgCallingList;
import com.genesyslab.platform.applicationblocks.com.objects.CfgCallingListInfo;
import com.genesyslab.platform.applicationblocks.com.objects.CfgCampaignGroup;
import com.genesyslab.platform.applicationblocks.com.objects.CfgFilter;
import com.genesyslab.platform.applicationblocks.com.objects.CfgGroup;
import com.genesyslab.platform.applicationblocks.com.objects.CfgPerson;
import com.genesyslab.platform.applicationblocks.com.objects.CfgTableAccess;
import com.genesyslab.platform.applicationblocks.com.objects.CfgTreatment;
import com.genesyslab.platform.applicationblocks.com.queries.CfgAgentGroupQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgCallingListQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgCampaignGroupQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgTableAccessQuery;
import com.genesyslab.platform.commons.collections.KeyValueCollection;
import com.genesyslab.platform.configuration.protocol.types.CfgDialMode;
import com.genesyslab.platform.configuration.protocol.types.CfgFlag;
import com.genesyslab.platform.configuration.protocol.types.CfgObjectType;
import com.genesyslab.platform.configuration.protocol.types.CfgOperationMode;
import com.genesyslab.platform.configuration.protocol.types.CfgOptimizationMethod;

public class ConfigCallingList {
	private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
	ConfigPerson ps = null;

	ConfigOB ob = null;
	public ConfigCallingList(){
	}
	
	/**
	 * CallingList을 조회한다. 
	 * @param service
	 * @param tenant_dbid
	 * @param callinglist_name
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgCallingList getCallingList(
			final IConfService service,
			int tenant_dbid,
			String callinglist_name
			) {
		CfgCallingList callinglist = null;
		try {
			CfgCallingListQuery callinglistquery = new CfgCallingListQuery();
			callinglistquery.setTenantDbid(tenant_dbid);
			callinglistquery.setName(callinglist_name);
			callinglist = service.retrieveObject(CfgCallingList.class,	callinglistquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return callinglist;
	}
	
	/**
	 * CallingList을 조회한다. 
	 * @param service
	 * @param tenant_dbid
	 * @param callinglist_dbid
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgCallingList getCallingList(
			final IConfService service,
			int tenant_dbid,
			int callinglist_dbid
			) {
		CfgCallingList callinglist = null;
		try {
			CfgCallingListQuery callinglistquery = new CfgCallingListQuery();
			callinglistquery.setTenantDbid(tenant_dbid);
			callinglistquery.setDbid(callinglist_dbid);
			callinglist = service.retrieveObject(CfgCallingList.class,	callinglistquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return callinglist;
	}

	/**
	 * CallingList을 조회한다.
	 * @param iTenantDBID
	 * @param service
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public Collection<CfgCallingList> getCallingList(
			final IConfService service,
			int tenant_dbid
			) {
		Collection<CfgCallingList> callinglist = null;
		try {
			CfgCallingListQuery callinglistquery = new CfgCallingListQuery();
			callinglistquery.setTenantDbid(tenant_dbid);
			callinglist = service.retrieveMultipleObjects(CfgCallingList.class,	callinglistquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}catch (InterruptedException e) {
			logger.error("InterruptedException", e);
		}
		return callinglist;
	}
	
	/**
	 * CallingList을 삭제한다.
	 * @param service
	 * @param tenant_dbid
	 * @param calinglist_name
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public int deleteCallingList(
			final IConfService service,
			int tenant_dbid,
			String calinglist_name
			)
					throws ConfigException, InterruptedException {
		// Read configuration objects:
		int returnval = 0;
		CfgCallingList callinglist = getCallingList(service, tenant_dbid, calinglist_name);
		if(callinglist != null) {
			callinglist.delete();
            returnval = 1;
		}
		
		return returnval;
	}
	

	/**
	 * CallingList을 수정한다.
	 * @param service
	 * @param tenant_dbid
	 * @param calinglist_name
	 * @param table_name
	 * @param filter_name
	 * @param maximum
	 * @param treatment_names
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgCallingList modifyCallingList(
			final IConfService service,
			int tenant_dbid,
			String calinglist_name,
			String table_name,
			String filter_name,
			int maximum,
			String treatment_names
			)
					throws ConfigException, InterruptedException {
		// Read configuration objects:
		String[] arr_treatment = treatment_names.split(",");
		Collection<CfgTreatment> treatments = new HashSet<CfgTreatment>();
		CfgCallingList callinglist = getCallingList(service, tenant_dbid, calinglist_name);
		ob = new ConfigOB();
		CfgTreatment tmptreatment = null;
		if(arr_treatment.length > 0) {
			for(int i=0; i<arr_treatment.length; i++) {
				tmptreatment = null;
				tmptreatment = ob.getTreatment(service, tenant_dbid, arr_treatment[i]);
				treatments.add(tmptreatment);
			}
		}
		CfgTableAccess ta = getTableAccess(service, tenant_dbid, table_name);
		CfgFilter filter = ob.getFilter(service, tenant_dbid, filter_name);
		callinglist.setTableAccess(ta);
		callinglist.setTreatments(treatments);
		//callinglist.setFilterDBID(filter.getDBID());
		callinglist.setMaxAttempts(maximum);
		callinglist.save();
		
		return callinglist;
	}
	

	/**
	 * CallingList을 생성한다.
	 * @param service
	 * @param tenant_dbid
	 * @param calinglist_name
	 * @param table_name
	 * @param filter_name
	 * @param maximum
	 * @param treatment_names
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgCallingList createCallingList(
			final IConfService service,
			int tenant_dbid,
			String calinglist_name,
			String table_name,
			String filter_name,
			int maximum,
			String treatment_names
			)
					throws ConfigException, InterruptedException {
		// Read configuration objects:
		String[] arr_treatment = treatment_names.split(",");
		Collection<CfgTreatment> treatments = new HashSet<CfgTreatment>();
		CfgCallingList callinglist = new CfgCallingList(service);
		callinglist.setTenantDBID(tenant_dbid);
		callinglist.setName(calinglist_name);
		ob = new ConfigOB();
		CfgTreatment tmptreatment = null;
		if(arr_treatment.length > 0) {
			for(int i=0; i<arr_treatment.length; i++) {
				tmptreatment = null;
				tmptreatment = ob.getTreatment(service, tenant_dbid, arr_treatment[i]);
				treatments.add(tmptreatment);
			}
		}
		callinglist.setName(calinglist_name);
		CfgTableAccess ta = getTableAccess(service, tenant_dbid, table_name);
		CfgFilter filter = ob.getFilter(service, tenant_dbid, filter_name);
		callinglist.setTableAccess(ta);
		callinglist.setTreatments(treatments);
		//callinglist.setFilterDBID(filter.getDBID());
		callinglist.setMaxAttempts(maximum);
		callinglist.save();
		
		return callinglist;
	}
	

	/**
	 * callinglustinfo Collection을 생성한다.
	 * @param service
	 * @param tenant_dbid
	 * @param cl	콜링리스트
	 * @return
	 */
	public Collection<CfgCallingListInfo> createCallingListInfo(final IConfService service,
			int tenant_dbid,
			CfgCallingList cl
			) {
		Collection<CfgCallingListInfo> cmp = null;
		CfgCallingListInfo newcli = null;

		try {
			newcli = new CfgCallingListInfo(service, null);
			cmp = new HashSet<CfgCallingListInfo>();
			newcli.setIsActive(CfgFlag.CFGTrue);
			newcli.setShare(10);
			newcli.setCallingList(cl);
			cmp.add(newcli);
		} catch (Exception e) {
			logger.error("ConfigException", e);
		}
		return cmp;
	}
	
	/**
	 * TableAccess을 조회한다. 
	 * @param service
	 * @param tenant_dbid
	 * @param ta_name
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgTableAccess getTableAccess(
			final IConfService service,
			int tenant_dbid,
			String ta_name
			) {
		CfgTableAccess tableaccess = null;
		try {
			CfgTableAccessQuery taquery = new CfgTableAccessQuery();
			taquery.setTenantDbid(tenant_dbid);
			taquery.setName(ta_name);
			tableaccess = service.retrieveObject(CfgTableAccess.class,	taquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return tableaccess;
	}
}
