package com.eactive.eai.rms.onl.manage.serviceinfo.eaimsg.mapping;

import org.apache.commons.lang3.StringUtils;
import org.mapstruct.BeanMapping;
import org.mapstruct.InheritConfiguration;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;
import org.mapstruct.NullValuePropertyMappingStrategy;
import org.springframework.beans.factory.annotation.Autowired;

import com.eactive.eai.data.entity.onl.message.EAIMessageEntity;
import com.eactive.eai.data.mapper.BaseMapperConfig;
import com.eactive.eai.data.mapper.GenericMapper;
import com.eactive.eai.rms.data.entity.onl.code.CodeService;
import com.eactive.eai.rms.onl.manage.serviceinfo.eaimsg.ui.EAIMessageUI;

@Mapper(config = BaseMapperConfig.class, uses = { ServiceMessageUIMapper.class })
public abstract class EAIMessageUIMapper implements GenericMapper<EAIMessageUI, EAIMessageEntity> {

	@Autowired
	private CodeService codeService;

	@Mapping(source = "eaisvcname", target = "eaiSvcName")
	@Mapping(source = "author", target = "author")
	@Mapping(source = "dmnderrchngidname", target = "dmndErrChngIdName")
	@Mapping(source = "dmnderrfldname", target = "dmndErrFldName")
	@Mapping(source = "eaibzwkdstcd", target = "eaiBzwkDstcd")
	@Mapping(source = "eaigroupcocd", target = "eaiGroupCoCd")
	@Mapping(source = "eailastamndyms", target = "eaiLastAmndYms")
	@Mapping(source = "eaisevrdstcd", target = "eaiSevrDstCd")
	@Mapping(source = "eaisvcdesc", target = "eaiSvcDesc")
	@Mapping(source = "erreaisvcname", target = "errEAISvcName")
	@Mapping(source = "extbizcd", target = "extBizCd")
	@Mapping(source = "flowctrlroutname", target = "flowCtrlRoutName")
	@Mapping(source = "gstatsysadptrbzwkgroupname", target = "gstatSysAdptrBzwkGroupName")
	@Mapping(source = "internalexternaldvcd", target = "internalExternalDvcd")
	@Mapping(source = "intgradsticname", target = "intgraDsticName")
	@Mapping(source = "modfimgtstusdstcd", target = "modfiMgtStusDstCd")
	@Mapping(source = "refkey", target = "refKey")
	@Mapping(source = "rspnserrchngidname", target = "rspnsErrChngIdName")
	@Mapping(source = "rspnserrfldname", target = "rspnsErrFldName")
	@Mapping(source = "sevrloglvelno", target = "sevrLogLvelNo")
	@Mapping(source = "svcbfclmnlogyn", target = "svcBfClmnLogYn")
	@Mapping(source = "svchmseonot", target = "svcHmsEonot")
	@Mapping(source = "svcloglvelno", target = "svcLogLvelNo")
	@Mapping(source = "svcmotivusedstcd", target = "svcMotivUseDstcd")
	@Mapping(source = "svcprcesdsticname", target = "svcPrcesDsticName")
	@Mapping(source = "trantype", target = "tranType")
	@Mapping(source = "txid", target = "txId")
	@Mapping(source = "useyn", target = "useYn")
	@Mapping(source = "verinfo", target = "verInfo")
	@Mapping(source = "trantype", target = "tranTypeCode", qualifiedByName = "getTranCodeType")
	@Override
	public abstract EAIMessageUI toVo(EAIMessageEntity entity);

	@InheritInverseConfiguration(name = "toVo")
	@Override
	public abstract EAIMessageEntity toEntity(EAIMessageUI vo);

	@Named("getTranCodeType")
	public String getTranCodeType(String code) {
		if (StringUtils.isBlank(code)) {
			return null;
		}
		return codeService.getTranTypeCode(code);
	}

	@InheritConfiguration
	@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
	public abstract void updateEntity(EAIMessageUI eaiMessageUI, @MappingTarget EAIMessageEntity eaiMessageEntity);

	@InheritConfiguration
	@Mapping(target = "serviceMessages", ignore = true)
	public abstract EAIMessageUI toVoWithoutChildren(EAIMessageEntity eaiMessageEntity);

}
