package kr.co.i4way.genesys.stat;

import java.net.URI;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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.CfgPerson;
import com.genesyslab.platform.applicationblocks.com.objects.CfgPlaceGroup;
import com.genesyslab.platform.applicationblocks.warmstandby.WarmStandbyConfiguration;
import com.genesyslab.platform.applicationblocks.warmstandby.WarmStandbyListener;
import com.genesyslab.platform.applicationblocks.warmstandby.WarmStandbyService;
import com.genesyslab.platform.applicationblocks.warmstandby.WarmStandbyStateChangedEvent;
import com.genesyslab.platform.commons.collections.KeyValueCollection;
import com.genesyslab.platform.commons.collections.KeyValuePair;
import com.genesyslab.platform.commons.collections.ValueType;
import com.genesyslab.platform.commons.connection.Connection;
import com.genesyslab.platform.commons.connection.configuration.PropertyConfiguration;
import com.genesyslab.platform.commons.protocol.ChannelClosedEvent;
import com.genesyslab.platform.commons.protocol.ChannelErrorEvent;
import com.genesyslab.platform.commons.protocol.ChannelListener;
import com.genesyslab.platform.commons.protocol.ClientChannel;
import com.genesyslab.platform.commons.protocol.Endpoint;
import com.genesyslab.platform.commons.protocol.Message;
import com.genesyslab.platform.commons.protocol.MessageHandler;
import com.genesyslab.platform.commons.protocol.ProtocolException;
import com.genesyslab.platform.commons.protocol.RegistrationException;
import com.genesyslab.platform.commons.threading.CompletionHandler;
import com.genesyslab.platform.configuration.protocol.types.CfgAppType;
import com.genesyslab.platform.reporting.protocol.StatServerProtocol;
import com.genesyslab.platform.reporting.protocol.statserver.AgentGroup;
import com.genesyslab.platform.reporting.protocol.statserver.AgentStatus;
import com.genesyslab.platform.reporting.protocol.statserver.AgentStatusesCollection;
import com.genesyslab.platform.reporting.protocol.statserver.DnActionsMask;
import com.genesyslab.platform.reporting.protocol.statserver.DnStatus;
import com.genesyslab.platform.reporting.protocol.statserver.DnStatusesCollection;
import com.genesyslab.platform.reporting.protocol.statserver.Notification;
import com.genesyslab.platform.reporting.protocol.statserver.NotificationMode;
import com.genesyslab.platform.reporting.protocol.statserver.PlaceStatus;
import com.genesyslab.platform.reporting.protocol.statserver.StatisticCategory;
import com.genesyslab.platform.reporting.protocol.statserver.StatisticInterval;
import com.genesyslab.platform.reporting.protocol.statserver.StatisticMetric;
import com.genesyslab.platform.reporting.protocol.statserver.StatisticMetricEx;
import com.genesyslab.platform.reporting.protocol.statserver.StatisticObject;
import com.genesyslab.platform.reporting.protocol.statserver.StatisticObjectType;
import com.genesyslab.platform.reporting.protocol.statserver.StatisticSubject;
import com.genesyslab.platform.reporting.protocol.statserver.events.EventInfo;
import com.genesyslab.platform.reporting.protocol.statserver.events.EventStatisticOpened;
import com.genesyslab.platform.reporting.protocol.statserver.requests.RequestCloseStatistic;
import com.genesyslab.platform.reporting.protocol.statserver.requests.RequestOpenStatistic;
import com.genesyslab.platform.reporting.protocol.statserver.requests.RequestOpenStatisticEx;

public class StatReciver {

	public interface statReciverCallback {
		void statReciver(String tranType, HashMap<String,String>wbdMap);
	}

	WarmStandbyService wsService;
	private WallBoardVo wv;
		
	private List<Integer> openedStats = Collections.synchronizedList(new ArrayList<Integer>());
	int iRefId = 1;
	String qg_readyCall = "";		//대기콜
	String ag_login = "";	//로그인 상담사수
	String ag_ready = "";	//대기 상담사수
	String ag_talk = "";	//통화중 상담사수
	String ag_talk_all = "";	//전체통화중 상담사수
	String ag_ib = "";	//인바운드통화중 상담사수
	String ag_ob = "";	//아웃바운드통화중 상담사수
	String ag_notready = "";	//이석 상담사수
	String ag_acw = "";	//통화후작업 상담사수

	String a_avg_call = "0";	//평균통화시간
	String a_avg_ready = "0";	//평균대기시간
	String a_avg_acw = "0";	//평균후처리시간
	String a_tot_call = "0";	//전체통화시간
	String a_ib_cnt = "0";	//인바운드콜
	String a_ob_cnt = "0";	//아웃바운드콜
	
	//String AgentValue = "";	//상담사 상태 데이터
	String Gubun = "";		//조회구분
	
	String wall = "0", wall_readyCall = "0", wall_stat = "0"				//팀상태
			, wall_nready = "0", wall_enter = "0", wall_answer = "0"
			, wall_answer_tr = "0", wall_abn = "0", wall_abn_sys = "0";

	static CfgAppType clientType = CfgAppType.CFGStatServer;
	StatServerProtocol protocol;
	private statReciverCallback statCallback;
	EventInfo eventinfo = null;

	ArrayList<StatObject> wallStatObj;	//Stat요청 배열
	HashMap<String,String> agentStatMap;	//상담사 상태 배열
	HashMap<Integer,String> statMap;	//Stat요청과 결과 배열
	
	HashMap<String,Object> statValueMap;	//Stat 결과 배열
	
	HashMap<String,String> wallBoardMap;	//가공된 결과 리턴을 위한 배열

	public void setStatReciverCallback(statReciverCallback callback) {
		statCallback = callback;
	}

	/**
	 * class 초기화
	 * @param wallboardvo
	 */
	public StatReciver(WallBoardVo wallboardvo) {
		statMap = new HashMap<Integer, String>();
		statValueMap = new HashMap<String, Object>();
		wallBoardMap = new HashMap<String,String>();

		wv = wallboardvo;
	}

	/**
	 * Stat요청을 시작한다.
	 * @param Gbn
	 */
	public void openStat(String Gbn){
		Gubun = Gbn;
		openProtocol();
	}
	
	/**
	 * Stat요청을 종료한다.
	 */
	public void closeStat(){
		closeProtocol();
	}	
	
