package kr.co.i4way.genesys.config;

import java.util.ArrayList;
import java.util.Collection;

//import org.apache.log4j.Logger;

import com.genesyslab.platform.applicationblocks.com.IConfService;
import com.genesyslab.platform.applicationblocks.com.ConfigException;
import com.genesyslab.platform.applicationblocks.com.objects.CfgAgentGroup;
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.CfgObjectID;
import com.genesyslab.platform.applicationblocks.com.objects.CfgPerson;
import com.genesyslab.platform.applicationblocks.com.objects.CfgPlaceGroup;
import com.genesyslab.platform.commons.protocol.ProtocolException;
import com.genesyslab.platform.configuration.protocol.types.CfgObjectType;

public class ConfigModule {
	
	IConfService service;
	InitializationConfig initcfgsrv;
 
	AgentLogin al;
	AgentGroup ag;
	PlaceGroup pg;
	Person ps;
	DNGroup dg;
	DNs dn;
	Folder fs;

	String 	sCfgSvrIp;
	String 	sCfgSvrIp_b;
	int 	iCfgSvrPort;
	int 	iCfgSvrPort_b;
	String 	sCfgSvrId;
	String 	sCfgSvrPasswd;
	String  sAppName;
	String sCharSet;
	
	String system_key = "http://www.i4way.co.kr";
	
	private Collection tmpColl;

	public ConfigModule(String cfg_svr_ip_p, String cfg_svr_ip_b, int cfg_svr_port_p, int cfg_svr_port_b, String cfg_svr_id, String cfg_svr_pw, String cfg_svr_appnm, String cfg_svr_charset){
		al = new AgentLogin();
		ag = new AgentGroup();
		pg = new PlaceGroup();
		dg = new DNGroup();
		dn = new DNs();
		fs = new Folder();
		ps = new Person();
		
		sCfgSvrIp = cfg_svr_ip_p;
		iCfgSvrPort = cfg_svr_port_p;
		sCfgSvrIp_b = cfg_svr_ip_b;
		iCfgSvrPort_b = cfg_svr_port_b;
		sCfgSvrId = cfg_svr_id;
		sCfgSvrPasswd = cfg_svr_pw;
		sAppName = cfg_svr_appnm;
		sCharSet = cfg_svr_charset;
	}
	
	public IConfService getService(){
		return service;
	}

	private boolean connectCfgServer(){
		boolean rtnval = false;
		initcfgsrv = new InitializationConfig();

		try {
			service = initcfgsrv.initializeConfigService(
					sAppName, sCfgSvrIp, iCfgSvrPort,
					sCfgSvrId, sCfgSvrPasswd, sCharSet
					);
			rtnval = true;
		} catch (ProtocolException e) {
			e.printStackTrace();
			rtnval = false;
		} catch (ConfigException e) {
			e.printStackTrace();
			rtnval = false;
		} catch (InterruptedException e) {
			e.printStackTrace();
			rtnval = false;
		}catch(Exception ex){
			ex.printStackTrace();
			rtnval = false;
		}
		return rtnval;
	}
	
	private boolean disConnectCfgServer(){
		boolean rtnval = false;
		try {
			if(service != null){
				initcfgsrv.uninitializeConfigService(service);
				rtnval = true;
				
			}else{
			}
		} catch (ProtocolException e) {
			e.printStackTrace();
			rtnval = false;
		} catch (InterruptedException e) {
			e.printStackTrace();
			rtnval = false;
		}catch(Exception ex){
			ex.printStackTrace();
			rtnval = false;
		}
		return rtnval;
	}
	
	public Collection<CfgObjectID> selectFolder(int folderDBID, String FolderName, CfgObjectType objtype){
		Collection<CfgFolder> folders = null;
		Collection<CfgObjectID> objectids = null;
		try{
			if(connectCfgServer()){
				folders = fs.SelectCfgFolder(folderDBID, FolderName, objtype, service);
				for (CfgFolder foldera : folders) {
					objectids = foldera.getObjectIDs();
				}
			}
		}catch(ConfigException e){
			e.printStackTrace();
		}catch(InterruptedException e){
			e.printStackTrace();
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			disConnectCfgServer();
		}
		return objectids;
	}
	

