package com.eactive.eai.common.messagekey;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eactive.eai.common.dao.BaseDAO;
import com.eactive.eai.common.dao.DAOException;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.messagekey.loader.MessageKeyLoader;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.data.entity.onl.messagekey.MessageKey;

@Service
@Transactional
public class MessageKeyDAO extends BaseDAO {
	private static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	@Autowired
	private MessageKeyLoader messageKeyLoader;

	public Map<String, MessageKeyVO> getAllMessageKeys() throws DAOException {
		try {
			Map<String, MessageKeyVO> messageKeyMap = new HashMap<>();
			List<MessageKey> messageKeys = messageKeyLoader.loadAllByOrderByDefault();
			if (logger.isInfo())
				logger.info("[ @@ Load MessageKey Configuration - starting >>>>>>>>>>>>>>>>> ]");

			String current = null, previous = null;
			int currentGroup = 0;
			int previousGroup = 0;
			MessageKeyVO msgKey = null;
			MessageKeyGroupVO msgGroup = null;
			for (MessageKey messageKey : messageKeys) {
				current = messageKey.getId().getAdptrbzwkgroupname() + messageKey.getId().getIodstcd();
				currentGroup = messageKey.getId().getGroupinorno();

				if (!current.equals(previous)) {
					msgKey = new MessageKeyVO(current, messageKey.getId().getMsgdstcd(),
							messageKey.getId().getIodstcd());
					messageKeyMap.put(current, msgKey);
					previous = current;
					msgGroup = new MessageKeyGroupVO();
					msgKey.addMessageKeyGroup(msgGroup);
					previousGroup = currentGroup;
				} else {
					if (currentGroup != previousGroup) {
						msgGroup = new MessageKeyGroupVO();
						msgKey.addMessageKeyGroup(msgGroup);
						previousGroup = currentGroup;
					}
				}
				msgGroup.addMessageKeyFld(messageKey.getId().getGroupinorno(), messageKey.getId().getFldprcssno(),
						messageKey.getBzwkfldname(), messageKey.getClsname(),
						messageKey.getMsgfldstartsituval().intValue(), messageKey.getMsgfldlen(),
						messageKey.getNomalprcssctnt());
			}
			if (logger.isInfo()) {
//				messageKeyMap.entrySet().forEach( entry -> logger.info("@ " + entry.getValue().toString()) );
				logger.info("[>>Load MessageKey Configuration - ended ]");
			}
			return messageKeyMap;
		} catch (Exception e) {
			logger.error("getAllMessageKeys Error", e);
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMK101"));
		}
	}

	public MessageKeyVO getMessageKey(String adptrBzwkGroupName, String ioDstcd) throws DAOException {

		try {
			List<MessageKey> messageKeys = messageKeyLoader
					.selectMessageKeyByNameAndIoDstCode(adptrBzwkGroupName, ioDstcd);

			String current = null, previous = null;

			int currentGroup = 0;
			int previousGroup = 0;

			MessageKeyVO msgKey = null;
			MessageKeyGroupVO msgGroup = null;

			for (MessageKey messageKey : messageKeys) {
				current = messageKey.getId().getAdptrbzwkgroupname() + messageKey.getId().getIodstcd();
				currentGroup = messageKey.getId().getGroupinorno();

				if (!current.equals(previous)) {
					msgKey = new MessageKeyVO(current, messageKey.getId().getMsgdstcd(),
							messageKey.getId().getIodstcd());
					previous = current;
					msgGroup = new MessageKeyGroupVO();
					msgKey.addMessageKeyGroup(msgGroup);
					previousGroup = currentGroup;
				} else {
					if (currentGroup != previousGroup) {
						msgGroup = new MessageKeyGroupVO();
						msgKey.addMessageKeyGroup(msgGroup);
						previousGroup = currentGroup;
					}
				}
				msgGroup.addMessageKeyFld(messageKey.getId().getGroupinorno(), messageKey.getId().getFldprcssno(),
						messageKey.getBzwkfldname(), messageKey.getClsname(),
						messageKey.getMsgfldstartsituval().intValue(), messageKey.getMsgfldlen(),
						messageKey.getNomalprcssctnt());
			}
			return msgKey;
		} catch (Exception e) {

			String[] msgArgs = { adptrBzwkGroupName, ioDstcd };
			String errorText = ExceptionUtil.make("RECEAICMK107", msgArgs);
			if (logger.isError())
				logger.error(errorText);
			throw new DAOException(errorText);
		}
	}

}