	/**
	 * Stat Server와 연결방법을 설정하고 연결한다. 
	 */
	private void openProtocol() {

		PropertyConfiguration connConf = new PropertyConfiguration();
		connConf.setUseAddp(true); // - ConnectionConfiguration typified options
									// like "UseAddp", "AddpClientTimeout", etc
		connConf.setAddpServerTimeout(10);
		connConf.setAddpClientTimeout(10);
		connConf.setOption(Connection.STR_ATTR_ENCODING_NAME_KEY, wv.getStat_charset());

		Endpoint endpoint = new Endpoint(wv.getCfg_endpoint_p(), wv.getStat_ip_p(),
				wv.getStat_port_p(), connConf); // - Target server host/port are here
		Endpoint endpointBackup = new Endpoint(wv.getCfg_endpoint_b(), wv.getStat_ip_b(),
				wv.getStat_port_b(), connConf); // - Backup server host/port
													// are here

		protocol = new StatServerProtocol(endpoint);
		protocol.setClientName(wv.getStat_client_name()); // - Protocol handshake typified
												// options like "ClientName",
												// etc

		//웜스텐바이 설정
		WarmStandbyConfiguration wsConf = new WarmStandbyConfiguration(
				endpoint, endpointBackup);
		wsConf.setAttempts((short) 5); // - WarmStandby typified options like
										// "WarmStandbyAttempts", etc
		wsConf.setTimeout(2000);
		WarmStandbyService wsService = new WarmStandbyService(protocol);
		wsService.applyConfiguration(wsConf);
		wsService.start();
		wsService.addListener(new WarmStandbyListener() {		//warm Standby 연결시 이벤트 리스너 설정
			public void onStateChanged(final WarmStandbyStateChangedEvent event) {		//연결상태 변경시
				onWarmStandbyStateChanged(event);
			}

			public void onSwitchover(final EventObject event) {		//연결된 Stat Server가 Switch Over 됐을때
				onWarmStandbySwitchedOver(event);
			}
		});
		protocol.addChannelListener(new ChannelListener() {		//채널에 Listener가 추가됐을때
			public void onChannelOpened(final EventObject event) {	//채널Open
				onProtocolChannelOpened(event);
			}

			public void onChannelClosed(final ChannelClosedEvent event) {	//채널Close
				onProtocolChannelClosed(event);
			}

			public void onChannelError(final ChannelErrorEvent event) {		//채널 Error
				onProtocolChannelError(event);
			}
		});
		protocol.setMessageHandler(new MessageHandler() {	//메세지 발생시 처리
			public void onMessage(final Message message) {
				if(message.messageName().equals("EventInfo")){		//메세지 명이 EventInfo이면
					formatStatistics(message);	//통계 데이터 처리 메서드 호출
				}
			}
		});
		try {
			protocol.open();
		} catch (RegistrationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void closeProtocol(){
		try {
			protocol.close(true);
		} catch (RegistrationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void onWarmStandbySwitchedOver(final EventObject event) {
		System.out.println("onWarmStandbySwitchedOver");
	}

	private void onWarmStandbyStateChanged(
			final WarmStandbyStateChangedEvent event) {
		System.out.println("onWarmStandbyStateChanged");
	}

	private void onProtocolChannelOpened(final EventObject event) {
		System.out.println("onProtocolChannelOpened");
		setStatisticsAll();
	}

	private void onProtocolChannelClosed(final ChannelClosedEvent event) {
		System.out.println("onProtocolChannelClosed");
	}

	private void onProtocolChannelError(final ChannelErrorEvent event) {
		System.out.println("onProtocolChannelError");
		System.out.println(event.toString());
	}

	private void setStatisticsAll(){
		wallStatObj = new ArrayList<StatObject>();
		statMap = new HashMap<Integer, String>();
		iRefId = 1;
		try{
			System.out.println("Gubun==> " + Gubun);
			if(Gubun.equals("WALL") || Gubun.equals("MINI")){
				setStatistics_GA(wv.getStatModelList(), wv.getAgent_group_list(), "AGA");
				setStatistics_GQ(wv.getStatModelList(), wv.getQueue_group_list());
				setStatistics_PS(wv.getStatModelList(), wv.getCurrent_person());
			}else if(Gubun.equals("WALBD")){
				setStatistics_GA(wv.getStatModelList(), wv.getAgent_group_list(), "AGA");
				setStatistics_GQ(wv.getStatModelList(), wv.getQueue_group_list());
			}else if(Gubun.equals("STAT")){
				setStatistics_GA(wv.getStatModelList(), wv.getAgent_group_list(), "AGA");
				setStatistics_GQ(wv.getStatModelList(), wv.getQueue_group_list());
				setStatistics_PS(wv.getStatModelList(), wv.getPerson_list());
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void setStatistics_GA(ArrayList<StatModel> model,
			ArrayList<CfgAgentGroup> cfgobj, String Types) {
		StatObject tmpobj = null;
		String WallInfo = "";
		String statMapInfo = "";

		for (CfgAgentGroup cObj : cfgobj) {
			for (StatModel tmpModel : model) {
				if (tmpModel.getStatObjType().equals("GroupAgents")) {
					//if(Gubun.equals("STAT")){
						if(tmpModel.getStatId().equals("300")){
							tmpobj = new StatObject();
							setstat(cObj.getGroupInfo().getName(),
									ConvertObjectType(tmpModel.getStatObjType()),
									tmpModel.getStatType(), tmpModel.getTimeProfile(),
									tmpModel.getTimeRange(), tmpModel.getTimeFilter(),
									ConvertNotiMode(tmpModel.getNotiMode()), iRefId,
									cObj.getDBID(), tmpModel.getStatId());

							tmpobj.setRefId(iRefId);
							tmpobj.setStatId(tmpModel.getStatId());
							tmpobj.setDbid(cObj.getDBID());
							
							if(Types.equals("AGV")){
								tmpobj.setStatTypeShort("AGV");
								statMapInfo = "AGV^" + cObj.getDBID() + "^" + tmpModel.getStatId() + "^";
							}else{
								tmpobj.setStatTypeShort("AG");
								statMapInfo = "AG^" + cObj.getDBID() + "^" + tmpModel.getStatId() + "^";
							}
							
							tmpobj.setObjtype(tmpModel.getStatObjType());
							tmpobj.setStatType(tmpModel.getStatType());

							if (tmpModel.getTimeProfile() != null) {
								WallInfo = WallInfo + tmpModel.getTimeProfile();
							} else {
								WallInfo = WallInfo + "None";
							}
							if (tmpModel.getTimeFilter() != null) {
								WallInfo = WallInfo + "^" + tmpModel.getTimeFilter();
							} else {
								WallInfo = WallInfo + "^" + "None";
							}
							if (tmpModel.getTimeRange() != null) {
								WallInfo = WallInfo + "^" + tmpModel.getTimeRange();
							} else {
								WallInfo = WallInfo + "^" + "None";
							}
							tmpobj.setWallInfo(WallInfo);
							tmpobj.setVirtualYn(Types);
							tmpobj.setCurrValue(-1);
							wallStatObj.add(tmpobj);
							statMap.put(iRefId, statMapInfo);
							WallInfo = "";
							iRefId++;
						}
						
					//}else{
						//else if(!tmpModel.getStatId().equals("300")){
						else{
							tmpobj = new StatObject();
							setstat(cObj.getGroupInfo().getName(),
									ConvertObjectType(tmpModel.getStatObjType()),
									tmpModel.getStatType(), tmpModel.getTimeProfile(),
									tmpModel.getTimeRange(), tmpModel.getTimeFilter(),
									ConvertNotiMode(tmpModel.getNotiMode()), iRefId,
									cObj.getDBID(), tmpModel.getStatId());

							tmpobj.setRefId(iRefId);
							tmpobj.setStatId(tmpModel.getStatId());
							tmpobj.setDbid(cObj.getDBID());
							
							if(Types.equals("AGV")){
								tmpobj.setStatTypeShort("AGV");
								statMapInfo = "AGV^" + cObj.getDBID() + "^" + tmpModel.getStatId() + "^";
							}else{
								tmpobj.setStatTypeShort("AG");
								statMapInfo = "AG^" + cObj.getDBID() + "^" + tmpModel.getStatId() + "^";
							}
							
							tmpobj.setObjtype(tmpModel.getStatObjType());
							tmpobj.setStatType(tmpModel.getStatType());

							if (tmpModel.getTimeProfile() != null) {
								WallInfo = WallInfo + tmpModel.getTimeProfile();
							} else {
								WallInfo = WallInfo + "None";
							}
							if (tmpModel.getTimeFilter() != null) {
								WallInfo = WallInfo + "^" + tmpModel.getTimeFilter();
							} else {
								WallInfo = WallInfo + "^" + "None";
							}
							if (tmpModel.getTimeRange() != null) {
								WallInfo = WallInfo + "^" + tmpModel.getTimeRange();
							} else {
								WallInfo = WallInfo + "^" + "None";
							}
							tmpobj.setWallInfo(WallInfo);
							tmpobj.setVirtualYn(Types);
							tmpobj.setCurrValue(-1);
							wallStatObj.add(tmpobj);
							statMap.put(iRefId, statMapInfo);
							WallInfo = "";
							iRefId++;
						}
					//}

				}
			}
		}
	}

	public void setStatistics_GQ(ArrayList<StatModel> model,
			ArrayList<CfgDNGroup> cfgobj) {
		StatObject tmpobj = null;
		String WallInfo = "";
		String statMapInfo = "";
		for (CfgDNGroup cObj : cfgobj) {
			for (StatModel tmpModel : model) {
				if (tmpModel.getStatObjType().equals("GroupQueues")) {
					tmpobj = new StatObject();
					setstat(cObj.getGroupInfo().getName(),
							ConvertObjectType(tmpModel.getStatObjType()),
							tmpModel.getStatType(), tmpModel.getTimeProfile(),
							tmpModel.getTimeRange(), tmpModel.getTimeFilter(),
							ConvertNotiMode(tmpModel.getNotiMode()), iRefId,
							cObj.getDBID(), tmpModel.getStatId());
					tmpobj.setRefId(iRefId);
					tmpobj.setStatId(tmpModel.getStatId());
					tmpobj.setDbid(cObj.getDBID());
					tmpobj.setStatTypeShort("GQ");
					statMapInfo = "GQ^" + cObj.getDBID() + "^" + tmpModel.getStatId() + "^";
					tmpobj.setObjtype(tmpModel.getStatObjType());
					tmpobj.setStatType(tmpModel.getStatType());
					if (tmpModel.getTimeProfile() != null) {
						WallInfo = WallInfo + tmpModel.getTimeProfile();
					} else {
						WallInfo = WallInfo + "None";
					}
					if (tmpModel.getTimeFilter() != null) {
						WallInfo = WallInfo + "^" + tmpModel.getTimeFilter();
					} else {
						WallInfo = WallInfo + "^" + "None";
					}
					if (tmpModel.getTimeRange() != null) {
						WallInfo = WallInfo + "^" + tmpModel.getTimeRange();
					} else {
						WallInfo = WallInfo + "^" + "None";
					}
					tmpobj.setWallInfo(WallInfo);
					tmpobj.setVirtualYn("N");
					tmpobj.setCurrValue(-1);
					wallStatObj.add(tmpobj);
					statMap.put(iRefId, statMapInfo);
					WallInfo = "";
					iRefId++;
				}
			}
		}
	}

	public void setStatistics_GP(ArrayList<StatModel> model,
			ArrayList<CfgPlaceGroup> cfgobj) {
		StatObject tmpobj = null;
		String WallInfo = "";
		String statMapInfo = "";
		for (CfgPlaceGroup cObj : cfgobj) {
			for (StatModel tmpModel : model) {
				if (tmpModel.getStatObjType().equals("GroupPlaces")) {
					tmpobj = new StatObject();
					setstat(cObj.getGroupInfo().getName(),
							ConvertObjectType(tmpModel.getStatObjType()),
							tmpModel.getStatType(), tmpModel.getTimeProfile(),
							tmpModel.getTimeRange(), tmpModel.getTimeFilter(),
							ConvertNotiMode(tmpModel.getNotiMode()), iRefId,
							cObj.getDBID(), tmpModel.getStatId());
					tmpobj.setRefId(iRefId);
					tmpobj.setStatId(tmpModel.getStatId());
					tmpobj.setDbid(cObj.getDBID());
					tmpobj.setStatTypeShort("GP");
					statMapInfo = "GP^" + cObj.getDBID() + "^" + tmpModel.getStatId() + "^";
					// tmpobj.setObjectId(cObj.getGroupInfo().getName());
					tmpobj.setObjtype(tmpModel.getStatObjType());
					tmpobj.setStatType(tmpModel.getStatType());
					if (tmpModel.getTimeProfile() != null) {
						WallInfo = WallInfo + tmpModel.getTimeProfile();
					} else {
						WallInfo = WallInfo + "None";
					}
					if (tmpModel.getTimeFilter() != null) {
						WallInfo = WallInfo + "^" + tmpModel.getTimeFilter();
					} else {
						WallInfo = WallInfo + "^" + "None";
					}
					if (tmpModel.getTimeRange() != null) {
						WallInfo = WallInfo + "^" + tmpModel.getTimeRange();
					} else {
						WallInfo = WallInfo + "^" + "None";
					}
					tmpobj.setWallInfo(WallInfo);
					tmpobj.setVirtualYn("N");
					tmpobj.setCurrValue(-1);
					wallStatObj.add(tmpobj);
					statMap.put(iRefId, statMapInfo);
					WallInfo = "";
					iRefId++;
				}
			}
		}
	}

	public void setStatistics_RP(ArrayList<StatModel> model,
			ArrayList<CfgDN> cfgobj) {
		StatObject tmpobj = null;
		String WallInfo = "";
		String statMapInfo = "";
		for (CfgDN cObj : cfgobj) {
			for (StatModel tmpModel : model) {
				if (tmpModel.getStatObjType().equals("RoutePoint")) {
					tmpobj = new StatObject();
					setstat(cObj.getNumber() + "@" + wv.getStat_switch_name(),
							ConvertObjectType(tmpModel.getStatObjType()),
							tmpModel.getStatType(), tmpModel.getTimeProfile(),
							tmpModel.getTimeRange(), tmpModel.getTimeFilter(),
							ConvertNotiMode(tmpModel.getNotiMode()), iRefId,
							cObj.getDBID(), tmpModel.getStatId());
					tmpobj.setRefId(iRefId);
					tmpobj.setStatId(tmpModel.getStatId());
					tmpobj.setDbid(cObj.getDBID());
					tmpobj.setStatTypeShort("RP");
					statMapInfo = "RP^" + cObj.getDBID() + "^" + tmpModel.getStatId() + "^";
					tmpobj.setObjtype(tmpModel.getStatObjType());
					tmpobj.setStatType(tmpModel.getStatType());
					if (tmpModel.getTimeProfile() != null) {
						WallInfo = WallInfo + tmpModel.getTimeProfile();
					} else {
						WallInfo = WallInfo + "None";
					}
					if (tmpModel.getTimeFilter() != null) {
						WallInfo = WallInfo + "^" + tmpModel.getTimeFilter();
					} else {
						WallInfo = WallInfo + "^" + "None";
					}
					if (tmpModel.getTimeRange() != null) {
						WallInfo = WallInfo + "^" + tmpModel.getTimeRange();
					} else {
						WallInfo = WallInfo + "^" + "None";
					}
					tmpobj.setWallInfo(WallInfo);
					tmpobj.setVirtualYn("N");
					tmpobj.setCurrValue(0);
					wallStatObj.add(tmpobj);
					statMap.put(iRefId, statMapInfo);
					WallInfo = "";
					statMapInfo = null;
					tmpobj = null;
					iRefId++;
				}
			}
		}
	}
	
	public void setStatistics_PS(ArrayList<StatModel> model,
			CfgPerson cfgobj) {
		StatObject tmpobj = null;
		String WallInfo = "";
		String statMapInfo = "";
		//for (CfgPerson cObj : cfgobj) {
			for (StatModel tmpModel : model) {
				if (tmpModel.getStatObjType().equals("Agent")) {
						tmpobj = new StatObject();
						setstat(cfgobj.getEmployeeID(),
								ConvertObjectType(tmpModel.getStatObjType()),
								tmpModel.getStatType(), tmpModel.getTimeProfile(),
								tmpModel.getTimeRange(), tmpModel.getTimeFilter(),
								ConvertNotiMode(tmpModel.getNotiMode()), iRefId,
								cfgobj.getDBID(), tmpModel.getStatId());
						tmpobj.setRefId(iRefId);
						tmpobj.setStatId(tmpModel.getStatId());
						tmpobj.setDbid(cfgobj.getDBID());
						tmpobj.setStatTypeShort("PS");
						statMapInfo = "PS^" + cfgobj.getEmployeeID() + "^" + tmpModel.getStatId() + "^";
						tmpobj.setObjtype(tmpModel.getStatObjType());
						tmpobj.setStatType(tmpModel.getStatType());
						if (tmpModel.getTimeProfile() != null) {
							WallInfo = WallInfo + tmpModel.getTimeProfile();
						} else {
							WallInfo = WallInfo + "None";
						}
						if (tmpModel.getTimeFilter() != null) {
							WallInfo = WallInfo + "^" + tmpModel.getTimeFilter();
						} else {
							WallInfo = WallInfo + "^" + "None";
						}
						if (tmpModel.getTimeRange() != null) {
							WallInfo = WallInfo + "^" + tmpModel.getTimeRange();
						} else {
							WallInfo = WallInfo + "^" + "None";
						}
						tmpobj.setWallInfo(WallInfo);
						tmpobj.setVirtualYn("N");
						tmpobj.setCurrValue(0);
						wallStatObj.add(tmpobj);	//메모리 사용량 줄이기 위해... 
						statMap.put(iRefId, statMapInfo);
						WallInfo = "";
						statMapInfo = null;
						tmpobj = null;
						iRefId++;	
					}

				}
		//}
	}
	
	public void setStatistics_PS(ArrayList<StatModel> model,
			ArrayList<CfgPerson> cfg_obj_list) {
		StatObject tmpobj = null;
		String WallInfo = "";
		String statMapInfo = "";
		for (CfgPerson cfgobj : cfg_obj_list) {
			for (StatModel tmpModel : model) {
				if (tmpModel.getStatObjType().equals("Agent")) {
					if(Gubun.equals("STAT")){
						if(tmpModel.getStatId().equals("303")||tmpModel.getStatId().equals("304")){		//303=상담사 인바운드건수, 304=상담사 아웃바운드건수
							tmpobj = new StatObject();
							setstat(cfgobj.getEmployeeID(),
									ConvertObjectType(tmpModel.getStatObjType()),
									tmpModel.getStatType(), tmpModel.getTimeProfile(),
									tmpModel.getTimeRange(), tmpModel.getTimeFilter(),
									ConvertNotiMode(tmpModel.getNotiMode()), iRefId,
									cfgobj.getDBID(), tmpModel.getStatId());
							tmpobj.setRefId(iRefId);
							tmpobj.setStatId(tmpModel.getStatId());
							tmpobj.setDbid(cfgobj.getDBID());
							tmpobj.setStatTypeShort("PS");
							statMapInfo = "PS^" + cfgobj.getEmployeeID() + "^" + tmpModel.getStatId() + "^";
							tmpobj.setObjtype(tmpModel.getStatObjType());
							tmpobj.setStatType(tmpModel.getStatType());
							if (tmpModel.getTimeProfile() != null) {
								WallInfo = WallInfo + tmpModel.getTimeProfile();
							} else {
								WallInfo = WallInfo + "None";
							}
							if (tmpModel.getTimeFilter() != null) {
								WallInfo = WallInfo + "^" + tmpModel.getTimeFilter();
							} else {
								WallInfo = WallInfo + "^" + "None";
							}
							if (tmpModel.getTimeRange() != null) {
								WallInfo = WallInfo + "^" + tmpModel.getTimeRange();
							} else {
								WallInfo = WallInfo + "^" + "None";
							}
							tmpobj.setWallInfo(WallInfo);
							tmpobj.setVirtualYn("N");
							tmpobj.setCurrValue(0);
							wallStatObj.add(tmpobj);	//메모리 사용량 줄이기 위해... 
							statMap.put(iRefId, statMapInfo);
							WallInfo = "";
							statMapInfo = null;
							tmpobj = null;
							iRefId++;							
						}
					}else{
						tmpobj = new StatObject();
						setstat(cfgobj.getEmployeeID(),
								ConvertObjectType(tmpModel.getStatObjType()),
								tmpModel.getStatType(), tmpModel.getTimeProfile(),
								tmpModel.getTimeRange(), tmpModel.getTimeFilter(),
								ConvertNotiMode(tmpModel.getNotiMode()), iRefId,
								cfgobj.getDBID(), tmpModel.getStatId());
						tmpobj.setRefId(iRefId);
						tmpobj.setStatId(tmpModel.getStatId());
						tmpobj.setDbid(cfgobj.getDBID());
						tmpobj.setStatTypeShort("PS");
						statMapInfo = "PS^" + cfgobj.getEmployeeID() + "^" + tmpModel.getStatId() + "^";
						tmpobj.setObjtype(tmpModel.getStatObjType());
						tmpobj.setStatType(tmpModel.getStatType());
						if (tmpModel.getTimeProfile() != null) {
							WallInfo = WallInfo + tmpModel.getTimeProfile();
						} else {
							WallInfo = WallInfo + "None";
						}
						if (tmpModel.getTimeFilter() != null) {
							WallInfo = WallInfo + "^" + tmpModel.getTimeFilter();
						} else {
							WallInfo = WallInfo + "^" + "None";
						}
						if (tmpModel.getTimeRange() != null) {
							WallInfo = WallInfo + "^" + tmpModel.getTimeRange();
						} else {
							WallInfo = WallInfo + "^" + "None";
						}
						tmpobj.setWallInfo(WallInfo);
						tmpobj.setVirtualYn("N");
						tmpobj.setCurrValue(0);
						wallStatObj.add(tmpobj);	//메모리 사용량 줄이기 위해... 
						statMap.put(iRefId, statMapInfo);
						WallInfo = "";
						statMapInfo = null;
						tmpobj = null;
						iRefId++;	
					}

				}
			}
		}
	}
	
	private static NotificationMode ConvertNotiMode(String strNotiMode) {
		NotificationMode rtnNotiMode = null;
		if (strNotiMode.equals("Immediate")) {
			rtnNotiMode = NotificationMode.Immediate;
		} else if (strNotiMode.equals("NoNotification")) {
			rtnNotiMode = NotificationMode.NoNotification;
		} else if (strNotiMode.equals("Periodical")) {
			rtnNotiMode = NotificationMode.Periodical;
		} else {
		}
		return rtnNotiMode;
	}

	private static StatisticObjectType ConvertObjectType(String strObjType) {
		StatisticObjectType rtnObjectType = null;

		if (strObjType.equals("GroupAgents")) {
			rtnObjectType = StatisticObjectType.GroupAgents;
		} else if (strObjType.equals("GroupPlaces")) {
			rtnObjectType = StatisticObjectType.GroupPlaces;
		} else if (strObjType.equals("RoutePoint")) {
			rtnObjectType = StatisticObjectType.RoutePoint;
		} else if (strObjType.equals("GroupQueues")) {
			rtnObjectType = StatisticObjectType.GroupQueues;
		} else if (strObjType.equals("Agent")) {
			rtnObjectType = StatisticObjectType.Agent;
		}
		return rtnObjectType;
	}

	private void setstat(String objectid, StatisticObjectType statobjtype,
			String stattype, String t_profile, String t_range, String t_filter,
			NotificationMode notimode, int refId, int currDBID, String currStatId) {
		try {
			RequestOpenStatistic requestOpenStatistic = RequestOpenStatistic
					.create();

			StatisticObject object = StatisticObject.create();
			object.setObjectId(objectid);
			object.setObjectType(statobjtype);
			object.setTenantName(wv.getStat_tenant_name());
			object.setTenantPassword("");

			StatisticMetric metric = StatisticMetric.create();
			metric.setStatisticType(stattype);
			metric.setTimeProfile(t_profile);
			metric.setTimeRange(t_range);
			metric.setFilter(t_filter);

			Notification notification = Notification.create();
			notification.setMode(notimode);
			
			notification.setFrequency(wv.getStat_frequency());				

			requestOpenStatistic.setStatisticObject(object);
			requestOpenStatistic.setStatisticMetric(metric);
			requestOpenStatistic.setNotification(notification);

			requestOpenStatistic.setReferenceId(refId);

			//System.out.println("Sending:\n" + requestOpenStatistic);
			protocol.send(requestOpenStatistic);
			requestOpenStatistic = null;
			notification = null;
			object = null;
			metric = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private long getThreadId() {
		return Thread.currentThread().getId();
	}

	//Creating Dynamic Statistics and subscribe for notifications.
		//See http://docs.genesys.com/Documentation/PSDK/latest/Developer/StatServer
		public void openStatistics(StatExModel model) {		
			System.out.println(String.format("[ThreadId %d]: Subscribing for statistic notification....", getThreadId()));
			openedStats.clear();
			try {

					//Define statistic objects that have not already been defined in the Configuration Layer
					StatisticObject obj = StatisticObject.create(
							model.getObjId(),
							StatisticObjectType.valueOf(model.getObjType()),
							model.getTenant(),
							model.getTenantPw());

					StatisticMetricEx metric = StatisticMetricEx.create();
					metric.setMainMask(new DnActionsMask());
					metric.getMainMask().setAll();
					metric.setRelativeMask(new DnActionsMask());
					metric.getRelativeMask().clearAll();
					metric.setIntervalType(StatisticInterval.valueOf(model.getIntervalType()));
					metric.setIntervalLength(Integer.parseInt(model.getIntervalLength()));
					metric.setCategory(StatisticCategory.valueOf(model.getCategory()));
					metric.setSubject(StatisticSubject.valueOf(model.getSubject()));

					Notification notification = Notification.create(
							NotificationMode.valueOf(model.getNotificationMode()),
							model.getFrequency(),
							model.getInsensitivity());

					RequestOpenStatisticEx request = RequestOpenStatisticEx.create(obj, metric, notification);
					ClientChannel channel = (ClientChannel) wsService.getChannel();

					channel.requestAsync(request, this, new CompletionHandler<Message, StatReciver>() {

						@Override
						public void completed(Message result, StatReciver attachment) {
							if (result instanceof EventStatisticOpened) {
								Integer id = ((EventStatisticOpened) result).getReferenceId();
								openedStats.add(id);
								System.out.println(String.format("[ThreadId %d]: Statistic id=%d opened!", getThreadId(), id));
							} else {
								System.out.println(String.format("[ThreadId %d]: Failed to open statistic: %s", getThreadId(), result.toString()));
							}
						}

						@Override
						public void failed(Throwable arg0, StatReciver arg1) {
							// TODO Auto-generated method stub
							
						}
					});
			} catch (Throwable t) {
				t.printStackTrace();
			}

		}

		public void closeStatistics() {
			if (wsService != null) {
				ClientChannel channel = (ClientChannel) wsService.getChannel();
				synchronized (openedStats) {
					try {
						for (Integer id : openedStats) {
							RequestCloseStatistic close = RequestCloseStatistic.create(id);
							System.out.println("Closing statistics " + id);
							channel.request(close);
							System.out.println("Done.");
						}
					} catch (Throwable t) {
						t.printStackTrace();
					}
					openedStats.clear();
				}
			}
		}	
	
	
	private void formatStatistics(Message response) {
		String CurrValue = "";
		String CurrTime = "";
		String tmpStr = "";
		String tmpAni = "";
		String Reason = "";
		String Dn = "";
		String PersonName = ""; 
		String[] tmpStrArray;
		KeyValueCollection kvc = null;
		AgentStatusesCollection as_list;
		try{
			eventinfo = (EventInfo) response;
			eventinfo.getTimestamp();
			tmpStr = statMap.get(eventinfo.getReferenceId());
			tmpStrArray = tmpStr.split("\\^");
			
			if(tmpStrArray[2].equals("300")){
				AgentGroup group = (AgentGroup)eventinfo.getStateValue();
				if(group == null)
					return;
				
				if(group.getAgents() == null || group.getAgentCount() <= 0) return;
				
				as_list = group.getAgents();
				agentStatMap = new HashMap<String, String>();
				for(int j=0; j< as_list.getCount();j++){
					AgentStatus as = (AgentStatus)as_list.getItem(j);
					PlaceStatus ps = as.getPlace();
					CurrValue = as.getStatus() + "";
					CurrTime = (eventinfo.getTimestamp() - as.getTime()) + "";
					if(ps != null && ps.getPlaceId() != null && ps.getPlaceId().length() > 0){
						Dn = ps.getPlaceId();
					}
					if(CurrValue.equals("8")){
						DnStatusesCollection dc = ps.getDnStatuses();
						for(int i=0; i<dc.getCount(); i++){
							DnStatus ds = dc.getItem(i);
							if(ds.getActions().getCount() > 0){
								kvc = ds.getActions().getItem(3).getUserData();
								Reason = getVal(kvc, "ReasonCode");
							}
						}	
						CurrValue = CurrValue + Reason;
					}else if(CurrValue.equals("6") || CurrValue.equals("21")){
						DnStatusesCollection dc = ps.getDnStatuses();
						for(int i=0; i<dc.getCount(); i++){
							DnStatus ds = dc.getItem(i);
							if(ds.getActions().getCount() > 0){
								tmpAni = ds.getActions().getItem(4).getDnis();
							}
						}	
					}else if(CurrValue.equals("7") || CurrValue.equals("22")){
						DnStatusesCollection dc = ps.getDnStatuses();
						for(int i=0; i<dc.getCount(); i++){
							DnStatus ds = dc.getItem(i);
							if(ds.getActions().getCount() > 0){
								tmpAni = ds.getActions().getItem(4).getAni();
							}
						}	
					}
					for (CfgPerson cObj : wv.getPerson_list()) {
						if(as.getAgentId().equals(cObj.getEmployeeID())){
							PersonName = cObj.getFirstName();
							break;
						}
					}
					//agentStatMap.put(as.getAgentId(), as.getAgentId() + "*" +  CurrValue + "*" + CurrTime + "*" + PersonName + "*" + tmpAni + "*" + Dn + "@");
					agentStatMap.put(as.getAgentId(), as.getAgentId() + "*" +  CurrValue + "*" + CurrTime + "*" + PersonName + "*" + tmpAni + "*" + Dn);
					//AgentValue += as.getAgentId() + "*" +  CurrValue + "*" + CurrTime + "*" + PersonName + "*" + tmpAni + "*" + Dn + "@";
					PersonName = "";
					Dn = "";
					tmpAni = "";
				}
			}else{
				CurrValue = eventinfo.getStringValue() + "";
			}

			statValueMap.put(tmpStr, CurrValue);
			
			if(eventinfo.getReferenceId() == statMap.size()){
				statValueMap.put("AGENTS", agentStatMap);
				agentStatMap = null;
				if(wv.getCenter_gbn().equals("MALL")){
					makeSendData_mall();
				}else{
					makeSendData_park();
				}
				statCallback.statReciver("GROUP", wallBoardMap);
				//System.out.println(wallBoardMap.toString());
			}
			eventinfo = null;
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	/**
	 * TEST용 메서드(본사_park)
	 */
//	private void makeSendData_park(){
//		int tmpval = 0;
//		String parkstat = "";
//		String psstat = "";
//		String currentstat = "";
//		
//		qg_readyCall = objToInt(statValueMap.get("GQ^171^200^")) + objToInt(statValueMap.get("GQ^172^200^")) + objToInt(statValueMap.get("GQ^173^200^")) + "";
//		wall_readyCall = statValueMap.get("GQ^171^200^") + "*" + statValueMap.get("GQ^172^200^") + "*" + statValueMap.get("GQ^173^200^");
//		wall_stat = statValueMap.get("AG^174^100^") + "*" + statValueMap.get("AG^174^108^") + "*" + statValueMap.get("AG^174^101^") 
//							+ "*" + statValueMap.get("AG^174^107^") + "*" + statValueMap.get("AG^174^102^");
//		wall_nready = statValueMap.get("AG^174^103^") + "*" + statValueMap.get("AG^174^104^") + "*" + statValueMap.get("AG^174^105^")
//							+ "*" + statValueMap.get("AG^174^106^");
//		wall_enter = statValueMap.get("GQ^171^204^") + "*" + statValueMap.get("GQ^172^204^") + "*" + statValueMap.get("GQ^173^204^");
//		wall_answer = statValueMap.get("GQ^171^202^") + "*" + statValueMap.get("GQ^172^202^") + "*" + statValueMap.get("GQ^173^202^");
//		wall_answer_tr = statValueMap.get("GQ^171^203^") + "*" + statValueMap.get("GQ^172^203^") + "*" + statValueMap.get("GQ^173^203^");
//		wall_abn = statValueMap.get("GQ^171^201^") + "*" + statValueMap.get("GQ^172^201^") + "*" + statValueMap.get("GQ^173^201^");
//		wall_abn_sys = statValueMap.get("GQ^171^205^") + "*" + statValueMap.get("GQ^172^205^") + "*" + statValueMap.get("GQ^173^205^");
//		ag_login = (String)statValueMap.get("AG^174^100^");
//		ag_ready = (String)statValueMap.get("AG^174^108^");
//		ag_talk = objToInt(statValueMap.get("AG^174^101^")) + objToInt(statValueMap.get("AG^174^107^")) + "";
//		ag_notready = objToInt(statValueMap.get("AG^174^103^")) + objToInt(statValueMap.get("AG^174^109^")) + objToInt(statValueMap.get("AG^174^104^")) + objToInt(statValueMap.get("AG^174^105^")) + objToInt(statValueMap.get("AG^174^106^")) + "";
//		ag_acw = (String)statValueMap.get("AG^174^102^");
//		ag_talk_all = (String)statValueMap.get("AG^174^110^");
//
//		wall = "W1@" + wall_readyCall + "^" + "W2@" + wall_stat + "^" + "W3@" + wall_nready + "^" + "W4@" + wall_enter + "^" + "W5@" + wall_answer
//					+ "^" + "W6@" + wall_answer_tr + "^" + "W7@" + wall_abn + "^" + "W8@" + wall_abn_sys;
//		parkstat = qg_readyCall + "^" + ag_login + "^" + ag_ready + "^" +ag_talk  + "^" + ag_notready + "^" + ag_acw + "^" + ag_talk_all;	//대기콜수^로그인수^대기상담사^통화중(인,아웃)^이석^후처리^전체통화중
//		
//		wallBoardMap.put("PARK_STAT", parkstat);
//		
//		wallBoardMap.put("PARK_WALL", wall);
//		
//		CfgPerson cObj = wv.getCurrent_person();
//		tmpval = 0;
//		a_avg_call = "0";
//		a_avg_ready = "0";
//		a_avg_acw = "0";
//			
//		tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^303^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^304^"));
//		if(tmpval > 0){
//			a_avg_call = (objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^301^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^302^"))) / tmpval + "";
//		}
//
//		tmpval = 0;
//		tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^306^"));
//		if(tmpval > 0){
//			a_avg_ready = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^305^")) / tmpval + "";
//		}
//			
//		tmpval = 0;
//		tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^308^"));
//		if(tmpval > 0){
//			a_avg_acw = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^307^")) / tmpval + "";
//		}
//			
//		a_tot_call = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^301^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^302^")) + "";
//
//		a_ib_cnt = (String)statValueMap.get("PS^"+cObj.getEmployeeID()+"^303^");
//		a_ob_cnt = (String)statValueMap.get("PS^"+cObj.getEmployeeID()+"^304^");
//			
//		psstat = a_avg_call + "^" + a_avg_ready + "^" + a_avg_acw + "^" +a_tot_call  + "^" + a_ib_cnt + "^" + a_ob_cnt;	//평균통화^평균대기^평균후처리^누적통화^IB건수^OB건수
//			
//		wallBoardMap.put("PARK_" + cObj.getEmployeeID(), psstat);
//
//		
//		/***********************************************************************/	
//		// 새로 추가한거
//		/***********************************************************************/		
//		currentstat = "";																																		//지속시간					74*		
//		@SuppressWarnings("unchecked")																											//상담사ID					2000*	
//		HashMap<String,String> tmpMap = (HashMap<String, String>) statValueMap.get("AGENTS");					//상담사 상태, 사유		8*		
//		if(tmpMap != null){
//			for (Map.Entry<String, String> elem : tmpMap.entrySet()){																		//상담사명					홍준기*	
//				currentstat += elem.getValue() + "*" +  statValueMap.get("PS^"+elem.getKey()+"^303^") 	//전화번호					*		
//                + "*" + statValueMap.get("PS^"+elem.getKey()+"^304^")+ "@";						//내선						1000*	
//
//			}																																								//IB건수						0*		
//		}
//		if(currentstat.length() > 0){																														//OB건수					0		
//			currentstat= currentstat.substring(0, currentstat.length()-1);
//		}
//		wallBoardMap.put("PARK_CURRENT", currentstat);
////		/***********************************************************************/
////		// 예전꺼
////		/***********************************************************************/				
////		currentstat = (String)statValueMap.get("AGENTS");
////		if(currentstat.length() > 0){
////			currentstat= currentstat.substring(0, currentstat.length()-1);
////		}
////		wallBoardMap.put("PARK_CURRENT", currentstat);
//	}
	
	/**
	 * TEST용 메서드(본사_mall)
	 */
	private void makeSendData_mall(){
		int tmpval = 0;
		String mallstat = "";
		String psstat = "";
		String currentstat = "";
		
		qg_readyCall = objToInt(statValueMap.get("GQ^171^200^")) + "";
		wall_readyCall = (String)statValueMap.get("GQ^171^200^");
		wall_stat = statValueMap.get("AG^174^100^") + "*" + statValueMap.get("AG^174^108^") + "*" + statValueMap.get("AG^174^101^") 
							+ "*" + statValueMap.get("AG^174^107^") + "*" + statValueMap.get("AG^174^102^");
		wall_nready = statValueMap.get("AG^174^103^") + "*" + statValueMap.get("AG^174^104^") + "*" + statValueMap.get("AG^174^105^")
							+ "*" + statValueMap.get("AG^174^106^");
		wall_enter = (String)statValueMap.get("GQ^171^204^");
		wall_answer = (String)statValueMap.get("GQ^171^202^");
		wall_answer_tr = (String)statValueMap.get("GQ^171^203^");
		wall_abn = (String)statValueMap.get("GQ^171^201^");
		wall_abn_sys = (String)statValueMap.get("GQ^171^205^");
		ag_login = (String)statValueMap.get("AG^174^100^");
		ag_ready = (String)statValueMap.get("AG^174^108^");
		ag_talk = objToInt(statValueMap.get("AG^174^101^")) + objToInt(statValueMap.get("AG^174^107^")) + "";
		ag_ib = (String)statValueMap.get("AG^174^101^");
		ag_ob = (String)statValueMap.get("AG^174^107^");
		ag_notready = objToInt(statValueMap.get("AG^174^103^")) + objToInt(statValueMap.get("AG^174^109^")) + objToInt(statValueMap.get("AG^174^104^")) + objToInt(statValueMap.get("AG^174^105^")) + objToInt(statValueMap.get("AG^174^106^")) + "";
		ag_acw = (String)statValueMap.get("AG^174^102^");
		ag_talk_all = (String)statValueMap.get("AG^174^110^");

		wall = "W1@" + wall_readyCall + "^" + "W2@" + wall_stat + "^" + "W3@" + wall_nready + "^" + "W4@" + wall_enter + "^" + "W5@" + wall_answer
					+ "^" + "W6@" + wall_answer_tr + "^" + "W7@" + wall_abn + "^" + "W8@" + wall_abn_sys;
		mallstat = qg_readyCall + "^" + ag_login + "^" + ag_ready + "^" +ag_talk  + "^" + ag_notready + "^" + ag_acw + "^" + ag_talk_all + "^" + ag_ib + "^" + ag_ob;	//대기콜수^로그인수^대기상담사^통화중^이석^후처리^인통화^아웃통화
		
		wallBoardMap.put("MALL_STAT", mallstat);
		
		wallBoardMap.put("MALL_WALL", wall);
		
		CfgPerson cObj = wv.getCurrent_person();
		tmpval = 0;
		a_avg_call = "0";
		a_avg_ready = "0";
		a_avg_acw = "0";
			
		tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^303^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^304^"));
		if(tmpval > 0){
			a_avg_call = (objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^301^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^302^"))) / tmpval + "";
		}

		tmpval = 0;
		tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^306^"));
		if(tmpval > 0){
			a_avg_ready = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^305^")) / tmpval + "";
		}
			
		tmpval = 0;
		tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^308^"));
		if(tmpval > 0){
			a_avg_acw = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^307^")) / tmpval + "";
		}
			
		a_tot_call = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^301^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^302^")) + "";

		a_ib_cnt = (String)statValueMap.get("PS^"+cObj.getEmployeeID()+"^303^");
		a_ob_cnt = (String)statValueMap.get("PS^"+cObj.getEmployeeID()+"^304^");
			
		psstat = a_avg_call + "^" + a_avg_ready + "^" + a_avg_acw + "^" +a_tot_call  + "^" + a_ib_cnt + "^" + a_ob_cnt;	//평균통화^평균대기^평균후처리^누적통화^IB건수^OB건수
			
		wallBoardMap.put("MALL_" + cObj.getEmployeeID(), psstat);
		
		/***********************************************************************/	
		// 새로 추가한거
		/***********************************************************************/		
		currentstat = "";																																		//지속시간					74*		
		@SuppressWarnings("unchecked")																											//상담사ID					2000*	
		HashMap<String,String> tmpMap = (HashMap<String, String>) statValueMap.get("AGENTS");					//상담사 상태, 사유		8*		
		if(tmpMap != null){
			for (Map.Entry<String, String> elem : tmpMap.entrySet()){																		//상담사명					홍준기*	
				currentstat += elem.getValue() + "*" +  statValueMap.get("PS^"+elem.getKey()+"^303^") 	//전화번호					*		
				                       + "*" + statValueMap.get("PS^"+elem.getKey()+"^304^")+ "@";						//내선						1000*	
			}																																								//IB건수						0*		
		}
		if(currentstat.length() > 0){																														//OB건수					0		
			currentstat= currentstat.substring(0, currentstat.length()-1);
		}
		wallBoardMap.put("MALL_CURRENT", currentstat);
//		/***********************************************************************/
//		// 예전꺼
//		/***********************************************************************/				
//		currentstat = (String)statValueMap.get("AGENTS");
//		if(currentstat.length() > 0){
//			currentstat= currentstat.substring(0, currentstat.length()-1);
//		}
//		wallBoardMap.put("MALL_CURRENT", currentstat);
	}

	/**
	 * Real (AJ_Park)
	 */
	private void makeSendData_park(){
	int tmpval = 0;
	String parkstat = "";
	String psstat = "";
	String currentstat = "";
	
	qg_readyCall = objToInt(statValueMap.get("GQ^148^200^")) + objToInt(statValueMap.get("GQ^163^200^")) + objToInt(statValueMap.get("GQ^164^200^")) + "";
	wall_readyCall = statValueMap.get("GQ^148^200^") + "*" + statValueMap.get("GQ^163^200^") + "*" + statValueMap.get("GQ^164^200^");
	wall_stat = statValueMap.get("AG^182^100^") + "*" + statValueMap.get("AG^182^108^") + "*" + statValueMap.get("AG^182^101^") 
						+ "*" + statValueMap.get("AG^182^107^") + "*" + statValueMap.get("AG^182^102^");
	wall_nready = statValueMap.get("AG^182^103^") + "*" + statValueMap.get("AG^182^104^") + "*" + statValueMap.get("AG^182^105^")
						+ "*" + statValueMap.get("AG^182^106^");
	wall_enter = statValueMap.get("GQ^148^204^") + "*" + statValueMap.get("GQ^163^204^") + "*" + statValueMap.get("GQ^164^204^");
	wall_answer = statValueMap.get("GQ^148^202^") + "*" + statValueMap.get("GQ^163^202^") + "*" + statValueMap.get("GQ^164^202^");
	wall_answer_tr = statValueMap.get("GQ^148^203^") + "*" + statValueMap.get("GQ^163^203^") + "*" + statValueMap.get("GQ^164^203^");
	wall_abn = statValueMap.get("GQ^148^201^") + "*" + statValueMap.get("GQ^163^201^") + "*" + statValueMap.get("GQ^164^201^");
	wall_abn_sys = statValueMap.get("GQ^148^205^") + "*" + statValueMap.get("GQ^163^205^") + "*" + statValueMap.get("GQ^164^205^");
	ag_login = (String)statValueMap.get("AG^182^100^");
	ag_ready = (String)statValueMap.get("AG^182^108^");
	ag_talk = objToInt(statValueMap.get("AG^182^101^")) + objToInt(statValueMap.get("AG^182^107^")) + "";
	ag_ib = (String)statValueMap.get("AG^182^101^");
	ag_ob = (String)statValueMap.get("AG^182^107^");
	//ag_notready = objToInt(statValueMap.get("AG^182^109^")) - (objToInt(statValueMap.get("AG^182^103^")) + objToInt(statValueMap.get("AG^182^104^")) + objToInt(statValueMap.get("AG^182^105^")) + objToInt(statValueMap.get("AG^182^106^"))) + "";
	ag_notready = objToInt(statValueMap.get("AG^182^109^")) + "";
	ag_acw = (String)statValueMap.get("AG^182^102^");
	ag_talk_all = (String)statValueMap.get("AG^182^110^");

	wall = "W1@" + wall_readyCall + "^" + "W2@" + wall_stat + "^" + "W3@" + wall_nready + "^" + "W4@" + wall_enter + "^" + "W5@" + wall_answer
				+ "^" + "W6@" + wall_answer_tr + "^" + "W7@" + wall_abn + "^" + "W8@" + wall_abn_sys;
	parkstat = qg_readyCall + "^" + ag_login + "^" + ag_ready + "^" +ag_talk  + "^" + ag_notready + "^" + ag_acw + "^" + ag_talk_all + "^" + ag_ib + "^" + ag_ob;	//대기콜수^로그인수^대기상담사^통화중^이석^후처리^인통화^아웃통화
	wallBoardMap.put("PARK_STAT", parkstat);
	
	wallBoardMap.put("PARK_WALL", wall);
	
	CfgPerson cObj = wv.getCurrent_person();
	tmpval = 0;
	a_avg_call = "0";
	a_avg_ready = "0";
	a_avg_acw = "0";
		
	tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^303^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^304^"));
	if(tmpval > 0){
		a_avg_call = (objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^301^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^302^"))) / tmpval + "";
	}

	tmpval = 0;
	tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^306^"));
	if(tmpval > 0){
		a_avg_ready = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^305^")) / tmpval + "";
	}
		
	tmpval = 0;
	tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^308^"));
	if(tmpval > 0){
		a_avg_acw = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^307^")) / tmpval + "";
	}
		
	a_tot_call = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^301^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^302^")) + "";

	a_ib_cnt = (String)statValueMap.get("PS^"+cObj.getEmployeeID()+"^303^");
	a_ob_cnt = (String)statValueMap.get("PS^"+cObj.getEmployeeID()+"^304^");
		
	psstat = a_avg_call + "^" + a_avg_ready + "^" + a_avg_acw + "^" +a_tot_call  + "^" + a_ib_cnt + "^" + a_ob_cnt;	//평균통화^평균대기^평균후처리^누적통화^IB건수^OB건수
		
	wallBoardMap.put("PARK_" + cObj.getEmployeeID(), psstat);

	
	/***********************************************************************/	
	// 새로 추가한거
	/***********************************************************************/		
	currentstat = "";																																		//지속시간					74*		
	@SuppressWarnings("unchecked")																											//상담사ID					2000*	
	HashMap<String,String> tmpMap = (HashMap<String, String>) statValueMap.get("AGENTS");					//상담사 상태, 사유		8*		
	if(tmpMap != null){
		for (Map.Entry<String, String> elem : tmpMap.entrySet()){																		//상담사명					홍준기*	
			currentstat += elem.getValue() + "*" +  statValueMap.get("PS^"+elem.getKey()+"^303^") 	//전화번호					*		
            + "*" + statValueMap.get("PS^"+elem.getKey()+"^304^")+ "@";						//내선						1000*	

		}																																								//IB건수						0*		
	}
	if(currentstat.length() > 0){																														//OB건수					0		
		currentstat= currentstat.substring(0, currentstat.length()-1);
	}
	wallBoardMap.put("PARK_CURRENT", currentstat);
	}
	

	/**
	 * Real (AJ_Mall)
	 */
//	private void makeSendData_mall(){
//		int tmpval = 0;
//		String wallstat = "";
//		String psstat = "";
//		String currentstat = "";
//		
//		qg_readyCall = objToInt(statValueMap.get("GQ^148^200^")) + "";
//		wall_readyCall = statValueMap.get("GQ^148^200^");
//		wall_stat = statValueMap.get("AG^182^100^") + "*" + statValueMap.get("AG^182^108^") + "*" + statValueMap.get("AG^182^101^") 
//							+ "*" + statValueMap.get("AG^182^107^") + "*" + statValueMap.get("AG^182^102^");
//		wall_nready = statValueMap.get("AG^182^103^") + "*" + statValueMap.get("AG^182^104^") + "*" + statValueMap.get("AG^182^105^")
//							+ "*" + statValueMap.get("AG^182^106^");
//		wall_enter = statValueMap.get("GQ^148^204^");
//		wall_answer = statValueMap.get("GQ^148^202^");
//		wall_answer_tr = statValueMap.get("GQ^148^203^");
//		wall_abn = statValueMap.get("GQ^148^201^");
//		wall_abn_sys = statValueMap.get("GQ^148^205^");
//		ag_login = statValueMap.get("AG^182^100^");
//		ag_ready = statValueMap.get("AG^182^108^");
//		ag_talk = objToInt(statValueMap.get("AG^182^101^")) + objToInt(statValueMap.get("AG^182^107^")) + "";
//		ag_notready = objToInt(statValueMap.get("AG^182^103^")) + objToInt(statValueMap.get("AG^182^109^")) + objToInt(statValueMap.get("AG^182^104^")) + objToInt(statValueMap.get("AG^182^105^")) + objToInt(statValueMap.get("AG^182^106^")) + "";
//		ag_acw = statValueMap.get("AG^182^102^");
//
//		wall = "W1@" + wall_readyCall + "^" + "W2@" + wall_stat + "^" + "W3@" + wall_nready + "^" + "W4@" + wall_enter + "^" + "W5@" + wall_answer
//					+ "^" + "W6@" + wall_answer_tr + "^" + "W7@" + wall_abn + "^" + "W8@" + wall_abn_sys;
//		wallstat = qg_readyCall + "^" + ag_login + "^" + ag_ready + "^" +ag_talk  + "^" + ag_notready + "^" + ag_acw + "^" + ag_talk_all;	//대기콜수^로그인수^대기상담사^통화중^이석^후처리
//		
//		wallBoardMap.put("WALL_STAT", wallstat);
//		
//		wallBoardMap.put("WALL_WALL", wall);
//		
//		for (CfgPerson cObj : arrPerson) {
//			tmpval = 0;
//			a_avg_call = "0";
//			a_avg_ready = "0";
//			a_avg_acw = "0";
//			
//			tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^303^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^304^"));
//			if(tmpval > 0){
//				a_avg_call = (objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^301^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^302^"))) / tmpval + "";
//			}
//
//			tmpval = 0;
//			tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^306^"));
//			if(tmpval > 0){
//				a_avg_ready = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^305^")) / tmpval + "";
//			}
//			
//			tmpval = 0;
//			tmpval = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^308^"));
//			if(tmpval > 0){
//				a_avg_acw = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^307^")) / tmpval + "";
//			}
//			
//			a_tot_call = objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^301^")) + objToInt(statValueMap.get("PS^"+cObj.getEmployeeID()+"^302^")) + "";
//
//			a_ib_cnt = statValueMap.get("PS^"+cObj.getEmployeeID()+"^303^");
//			a_ob_cnt = statValueMap.get("PS^"+cObj.getEmployeeID()+"^304^");
//			
//			psstat = a_avg_call + "^" + a_avg_ready + "^" + a_avg_acw + "^" +a_tot_call  + "^" + a_ib_cnt + "^" + a_ob_cnt;	//평균통화^평균대기^평균후처리^누적통화^IB건수^OB건수
//			
//			wallBoardMap.put("WALL_" + cObj.getEmployeeID(), psstat);
//			currentstat = currentstat + cObj.getEmployeeID() + "*" + statValueMap.get("PS^"+cObj.getEmployeeID()+"^300^") + "@";
//		}
//		if(currentstat.length() > 0){
//			currentstat= currentstat.substring(0, currentstat.length()-1);
//		}
//		wallBoardMap.put("WALL_CURRENT", currentstat);
//	}

	private static int objToInt(Object args){
		int rtn = 0;
		try{
			if(args == null || args.equals("")){
				rtn = 0;
			}else{
				rtn = Integer.valueOf((String) args);
			}
		}catch(NumberFormatException e){
			e.printStackTrace();
		}
		return rtn;
	}	
	
	private String getVal(final KeyValueCollection data, String key){
		String dataString = "";
		try{
			if(data != null){
				for(Object o : data){
					KeyValuePair pair = (KeyValuePair) o;
					ValueType type = pair.getValueType();
					Object value = pair.getValue();
					if(pair.getStringKey().equals(key)){
						if(value != null && (type == ValueType.INT 
								|| type == ValueType.FLOAT 
								|| type == ValueType.STRING 
								|| type == ValueType.WIDE_STRING 
								|| type == ValueType.LONG)){
							dataString = value + "";
						}
					}
				}
			}			
		}catch(Exception e){
		}
		return dataString;
	}

}