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.CfgGroup;
import com.genesyslab.platform.applicationblocks.com.objects.CfgPerson;
import com.genesyslab.platform.applicationblocks.com.objects.CfgTreatment;
import com.genesyslab.platform.applicationblocks.com.queries.CfgAgentGroupQuery;
import com.genesyslab.platform.commons.collections.KeyValueCollection;

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

	public ConfigAgentGroup(){
	}
	
	/**
	 * AgentGroup을 조회한다. 
	 * @param service
	 * @param tenant_dbid
	 * @param group_name
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentGroup getAgentGroupInfo(
			final IConfService service,
			int tenant_dbid,
			String group_name
			) {
		CfgAgentGroup agentgroup = null;
		try {
			CfgAgentGroupQuery agentgroupquery = new CfgAgentGroupQuery();
			agentgroupquery.setTenantDbid(tenant_dbid);
			agentgroupquery.setName(group_name);
			agentgroup = service.retrieveObject(CfgAgentGroup.class,	agentgroupquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return agentgroup;
	}
	
	/**
	 * AgentGroup을 조회한다. 
	 * @param service
	 * @param tenant_dbid
	 * @param group_dbid
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentGroup getAgentGroupInfo(
			final IConfService service,
			int tenant_dbid,
			int group_dbid
			) {
		CfgAgentGroup agentgroup = null;
		try {
			CfgAgentGroupQuery agentgroupquery = new CfgAgentGroupQuery();
			agentgroupquery.setTenantDbid(tenant_dbid);
			agentgroupquery.setDbid(group_dbid);
			agentgroup = service.retrieveObject(CfgAgentGroup.class,	agentgroupquery);
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		
		return agentgroup;
	}

	/**
	 * AgentGroup을 조회한다.
	 * @param iTenantDBID
	 * @param service
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public Collection<CfgAgentGroup> getAgentGroupInfo(
			final IConfService service,
			int tenant_dbid
			) {
		Collection<CfgAgentGroup> agentgroups = null;
		try {
			CfgAgentGroupQuery agentgroupquery = new CfgAgentGroupQuery();
			agentgroupquery.setTenantDbid(tenant_dbid);
			agentgroups = service.retrieveMultipleObjects(CfgAgentGroup.class,
					agentgroupquery);

		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}catch (InterruptedException e) {
			logger.error("InterruptedException", e);
		}
		return agentgroups;
	}
	
	/**
	 * AgentGroup에 Assign 조회한다.
	 * @param service
	 * @param tenant_dbid
	 * @param person_dbid
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public Collection<CfgAgentGroup> getAssignAgentGroup(
			final IConfService service,
			int tenant_dbid,
			int person_dbid
			) {
		Collection<CfgAgentGroup> agentgroups = null;
		try {
			CfgAgentGroupQuery agentgroupquery = new CfgAgentGroupQuery();
			agentgroupquery.setTenantDbid(tenant_dbid);
			agentgroupquery.setPersonDbid(person_dbid);

			agentgroups = service.retrieveMultipleObjects(CfgAgentGroup.class,
					agentgroupquery);

		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}catch (InterruptedException e) {
			logger.error("InterruptedException", e);
		}
		return agentgroups;
	}
	
	/**
	 * AgentGroup을 생성한다.
	 * @param service
	 * @param tenant_dbid
	 * @param group_name
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentGroup createAgentGroup(
			final IConfService service,
			int tenant_dbid,
			String group_name) {
		CfgGroup group = new CfgGroup(service, null);
		CfgAgentGroup cfggroup = null;
		KeyValueCollection kvc = new KeyValueCollection();		
		
		try {
			group.setTenantDBID(tenant_dbid);
			group.setName(group_name);
			kvc.addString("app", "true");
			group.getUserProperties().addList("app", kvc);
			//group.setUserProperties(kvc);

			cfggroup = new CfgAgentGroup(service);
			cfggroup.setGroupInfo(group);
			
			//cfggroup.getGroupInfo().setUserProperties(kvc);
			//KeyValuePair kv = new KeyValuePair("app", "true");
			//cfggroup.setProperty("app", "true");
			
			cfggroup.save();
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return cfggroup;
	}
	
	/**
	 * AgentGroup을 수정한다
	 * @param service
	 * @param tenant_dbid
	 * @param group_dbid
	 * @param group_name
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentGroup modifyAgentGroup(
			final IConfService service,
			int tenant_dbid,
			int group_dbid,
			String group_name) {
		// Read configuration objects:
		CfgAgentGroup agentgroup = null;
		try {
			CfgAgentGroupQuery agentgroupquery = new CfgAgentGroupQuery();
			agentgroupquery.setTenantDbid(tenant_dbid);
			agentgroupquery.setDbid(group_dbid);
			agentgroup = service.retrieveObject(CfgAgentGroup.class,agentgroupquery);

			if(agentgroup != null){
				agentgroup.getGroupInfo().setName(group_name);
				agentgroup.save();
			}
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		
		return agentgroup;
	}
	

	/**
	 * AgentGroup을 삭제한다.
	 * @param service
	 * @param tenant_dbid
	 * @param group_dbid
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public Boolean deleteAgentGroup(
			final IConfService service,
			int tenant_dbid,
			int group_dbid) {
		Boolean rtnval = false;
		
		try {
			CfgAgentGroupQuery agentgroupquery = new CfgAgentGroupQuery();
			agentgroupquery.setTenantDbid(tenant_dbid);
			agentgroupquery.setDbid(group_dbid);
			CfgAgentGroup agentgroup = service.retrieveObject(CfgAgentGroup.class,agentgroupquery);

			if(agentgroup != null){
				agentgroup.delete();
				rtnval = true;
			}
		} catch (ConfigException e) {
			logger.error("ConfigException", e);
		}
		return rtnval;
	}
	
	/**
	 * AgentGroup에 상담사를 Assign한다.
	 * @param service
	 * @param tenant_dbid
	 * @param group_name
	 * @param employee_ids
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentGroup assignPersonToAgentGroup(
			final IConfService service,
			int tenant_dbid,
			String group_name,
			String employee_ids
			) {
		CfgAgentGroup agentgroup = null;
		Collection<CfgPerson> rtn_persons = null;
		CfgPerson rtn_person = null;
		try{
			CfgAgentGroupQuery agentgroupquery = new CfgAgentGroupQuery();
			agentgroupquery.setTenantDbid(tenant_dbid);
			agentgroupquery.setName(group_name);
			agentgroup = service.retrieveObject(CfgAgentGroup.class,	agentgroupquery);
			String[] emps = employee_ids.split(",");
			if(emps != null && emps.length > 0) {
				ps = new ConfigPerson();

				if(agentgroup != null){
					if(agentgroup.getAgentDBIDs() != null){
						for(int i=0; i<emps.length; i++){
							rtn_person = null;
							rtn_person = ps.getPersonInfo(service, tenant_dbid, "emp", emps[i]);
							agentgroup.getAgentDBIDs().add(rtn_person.getDBID());
						}
					}else{
						rtn_persons = new HashSet<CfgPerson>();

						for(int i=0; i<emps.length; i++){
							rtn_person = null;
							rtn_person = ps.getPersonInfo(service, tenant_dbid, "emp", emps[i]);
							if(rtn_person != null) {
								rtn_persons.add(rtn_person);
							}
						}
						agentgroup.setAgents(rtn_persons);
					}
				}							
			}
			agentgroup.save();

		}catch(ConfigException ex){
			logger.error("ConfigException", ex);
		}
		return agentgroup;
	}
	
	/**
	 * AgentGroup에 상담사를 Assign한다.
	 * @param service
	 * @param tenant_dbid
	 * @param group_dbid
	 * @param person_dbid
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentGroup assignPersonToAgentGroup(
			final IConfService service,
			int tenant_dbid,
			int group_dbid,
			int person_dbid
			) {
		Boolean rtnval = false;
		Collection<CfgPerson> rtn_persons = null;
		CfgAgentGroup agentgroup = null;
		try{
			CfgAgentGroupQuery agentgroupquery = new CfgAgentGroupQuery();
			agentgroupquery.setTenantDbid(tenant_dbid);
			agentgroupquery.setDbid(group_dbid);
			agentgroup = service.retrieveObject(CfgAgentGroup.class,	agentgroupquery);
			
			if(agentgroup != null){
				if(agentgroup.getAgentDBIDs() != null){
					logger.info("null아님");
					agentgroup.getAgentDBIDs().add(person_dbid);
				}else{
					logger.info("null임");
					rtn_persons = ps.getPersonInfo(service, tenant_dbid, person_dbid);
					agentgroup.setAgents(rtn_persons);
				}
				agentgroup.save();
			}			
		}catch(ConfigException ex){
			logger.error("ConfigException", ex);
		}
		return agentgroup;
	}
	
	/**
	 * AgentGroup에 Assign된 상담사를 삭제한다.
	 * @param service
	 * @param tenant_dbid
	 * @param group_dbid
	 * @param person_dbid
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentGroup removePersonFromAgentGroup(
			final IConfService service,
			int tenant_dbid,
			String group_name,
			String employee_ids		 
			){

		Boolean rtnval = false;
		CfgPerson person = null;
		CfgAgentGroup agentgroup = null;
		try{			
			CfgAgentGroupQuery agentgroupquery = new CfgAgentGroupQuery();
			agentgroupquery.setTenantDbid(tenant_dbid);
			agentgroupquery.setName(group_name);
			agentgroup = service.retrieveObject(CfgAgentGroup.class,	agentgroupquery);
			String[] emps = employee_ids.split(",");
			if(agentgroup != null){
				ps = new ConfigPerson();
				for(int i=0; i<emps.length; i++){
					person = null;
					person = ps.getPersonInfo(service, tenant_dbid, "emp", emps[i]);
					if(agentgroup.getAgentDBIDs() != null) {
						agentgroup.getAgentDBIDs().remove(person.getDBID());
					}else {
						System.out.println("Agent Is Null");
					}
				}
				agentgroup.save();
			}			
		}catch(ConfigException ex){	
			logger.error("ConfigException", ex);
		}
		return agentgroup;
	}
	
	/**
	 * Source AgentGroup에 속한 Person을 Target AgentGroup으로 이동한다.
	 * @param service
	 * @param tenant_dbid
	 * @param source_group_name
	 * @param target_group_name
	 * @param delete_yn
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentGroup movePerson(
			final IConfService service,
			int tenant_dbid,
			String source_group_name,
			String target_group_name,
			String delete_yn
			){

		Boolean rtnval = false;
		CfgAgentGroup s_agentgroup = null;
		CfgAgentGroup t_agentgroup = null;
		try{
			CfgAgentGroupQuery agentgroupquery = new CfgAgentGroupQuery();
			agentgroupquery.setTenantDbid(tenant_dbid);
			agentgroupquery.setName(source_group_name);
			s_agentgroup = service.retrieveObject(CfgAgentGroup.class,	agentgroupquery);
			agentgroupquery.setName(target_group_name);
			t_agentgroup = service.retrieveObject(CfgAgentGroup.class,	agentgroupquery);			
			
			if(s_agentgroup != null) {
				if(s_agentgroup.getAgentDBIDs() != null) {
					if(t_agentgroup != null) {
						if(t_agentgroup.getAgentDBIDs() != null) {
							for(int dbid : s_agentgroup.getAgentDBIDs()) {
								t_agentgroup.getAgentDBIDs().add(dbid);
							}
						}else {
							t_agentgroup.setAgents(s_agentgroup.getAgents());
						}
						t_agentgroup.save();
						if(delete_yn.equals("Y")) {
							Collection<CfgPerson> rtnpersons = new HashSet<CfgPerson>();
							s_agentgroup.setAgents(rtnpersons);
							s_agentgroup.save();
						}
					}
				}
			}
		}catch(ConfigException ex){
			logger.error("ConfigException", ex);
		}
		return t_agentgroup;
	}
	
	/**
	 * Source AgentGroup에 속한 Person을 Target AgentGroup으로 이동한다.
	 * @param service
	 * @param tenant_dbid
	 * @param source_group_name
	 * @param target_group_name
	 * @param employee_ids
	 * @param delete_yn
	 * @return
	 * @throws ConfigException
	 * @throws InterruptedException
	 */
	public CfgAgentGroup movePerson2(
			final IConfService service,
			int tenant_dbid,
			String source_group_name,
			String target_group_name,
			String employee_ids,
			String delete_yn
			){

		Boolean rtnval = false;
		CfgAgentGroup s_agentgroup = null;
		CfgAgentGroup t_agentgroup = null;
		Collection<CfgPerson> rtn_persons = null;
		try{
			CfgAgentGroupQuery agentgroupquery = new CfgAgentGroupQuery();
			agentgroupquery.setTenantDbid(tenant_dbid);
			agentgroupquery.setName(source_group_name);
			s_agentgroup = service.retrieveObject(CfgAgentGroup.class,	agentgroupquery);
			agentgroupquery.setName(target_group_name);
			t_agentgroup = service.retrieveObject(CfgAgentGroup.class,	agentgroupquery);			
			String[] emps = employee_ids.split(",");
			if(s_agentgroup != null) {
				if(s_agentgroup.getAgentDBIDs() != null) {
					if(t_agentgroup != null) {

						
						if(emps != null && emps.length > 0) {
							if(t_agentgroup.getAgentDBIDs() != null) {	//Person이 있을때 (있는거에 add)
								for(CfgPerson person : s_agentgroup.getAgents()) {
									for(int i=0; i<emps.length; i++) {
										if(person.getEmployeeID().equals(emps[i])) {
											t_agentgroup.getAgentDBIDs().add(person.getDBID());										
										}
									}
								}
							}else {										//Person이 없을때(Collection만들어서 setAgent)
								rtn_persons = new HashSet<CfgPerson>();
								for(CfgPerson person : s_agentgroup.getAgents()) {
									for(int i=0; i<emps.length; i++) {
										if(person.getEmployeeID().equals(emps[i])) {	//상담그룹의 상담사 목록에 emps가 있으면
											rtn_persons.add(person);
										}
									}
								}
								t_agentgroup.setAgents(rtn_persons);
							}
						}else {
							if(t_agentgroup.getAgentDBIDs() != null) {	//Person이 있을때 (있는거에 add)
								for(int dbid : s_agentgroup.getAgentDBIDs()) {
									t_agentgroup.getAgentDBIDs().add(dbid);
								}
							}else {										//Person이 없을때(Collection만들어서 setAgent)
								t_agentgroup.setAgents(s_agentgroup.getAgents());
							}
						}
						t_agentgroup.save();
						if(delete_yn.equals("Y")) {
							
							Collection<CfgPerson> delpersons = s_agentgroup.getAgents();
							if(delpersons != null) {
								if(rtn_persons != null) {
									for(CfgPerson ps : rtn_persons) {
										delpersons.remove(ps);
									}
								}else {
									delpersons = new HashSet<CfgPerson>();
								}
							}else {
								delpersons = new HashSet<CfgPerson>();
							}
							s_agentgroup.setAgents(delpersons);
							s_agentgroup.save();
						}
					}
				}
			}
		}catch(ConfigException ex){
			logger.error("ConfigException", ex);
		}
		return t_agentgroup;
	}
}
