package com.kbstar.eai.common.businessday;

import java.util.Map;

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

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;

// 일자전환 변경대상목록 정보를 DB로 부터 로딩해 메모리에 관리

@Component
public class BusinessDayManager implements Lifecycle {
	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	@Autowired
	private BusinessDayDAO businessDayDAO;

	private Map<String, BusinessDayVO> messages;
	private LifecycleSupport lifecycle = new LifecycleSupport(this);
	private boolean started;

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

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

		lifecycle.fireLifecycleEvent(STARTING_EVENT, this);
		messages = null;
		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 = businessDayDAO.getAllMessages();
	}

	public synchronized void reload() throws Exception {
		if (logger.isWarn()) {
			logger.warn("BusinessDayManager] reload all Started ...");
		}
		init();
		if (logger.isWarn()) {
			logger.warn("BusinessDayManager] 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 BusinessDayVO getMessageVO(String eAISvcName) {
		if (eAISvcName == null)
			return null;
		return messages.get(eAISvcName);
	}

	public void setMessage(String eAISvcName, BusinessDayVO vo) {
		messages.put(eAISvcName, vo);
	}

	public void setMessage(BusinessDayVO vo) {
		messages.put(vo.getEAISvcName(), vo);
	}

	public void removeMessage(String eAISvcName) {
		messages.remove(eAISvcName);
	}

	public Map<String, BusinessDayVO> getAllBusinessDays() {
		return this.messages;
	}

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