package com.eactive.eai.common.util;

import java.util.List;

import com.eactive.eai.common.c2rservice.C2RServiceManager;
import com.eactive.eai.common.c2rservice.C2RServiceVO;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.message.EAIMessage;
import com.eactive.eai.common.message.ServiceMessage;

// B2BRouterFCProcess 에서 기관코드/업무데이터에 맞는 Service 정보 설정
public final class C2RServiceMapper {
	static Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);

	private C2RServiceMapper() {

	}

	// 기관코드에 해당하는 서비스메시지를 EAI메시지에 설정
	public static EAIMessage setC2RService(String c2rRequestSystemCd, EAIMessage eaiMsg) throws Exception {
		try {

            List<ServiceMessage> svcs = eaiMsg.getSvcMsgs();
            C2RServiceManager manager = C2RServiceManager.getInstance();

            if (c2rRequestSystemCd != null)
                c2rRequestSystemCd = c2rRequestSystemCd.trim();

            for (int p = 0; p < svcs.size(); p++) {

                C2RServiceVO c2rService = manager.getC2RServiceVO(c2rRequestSystemCd, p + 1);
                if (c2rService != null) {
                    ServiceMessage svcMsg = eaiMsg.getSvcMsg(p);
                    // Service Data Mapping
                    setServiceData(svcMsg, c2rService);
                    eaiMsg.setSvcMsgs(p, svcMsg);
                    return eaiMsg;
                }
            }

            return null;
        } catch (Exception e) {
            // B2B 기관코드에 해당하는 서비스메시지 설정 중 오류가 발생.
            logger.error("C2RServiceMapper]" + e.getMessage(), e);
            throw new Exception(ExceptionUtil.getErrorCode(e, "RECEAICBS001"));
        }
    }

    private static void setServiceData(ServiceMessage svcMsg, C2RServiceVO c2rService) {
        if (c2rService.getPsvItfTp() != null)
            svcMsg.setPsvItfTp(c2rService.getPsvItfTp());			// 수동인터페이스유형[TSEAIHE04.PsvIntfacDsticName]
        if (c2rService.getPsvBwkSysNm() != null)
            svcMsg.setPsvBwkSysNm(c2rService.getPsvBwkSysNm());		// 수동업무시스템명[TSEAIHE04.PsvSysbzwkdstcd]
        if (c2rService.getPsvSysID() != null)
            svcMsg.setPsvSysID(c2rService.getPsvSysID());			// 수동시스템ID명[TSEAIHE04.PsvSysIDName]
        if (c2rService.getPsvSysSvcCd() != null)
            svcMsg.setPsvSysSvcCd(c2rService.getPsvSysSvcCd());		// 수동시스템서비스구분명[TSEAIHE04.PsvSysSvcDsticName]
        if (c2rService.getPsvSysItfTp() != null)
            svcMsg.setPsvSysItfTp(c2rService.getPsvSysItfTp());		// 수동시스템어댑터업무그룹명[TSEAIHE04.PsvSysAdptrBzwkGroupName]
        if (c2rService.getFlOvrCls() != null)
            svcMsg.setFlOvrCls(c2rService.getFlOvrCls());			// 장애극복여부[TSEAIHE04.FlovrYn]
        if (c2rService.getOutbRtnNm() != null)
            svcMsg.setOutbRtnNm(c2rService.getOutbRtnNm());			// 아웃바운드라우팅명[TSEAIHE04.OutbndRoutName]
        svcMsg.setTmoVl(c2rService.getTmoVl());						// 타임아웃값[TSEAIHE04.ToutVal]
        if (c2rService.getCpnsSvcPssCd() != null)
            svcMsg.setCpnsSvcPssCd(c2rService.getCpnsSvcPssCd());	// 보상서비스처리코드[TSEAIHE04.CmpenSvcPrcssDsticName]
        if (c2rService.getSupplDelYn() != null)
            svcMsg.setSupplDelYn(c2rService.getSupplDelYn());		// 추가삭제여부[TSEAIHE02.SupplDelYn] -[Y/N]
        if (c2rService.getHdrCtrlDstcd() != null)
            svcMsg.setHdrCtrlDstcd(c2rService.getHdrCtrlDstcd());	// 헤더제어구분코드[TSEAIHE02.HdrCtrlDstcd]-[01/02]삭제제추가/추가삭제
        if (c2rService.getHdrRefClsName() != null)
            svcMsg.setHdrRefClsName(c2rService.getHdrRefClsName()); // 참고클래스명[TSEAIHE02.HdrRefClsName]
    }
}
