package com.eactive.eai.httpouttlsinfo;

import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.lifecycle.Lifecycle;
import com.eactive.eai.common.lifecycle.LifecycleException;
import com.eactive.eai.common.lifecycle.LifecycleListener;
import com.eactive.eai.common.lifecycle.LifecycleSupport;
import com.eactive.eai.common.util.ApplicationContextProvider;
import com.eactive.eai.common.util.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class HttpOutTlsInfoManager implements Lifecycle {

	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	private Map<String, HttpOutTlsInfoVO> httpOutTlsInfoMap = new HashMap<>();

	private LifecycleSupport lifecycle = new LifecycleSupport(this);

	private boolean started;

	@Autowired
	private HttpOutTlsInfoDAO httpOutTlsInfoDAO;

	public static HttpOutTlsInfoManager getInstance() {
		return ApplicationContextProvider.getContext().getBean(HttpOutTlsInfoManager.class);
	}

	@Override
	public void start() throws LifecycleException {
		if (started)
			throw new LifecycleException("RECEAIAAC015");

		lifecycle.fireLifecycleEvent(STARTING_EVENT, this);

		try {
			init();
		} catch (Exception e) {
			throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAICKM202"));
		}

		started = true;
		lifecycle.fireLifecycleEvent(STARTED_EVENT, this);
	}

	private void init() throws DAOException {
		httpOutTlsInfoMap.clear();
		httpOutTlsInfoMap.putAll(httpOutTlsInfoDAO.getAllHttpOutTlsInfos());
	}


	@Override
	public void stop() throws LifecycleException {
		if (!started)
			throw new LifecycleException("RECEAICKM203");

		lifecycle.fireLifecycleEvent(STOPING_EVENT, this);

		httpOutTlsInfoMap.clear();

		started = false;
		lifecycle.fireLifecycleEvent(STOPPED_EVENT, this);
	}

	public synchronized void reload() throws Exception {
		if (logger.isWarn()) {
			logger.warn("HttpOutTlsInfoManager] reload all Started ...");
		}
		init();
		if (logger.isWarn()) {
			logger.warn("HttpOutTlsInfoManager] reload all finished ...");
		}
	}

	public synchronized void reload(String clientId) throws Exception {
		if (logger.isWarn()) {
			logger.warn("HttpOutTlsInfoManager] reload Started ...");
		}
		HttpOutTlsInfoVO httpOutTlsInfoVO = httpOutTlsInfoDAO.getHttpOutTlsInfo(clientId);// eaiSvcCd
		httpOutTlsInfoMap.put(clientId, httpOutTlsInfoVO);

		if (logger.isWarn()) {
			logger.warn("HttpOutTlsInfoManager] reload finished ...");
		}
	}

	public void addLifecycleListener(LifecycleListener listener) {
		lifecycle.addLifecycleListener(listener);
	}

	public LifecycleListener[] findLifecycleListeners() {
		return lifecycle.findLifecycleListeners();
	}

	public void removeLifecycleListener(LifecycleListener listener) {
		lifecycle.removeLifecycleListener(listener);
	}

	public boolean isStarted() {
		return this.started;
	}

	public HttpOutTlsInfoVO getHttpOutTlsInfo(String clientId) {
		return httpOutTlsInfoMap.get(clientId);
	}

	public void setHttpOutTlsInfo(String clientId, HttpOutTlsInfoVO httpOutTlsInfoVO) {
		httpOutTlsInfoMap.put(clientId, httpOutTlsInfoVO);
	}

	public void removeHttpOutTlsInfo(String clientId) {
		httpOutTlsInfoMap.remove(clientId);
	}

	public String[] getAllClientIds() {
		Set<String> keySet = this.httpOutTlsInfoMap.keySet();
		String[] names = keySet.toArray(new String[keySet.size()]);
		Arrays.sort(names);
		return names;
	}
}