    /**
     * getPersonInfo
     * 특정 상담그룹에 속한 상담원 정보를 가져온다
     * @param AgentGroupDbid 상담그룹 DBID
     * @return List<CfgPerson> 상담사정보(List객체)
     */
    public ArrayList<CfgPerson> selectPerson(String AgentGroupDbid, int tenant_dbid){
    	ArrayList<CfgPerson> arrlist = null;
    	int tmpAgentGroupDbid =  Integer.parseInt(AgentGroupDbid);
		tmpColl = null;
		try{
			if(connectCfgServer()){
				tmpColl = ag.SelectPersonInGroup(tenant_dbid, tmpAgentGroupDbid, service);
				arrlist = new ArrayList<CfgPerson>(tmpColl);
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){ 
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			disConnectCfgServer();
		}
		return arrlist;
	}
    

    /**
     * getPersonInfo
     * 특정 상담사 정보를 가져온다
     * @param EmpID 상담사 사번
     * @return 상담사정보(Collection<CfgPerson>)
     */
    public CfgPerson selectPerson_emp(String EmpID, int tenant_dbid){
    	CfgPerson person = null;
		try{
			if(connectCfgServer()){
				person = ps.SelectPersons(tenant_dbid, EmpID, service);
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){ 
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			disConnectCfgServer();
		}
		return person;
	}
	
	public ArrayList<CfgAgentGroup> selectAgentGroups(Collection<CfgObjectID> objid, int tenant_dbid){
		ArrayList<CfgAgentGroup> arrlist = new ArrayList<CfgAgentGroup>();
		
		try{
			if(connectCfgServer()){
				for(CfgObjectID obj1 : objid){
					if(obj1.getType().equals(CfgObjectType.CFGAgentGroup)){
						//System.out.println("ag.SelectAgentGroup==> " + obj1.getDBID() + "");
						arrlist.add(ag.SelectAgentGroup(tenant_dbid, obj1.getDBID(), service));
					}
				}
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			disConnectCfgServer();
		}
		return arrlist;
	}
	
	public ArrayList<CfgPlaceGroup> selectPlaceGroups(Collection<CfgObjectID> objid, int tenant_dbid){
		ArrayList<CfgPlaceGroup> arrlist = new ArrayList<CfgPlaceGroup>();
		
		try{
			if(connectCfgServer()){
				for(CfgObjectID obj1 : objid){
					arrlist.add(pg.SelectPlaceGroup(tenant_dbid, obj1.getDBID(), service));
				}
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			disConnectCfgServer();
		}
		return arrlist;
	}
	
	public CfgPlaceGroup selectPlaceGroup(int dbid, int tenant_dbid){
		CfgPlaceGroup placegroup = null;
		
		try{
			if(connectCfgServer()){
				placegroup = pg.SelectPlaceGroup(tenant_dbid, dbid, service);
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			disConnectCfgServer();
		}
		return placegroup;
	}
	
	public ArrayList<CfgPlaceGroup> selectPlaceGroups(int objId, int tenant_dbid){
		ArrayList<CfgPlaceGroup> arrlist = new ArrayList<CfgPlaceGroup>();
		
		try{
			if(connectCfgServer()){
				arrlist.add(pg.SelectPlaceGroup(tenant_dbid, objId, service));
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			disConnectCfgServer();
		}
		return arrlist;
	}
	
	public ArrayList<CfgDNGroup> selectDNGroups(Collection<CfgObjectID> objid, int tenant_dbid){
		ArrayList<CfgDNGroup> arrlist = new ArrayList<CfgDNGroup>();
		
		try{
			if(connectCfgServer()){
				for(CfgObjectID obj1 : objid){
					arrlist.add(dg.SelectDNGroup(tenant_dbid, obj1.getDBID(), service));
				}
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			disConnectCfgServer();
		}
		return arrlist;
	}
	
	public CfgDNGroup selectDNGroup(int dbid, int tenant_dbid){
		CfgDNGroup group = null;
		
		try{
			if(connectCfgServer()){
				group = dg.SelectDNGroup(tenant_dbid, dbid, service);
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			disConnectCfgServer();
		}
		return group;
	}
	
	public ArrayList<CfgDN> selectDNs(Collection<CfgObjectID> objid, int tenant_dbid){
		ArrayList<CfgDN> arrlist = new ArrayList<CfgDN>();
		
		try{
			if(connectCfgServer()){
				for(CfgObjectID obj1 : objid){
					arrlist.add(dn.SelectDN(tenant_dbid, obj1.getDBID(), service));
				}
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			disConnectCfgServer();
		}
		return arrlist;
	}
	
	public CfgDN selectDN(int dbid, int tenant_dbid){
		CfgDN dns = null;
		
		try{
			if(connectCfgServer()){
				dns = dn.SelectDN(tenant_dbid, dbid, service);
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			disConnectCfgServer();
		}
		return dns;
	}
		
	public Collection<CfgAgentGroup> selectAgentGroups(int tenant_dbid){
		Collection<CfgAgentGroup> agentgroups = null;
		try{
			if(connectCfgServer()){
				agentgroups = ag.SelectAgentGroup(tenant_dbid, service);
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){
		}catch(Exception ex){
		}finally{
			disConnectCfgServer();
		}
		return agentgroups;
	}
	
	public CfgAgentGroup selectAgentGroups(int dbid, int tenant_dbid){
		CfgAgentGroup agentgroups = null;
		try{
			if(connectCfgServer()){
				agentgroups = ag.SelectAgentGroup(tenant_dbid, dbid, service);
			}
		}catch(ConfigException e){
		}catch(InterruptedException e){
		}catch(Exception ex){
		}finally{
			disConnectCfgServer();
		}
		return agentgroups;
	}
}