package com.eactive.eai.flowcontrol.jms;

import java.util.HashMap;
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.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 QueueMonitorManager implements Lifecycle {
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	private Map<String, QueueMonitorVO> queueVos = new HashMap<>();

	@Autowired
	QueueMonitorDAO queueMonitorDAO;

	private LifecycleSupport lifecycle = new LifecycleSupport(this);

	private boolean started;

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

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

		lifecycle.fireLifecycleEvent(STARTING_EVENT, this);

		try {
			queueVos = queueMonitorDAO.getAllQueue();
		} catch (DAOException e) {
			throw new LifecycleException(ExceptionUtil.getErrorCode(e, "RECEAICFM202"));
		}
		started = true;
		// Notify our interested LifecycleListeners
		lifecycle.fireLifecycleEvent(STARTED_EVENT, this);
	}

	public void stop() throws LifecycleException {
		// Validate and update our current component state
		if (!started)
			throw new LifecycleException("RECEAICFM203");

		// Notify our interested LifecycleListeners
		lifecycle.fireLifecycleEvent(STOPING_EVENT, this);

		queueVos.clear();
		started = false;
		// Notify our interested LifecycleListeners
		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 QueueMonitorVO getQueueMonitor(String qName) {
		if (qName == null)
			return null;
		return queueVos.get(qName);
	}

	public void setQueueMonitor(QueueMonitorVO vo) throws Exception {
		try {
			QueueMonitorVO queueVo = queueVos.get(vo.getQueueName());
			if (queueVo == null) {
				queueVos.put(vo.getQueueName(), vo);
			} else {
				queueVo.setMaxCount(vo.getMaxCount());

				queueVo.setQueBzwkCtnt(vo.getQueBzwkCtnt());
				queueVo.setQueUseYn(vo.getQueUseYn());
				queueVo.setQueUsagDstcd(vo.getQueUsagDstcd());
				queueVo.setBzwkDstcd(vo.getBzwkDstcd());
				queueVo.setQueBzwkDtalsCtnt(vo.getQueBzwkDtalsCtnt());
				queueVo.setSevrInstncName(vo.getSevrInstncName());
				queueVos.put(queueVo.getQueueName(), queueVo);

			}
		} catch (Exception e) {
			String message = "QueueMonitorManager] Error Occurred while setQueueMonitor[" + vo.getQueueName() + "] - "
					+ e.getMessage();
			logger.error(message, e);
			throw new Exception(message);
		}
	}

	public void removeQueueMonitor(String qName) throws Exception {
		try {
			queueVos.remove(qName);
		} catch (Exception e) {
			String message = "QueueMonitorManager] Error Occurred while remove[" + qName + "] - " + e.getMessage()
					+ "] - " + e.getMessage();
			logger.error(message, e);
			throw new Exception(message);
		}
	}

	public Map<String, QueueMonitorVO> getAllQueueMonitorVos() {
		return this.queueVos;
	}

}
