package com.eactive.eai.adapter.webt;

import java.util.Properties;

import com.eactive.eai.adapter.AdapterPropManager;
import com.eactive.eai.adapter.listener.AdapterException;
import com.eactive.eai.adapter.listener.AdapterListenerSupport;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.util.Logger;

import tmax.webt.WebtConnectionGroup;
import tmax.webt.WebtConnectionPool;
import tmax.webt.io.WebtConnectionPoolInfo;

public class WebTClientAdapterListener extends AdapterListenerSupport {
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	@Override
	public void start() throws AdapterException {
		AdapterPropManager pmanager = AdapterPropManager.getInstance();
		Properties prop = pmanager.getProperties(info.getPropGroupName());
		int portNumber = -1;
		int max = -1;
		int inc = 2;

		String webtConnGroupLog = "";
		String webtConnInfoLog = "";

		try {
			if (info == null)
				throw new Exception("WebTClientAdapterListener | AdapterVO가 NULL입니다.");
			String hostName = prop.getProperty("host.name");
			try {
				portNumber = Integer.parseInt(prop.getProperty("port.number"));
			} catch (Exception e) {
				throw new Exception(
						"WebTClientAdapterListener | port.number error : " + prop.getProperty("port.number"));
			}
			try {
				max = Integer.parseInt(prop.getProperty("max.connection"));
			} catch (Exception e) {
				max = 10;
				if (logger.isInfo())
					logger.info("WebTClientAdapterListener | max = " + max);
			}

			String name = info.getAdapterGroupName() + "." + info.getName();
			WebtConnectionPool.createGroup(name, hostName, portNumber, max, max, inc);

			WebtConnectionGroup webtConnGroup = WebtConnectionPool.getGroup(name);
			WebtConnectionPool.startMonitor(5, true, false); // 가이드 필요, webt properties에 설정

			if (logger.isInfo()) {
				webtConnGroupLog = "########### Webt Connection Group Info ################ " + "\n[Name] "
						+ webtConnGroup.getName() + "\n[Type]" + webtConnGroup.getType() + "\n[Available conn]"
						+ webtConnGroup.available() + "\n[isActive]" + webtConnGroup.isActive() + "\n[size]"
						+ webtConnGroup.size() + "\n\n";
			}

			WebtConnectionPoolInfo webtConnInfo = webtConnGroup.getConnectionPoolInfo();

			if (logger.isInfo()) {
				webtConnInfoLog = "########### Webt ConnectionPool Info################ " + "\n[Active Connection Num]"
						+ webtConnInfo.getActiveConnectionNum() + "\n[Address Info]" + webtConnInfo.getAddressInfo()
						+ "\n[Init Capacity]" + webtConnInfo.getInitCapacity() + "\n[Max Capacity]"
						+ webtConnInfo.getMaxCapacity() + "\n[Current PoolSize]" + webtConnInfo.getCurrentPoolSize()
						+ "\n[Create Count]" + webtConnInfo.getCreateCount() + "\n[Waiting Count]"
						+ webtConnInfo.getWaitingCount() + "\n[Free Connection Num]"
						+ webtConnInfo.getFreeConnectionNum() + "\n\n";
			}
			if (logger.isInfo())
				logger.info(webtConnGroupLog + webtConnInfoLog);

			info.setStatus(true);
		} catch (Exception e) {
			String errCode = ExceptionUtil.getErrorCode(e, "RECEAIOIA008");
			logger.error("WebtConnectionPoolInfo Failed", e);
			throw new AdapterException(errCode);
		} catch (Throwable t) {
			String errCode = ExceptionUtil.getErrorCode(t, "RECEAIOIA008");
			logger.error("WebtConnectionPoolInfo Failed", t);
			throw new AdapterException(errCode);
		}
	}

	public void stop() throws AdapterException {
		try {
			if (info == null)
				throw new Exception("WebTClientAdapterListener | AdapterVO가 NULL입니다.");

			String name = info.getAdapterGroupName() + "." + info.getName();
			WebtConnectionPool.destroyGroup(name);
			WebtConnectionPool.removeGroup(new String[] { name });
		} catch (Exception e) {
			String errCode = ExceptionUtil.getErrorCode(e, "RECEAIOIA009");
			throw new AdapterException(errCode);
		} catch (Throwable t) {
			String errCode = ExceptionUtil.getErrorCode(t, "RECEAIOIA009");
			logger.error("Start Failed", t);
			throw new AdapterException(errCode);
		}
	}
}