package com.eactive.eai.common.restrict;

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

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

import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.dao.DAOFactory;
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 RestrictInfoManager implements Lifecycle {
	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	private static RestrictInfoManager instance = new RestrictInfoManager();

	private Map<String, RestrictInfoVO> messages;
	
	private LifecycleSupport lifecycle = new LifecycleSupport(this);
	private boolean started;
	
	@Autowired
	RestrictInfoDAO restrictInfoDAO;
	
	public static RestrictInfoManager getInstance() {
		return ApplicationContextProvider.getContext().getBean(RestrictInfoManager.class);
	}
	
	public void addResrtictLog(String bzwkDstcd, String msgDpstYMS, String eAISvcSerno, String eAISevrInstncName,
			String eAISvcName, String eAIOsidInstiCd, String eAICtrlNameDstcd) throws DAOException {
		restrictInfoDAO.addResrtictLog(bzwkDstcd, msgDpstYMS, eAISvcSerno, eAISevrInstncName, eAISvcName, eAIOsidInstiCd, eAICtrlNameDstcd);
	}
	
	public void start() throws LifecycleException {
		if (started)
			throw new LifecycleException("RECEAICIM201");
		lifecycle.fireLifecycleEvent(STARTING_EVENT, this);
		try {
			init();
		} catch (Exception e) {
			throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAICIM202"));
		}

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

	private void init() throws Exception {		
		messages = restrictInfoDAO.getAllMessages();
	}

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

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

		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 RestrictInfoVO getRestrictInfoVO(String keyCode) {
		if (keyCode == null)
			return null;

		RestrictInfoVO vo = messages.get(keyCode);
		return vo;
	}

	public void setRestrictInfoVO(String keyCode, RestrictInfoVO vo) {
		messages.put(keyCode, vo);
	}

	public void setRestrictInfoVO(RestrictInfoVO vo) {
		messages.put(vo.getEAICtrlName(), vo);
	}

	public void removeRestrictInfoVO(String keyCode) {
		messages.remove(keyCode);
	}

	public void clearRestrictInfoVO() {
		messages.clear();
	}

	public Map<String, RestrictInfoVO> getAllRestrictInfos() {
		return this.messages;
	}

	public String[] getAllCodes() {
		return messages.keySet().toArray(new String[0]);		
	}
}
