package com.eactive.eai.common.message;

import java.util.ArrayList;
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.message.loader.CondPerRtgInfoLoader;
import com.eactive.eai.common.message.loader.EAIMessageLoader;
import com.eactive.eai.common.message.loader.EAIReferenceMessageLoader;
import com.eactive.eai.common.message.loader.ServiceMessageLoader;
import com.eactive.eai.common.message.mapper.CondPerRtgInfoMapper;
import com.eactive.eai.common.message.mapper.EAIMessageMapper;
import com.eactive.eai.common.message.mapper.ServiceMessageMapper;
import com.eactive.eai.common.util.Logger;
import com.eactive.eai.data.entity.onl.message.CondPerRtgInfoEntity;
import com.eactive.eai.data.entity.onl.message.EAIMessageEntity;
import com.eactive.eai.data.entity.onl.message.EAIReferenceMessage;

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

	@Autowired
	EAIMessageLoader eaiMessageService;

	@Autowired
	EAIMessageMapper eaiMessageMapper;

	@Autowired
	ServiceMessageLoader serviceMessageLoader;

	@Autowired
	ServiceMessageMapper serviceMessageMapper;

	@Autowired
	EAIReferenceMessageLoader eaiReferenceMessageService;

	@Autowired
	CondPerRtgInfoLoader condPerRtgInfoLoader;

	@Autowired
	CondPerRtgInfoMapper condPerRtgInfoMapper;

	public Map<String, EAIMessage> getAllEAIMessages() throws DAOException {
		try {

			List<EAIMessageEntity> eaiMessageEntities = eaiMessageService.findByUseynOrderByEaisvcname("1");
			logger.info("[ @@ Load EAIMessage Configuration - starting >>>>>>>>>>>>>>>>> ]");

			Map<String, EAIMessage> eaiMessages = new HashMap<>();
			for (EAIMessageEntity eaiMessageEntity : eaiMessageEntities) {
				EAIMessage eaiMessage = getEAIMessage(eaiMessageEntity);
				eaiMessages.put(eaiMessage.getEAISvcCd(), eaiMessage);
			}
			return eaiMessages;
		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICMM101"));
		}
	}

	private EAIMessage getEAIMessage(EAIMessageEntity eaiMessageEntity) {
		EAIMessage eaiMessage = eaiMessageMapper.toVo(eaiMessageEntity);

		mapEaiReference(eaiMessageEntity, eaiMessage);

		mapCondPerRtgInfo(eaiMessageEntity, eaiMessage);
		return eaiMessage;
	}

	private void mapCondPerRtgInfo(EAIMessageEntity eaiMessageEntity, EAIMessage eaiMessage) {

		eaiMessageEntity.getServiceMessages().stream().forEach(serviceMessageEntity -> {
			List<CondPerRtgInfoEntity> condPerRtgInfoEntities = serviceMessageEntity.getCondPerRtgInfos();

			List<CondPerRtgInfo> condPerRtgInfos = new ArrayList<>();
			condPerRtgInfoEntities.forEach(condPerRtgInfoEntity -> {
				CondPerRtgInfo condPerRtgInfo = new CondPerRtgInfo();
				condPerRtgInfos.add(condPerRtgInfo);
			});

			ServiceMessage serviceMessage = eaiMessage.getSvcMsg(serviceMessageEntity.getId().getSvcprcssno() - 1);
			serviceMessage.setCondPerRtgInfo(condPerRtgInfos);
		});
	}

	private void mapEaiReference(EAIMessageEntity eaiMessageEntity, EAIMessage eaiMessage) {

		eaiMessageEntity.getServiceMessages().stream().forEach(serviceMessageEntity -> {
			List<EAIReferenceMessage> eaiReferenceMessages = serviceMessageEntity.getEaiReferenceMessages();

			String[] refmsgidnames = eaiReferenceMessages.stream().map(EAIReferenceMessage::getRefmsgidname)
					.toArray(String[]::new);

			ServiceMessage serviceMessage = eaiMessage.getSvcMsg(serviceMessageEntity.getId().getSvcprcssno() - 1);
			serviceMessage.setRefMsgIDs(refmsgidnames);
		});
	}

	public EAIMessage getEAIMessagesByCode(String code) {
		EAIMessageEntity eaiMessageEntity = eaiMessageService.getById(code);
		return getEAIMessage(eaiMessageEntity);
	}

}
