package com.eactive.eai.common.b2badaptermapping;

import java.util.Collection;
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.b2badaptermapping.loader.B2BAdapterMapLoader;
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.util.Logger;
import com.eactive.eai.data.entity.onl.b2badaptermapping.B2BAdapterMap;

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

	@Autowired
	private B2BAdapterMapLoader b2bAdapterMapEntityService;

	public Map<String, B2BAdapterMapVO> getAllB2BAdapterMaps(String eaiInstCd) throws DAOException {
		try {

			List<B2BAdapterMap> adapterMaps = b2bAdapterMapEntityService
					.findByIdEaisevrinstncnameOrderByDefault(eaiInstCd);
			Map<String, B2BAdapterMapVO> msgKeys = new HashMap<>();
			logger.info("[ @@ Load B2BAdapterMap Configuration - starting >>>>>>>>>>>>>>>>> ]");

			for (B2BAdapterMap adapterMap : adapterMaps) {
				B2BAdapterMapVO vo = new B2BAdapterMapVO();
				vo.setEaiInstCd(eaiInstCd);
				vo.setEaiSvcCd(adapterMap.getId().getEaisvcname());

				msgKeys.put(adapterMap.getId().getEaisvcname(), vo);

				vo.addB2BAdapterMapFld(	adapterMap.getId().getExtnlinstiidnfiname(), 
										adapterMap.getId().getDmnddtalsbzwkdsticname(), 
										adapterMap.getPsvsysadptrbzwkgroupname(), 
										adapterMap.getAdptrbzwkname());

				logger.info("B2BAdapterMapFldVO in getAllB2BAdapterMaps():");
				for (B2BAdapterMapFldVO fldVO : vo.getB2BAdapterMapFlds()) {
					logger.info("@ B2BAdapterMapFld toString >> " + fldVO.toString());
				}
			}
			return msgKeys;

		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICBA101"));
		}
	}

	public Map<String, B2BAdapterMapVO> getB2BAdapterMap(String eaiInstCd, String eaiSvcCd) throws DAOException {
		try {

			HashMap<String, B2BAdapterMapVO> msgKeys = new HashMap<>();
			Collection<B2BAdapterMap> adapterMaps = b2bAdapterMapEntityService
					.findByIdEaisevrinstncnameAndIdEaisvcname(eaiInstCd, eaiSvcCd);
			logger.info("[ @@ Load B2BAdapterMap Configuration - starting >>>>>>>>>>>>>>>>> ]");

			for (B2BAdapterMap adapterMap : adapterMaps) {
				B2BAdapterMapVO vo = new B2BAdapterMapVO();

				msgKeys.put(eaiSvcCd, vo);

				vo.addB2BAdapterMapFld(	adapterMap.getId().getExtnlinstiidnfiname(), 
										adapterMap.getId().getDmnddtalsbzwkdsticname(), 
										adapterMap.getPsvsysadptrbzwkgroupname(), 
										adapterMap.getAdptrbzwkname());

				logger.info("B2BAdapterMapFldVO in getB2BAdapterMap():");
				for (B2BAdapterMapFldVO fldVO : vo.getB2BAdapterMapFlds()) {
					logger.info("@ B2BAdapterMapFld getB2BAdapterMap >> " + fldVO.toString());
				}
			}

			logger.info("[>>Load B2BAdapterMap Configuration - ended ]");
			return msgKeys;

		} catch (Exception e) {
			throw new DAOException(ExceptionUtil.getErrorCode(e, "RECEAICBA205"));
		}
	}

}
