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.CfgPerson;
import com.genesyslab.platform.applicationblocks.com.objects.CfgSkill;
import com.genesyslab.platform.applicationblocks.com.objects.CfgSkillLevel;
import com.genesyslab.platform.applicationblocks.com.objects.CfgTreatment;
import com.genesyslab.platform.applicationblocks.com.queries.CfgCampaignQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgPersonQuery;
import com.genesyslab.platform.applicationblocks.com.queries.CfgTreatmentQuery;
import com.genesyslab.platform.configuration.protocol.types.CfgFlag;
import com.genesyslab.platform.configuration.protocol.types.CfgObjectState;

public class ConfigCampaign {
	private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
	private ConfigCampaignGroup configcampaigngroup = null;
	public ConfigCampaign() {
		
	}
	
	public Collection<CfgCampaign> getCampaignInfo(final IConfService service, int tenant_dbid) {
		Collection<CfgCampaign> rtnCampaign = null;
		CfgCampaignQuery campaignquery = new CfgCampaignQuery();
		campaignquery.setTenantDbid(tenant_dbid);
		try {
			rtnCampaign = service.retrieveMultipleObjects(CfgCampaign.class, campaignquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		} catch (InterruptedException e) {
			logger.error("InterruptedException", e);
		}
		return rtnCampaign;
	}
	
	public CfgCampaign getCampaignInfo(final IConfService service, int tenant_dbid, String campaign_name) {
		CfgCampaign rtnCampaign = null;
		CfgCampaignQuery campaignquery = new CfgCampaignQuery();
		campaignquery.setTenantDbid(tenant_dbid);
		campaignquery.setName(campaign_name);
		try {
			rtnCampaign = service.retrieveObject(CfgCampaign.class, campaignquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		} 
		return rtnCampaign;
	}
	
	public CfgCampaign getCampaignInfo(final IConfService service, int tenant_dbid, int campaign_dbid) {
		CfgCampaign rtnCampaign = null;
		CfgCampaignQuery campaignquery = new CfgCampaignQuery();
		campaignquery.setTenantDbid(tenant_dbid);
		campaignquery.setDbid(campaign_dbid);
		try {
			rtnCampaign = service.retrieveObject(CfgCampaign.class, campaignquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		} 
		return rtnCampaign;
	}
	
	/**
	 * Campaign을 생성한다.
	 * @param service
	 * @param campaign_name
	 * @param group_name
	 * @param callinglist_name
	 * @param vdn
	 * @return
	 */
	public CfgCampaign createCampaign(
			final IConfService service, 
			int tenant_dbid,
			String campaign_name, 
			Collection<CfgCallingListInfo> callinglist) {
		CfgCampaign campaign = null;
		Collection<CfgCampaignGroupInfo> campaigngroups = null;
		configcampaigngroup = new ConfigCampaignGroup();
		try{
			// Read configuration objects:
			campaign = new CfgCampaign(service);
			campaign.setTenantDBID(tenant_dbid);
			campaign.setName(campaign_name);
			campaign.setState(CfgObjectState.CFGEnabled);
			campaign.setCallingLists(callinglist);
			campaign.save();
		}catch (Exception e) {
			e.printStackTrace();
		}	
		return campaign;
	}
	
	/**
	 * campaign을 수정한다.
	 * @param service
	 * @param tenant_dbid
	 * @param campaign_dbid
	 * @param campaign_name
	 * @param callinglist
	 * @return
	 */
	public CfgCampaign modifyCampaign(
			final IConfService service, 
			int tenant_dbid,
			int campaign_dbid,
			String campaign_name, 
			String campaign_state,
			Collection<CfgCallingListInfo> callinglist){
		// Read configuration objects:
		CfgCampaign campaign = null;
		CfgCampaignQuery campaignquery = new CfgCampaignQuery();
		try {
			campaignquery.setTenantDbid(tenant_dbid);
			campaignquery.setDbid(campaign_dbid);
			campaign = service.retrieveObject(CfgCampaign.class,campaignquery);
			campaign.setCallingLists(callinglist);
			campaign.setState(CfgObjectState.valueOf(campaign_state));
			campaign.save();
		}catch (ConfigException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return campaign;
	}

	/**
	 * campaign을 삭제한다.
	 * @param service
	 * @param tenant_dbid
	 * @param campaign_dbid
	 * @return
	 */
	public boolean deleteCampaign(
			final IConfService service, 
			int tenant_dbid,
			int campaign_dbid){
		// Read configuration objects:
		CfgCampaign campaign = null;
		CfgCampaignQuery campaignquery = new CfgCampaignQuery();
		boolean rtnbool = false;
		try {
			campaignquery.setTenantDbid(tenant_dbid);
			campaignquery.setDbid(campaign_dbid);
			campaign = service.retrieveObject(CfgCampaign.class,campaignquery);
			campaign.delete();
			rtnbool = true;
		}catch (ConfigException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rtnbool;
	}
	
	public Collection<CfgTreatment> getTreatments(final IConfService service, int tenant_dbid) {
		Collection<CfgTreatment> treatment = null;
		CfgTreatmentQuery treatmentquery = new CfgTreatmentQuery();
		treatmentquery.setTenantDbid(tenant_dbid);
		try {
			treatment = service.retrieveMultipleObjects(CfgTreatment.class, treatmentquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		} catch (InterruptedException e) {
			logger.error("InterruptedException", e);
		}
		return treatment;
	}
	
	public CfgTreatment getTreatment(final IConfService service, int tenant_dbid, String treatment_name) {
		CfgTreatment treatment = null;
		CfgTreatmentQuery treatmentquery = new CfgTreatmentQuery();
		treatmentquery.setTenantDbid(tenant_dbid);
		treatmentquery.setName(treatment_name);
		try {
			treatment = service.retrieveObject(CfgTreatment.class, treatmentquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		} 
		return treatment;
	}
}
