package com.eactive.eai.common.b2bextractor;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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;

@Component
public class B2BExtractManager implements Lifecycle {

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

	private LifecycleSupport lifecycle = new LifecycleSupport(this);

	private Map<String, B2BExtractVO> messages = new HashMap<>();

	private boolean started;

	@Autowired
	B2BExtractDAO b2bExtractDAO;

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

	public void start() throws LifecycleException {
		if (started)
			throw new LifecycleException("RECEAICKM201");

		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 {
		messages = b2bExtractDAO.getAllB2BExtracts();
	}

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

	public synchronized void reload(String eaiSvcCd) throws Exception {
		if (logger.isWarn()) {
			logger.warn("B2BExtractManager] reload Started ...");
		}
		Map<String, B2BExtractVO> map = b2bExtractDAO.getB2BExtract(eaiSvcCd);// eaiSvcCd

		Iterator<String> it = map.keySet().iterator();
		String key = null;

		while (it.hasNext()) {
			key = it.next();
			messages.put(key, map.get(key));
		}

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

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

		lifecycle.fireLifecycleEvent(STOPING_EVENT, this);

		messages.clear();

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

	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 B2BExtractVO getB2BExtract(String code) {
		return messages.get(code);
	}

	public void setB2BExtract(String code, B2BExtractVO msg) {
		messages.put(code, msg);
	}

	public void removeB2BExtract(String code) {
		messages.remove(code);
	}

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

}
