package com.eactive.eai.rms.common.combo;

import com.eactive.apim.portal.approval.entity.ApprovalType;
import com.eactive.apim.portal.monitoringcode.entity.MonitoringCode;
import com.eactive.apim.portal.portalorg.entity.PortalOrg;
import com.eactive.eai.data.entity.onl.adapter.AdapterGroup;
import com.eactive.eai.data.entity.onl.adapter.AdapterType;
import com.eactive.eai.data.entity.onl.authserver.ClientEntity;
import com.eactive.eai.data.entity.onl.b2bservice.B2BService;
import com.eactive.eai.data.entity.onl.routing.Routing;
import com.eactive.eai.data.entity.onl.server.EAIServer;
import com.eactive.eai.data.entity.onl.transformer.function.Function;
import com.eactive.eai.data.entity.onl.transformer.layout.LayoutTypeEntity;
import com.eactive.eai.data.entity.onl.transformer.transform.TransformEntity;
import com.eactive.eai.data.entity.onl.unifbwk.UnifBwkTp;
import com.eactive.eai.data.mapper.BaseMapperConfig;
import com.eactive.eai.data.mapper.StringTrimMapper;
import com.eactive.eai.rms.common.spring.LocaleMessage;
import com.eactive.eai.rms.data.entity.onl.adapter.CommonDomain;
import com.eactive.eai.rms.data.entity.onl.code.Code;
import org.mapstruct.*;
import org.springframework.beans.factory.annotation.Autowired;

@Mapper(config = BaseMapperConfig.class, componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
abstract class ComboMapper {

	private LocaleMessage localeMessage;

	@Mapping(source = "id.code", target = "code")
	public abstract ComboVo toCombo(Code entity);

	@AfterMapping
	public void toVoAfter(Code entity, @MappingTarget ComboVo vo) {
		String language = localeMessage.getLocale().getLanguage();
		vo.setCodename(entity.getCodeName(language));
	}

	@Mapping(source = "adptrcd", target = "code")
	@Mapping(source = "adptrname", target = "codename")
	public abstract ComboVo toCombo(AdapterType entity);

	@Mapping(source = "eaisevrinstncname", target = "code")
	@Mapping(source = "eaisevrinstncname", target = "codename")
	public abstract ComboVo toCombo(EAIServer entity);

	@Mapping(source = "eaibzwkdstcd", target = "code")
	@Mapping(source = "bzwkdsticname", target = "codename")
	public abstract ComboVo toCombo(UnifBwkTp entity);

	@Mapping(source = "applcd", target = "code", qualifiedBy = StringTrimMapper.class)
	@Mapping(source = "bzwkdsticname", target = "codename")
	public abstract ComboVo toComboAppl(UnifBwkTp entity);

	@Mapping(source = "routname", target = "code")
	@Mapping(source = "routname", target = "codename")
	public abstract ComboVo toCombo(Routing entity);

	@Mapping(source = "id.code", target = "code")
	@Mapping(source = "codeName", target = "codename")
	public abstract ComboVo toCombo(MonitoringCode entity);

	@Mapping(source = "loutptrnname", target = "code")
	@Mapping(source = "loutptrndesc", target = "codename")
	public abstract ComboVo toCombo(LayoutTypeEntity entity);

	@Mapping(source = "cnvsnfuntnname", target = "code")
	@Mapping(source = "cnvsnfuntndesc", target = "codename")
	public abstract ComboVo toCombo(Function entity);

	@Mapping(source = "cnvsnname", target = "code")
	@Mapping(source = "cnvsndesc", target = "codename")
	public abstract ComboVo toCombo(TransformEntity entity);

	@Mapping(source = "adptrbzwkgroupname", target = "code")
	@Mapping(source = "adptrbzwkgroupdesc", target = "codename")
	public abstract ComboVo toCombo(AdapterGroup entity);

	@Mapping(source = "domainNm", target = "code")
	@Mapping(source = "domainNm", target = "codename")
	public abstract ComboVo toCombo(CommonDomain entity);

	@Mapping(source = "id.extnlinstiidnfiname", target = "code")
	@Mapping(source = "id.extnlinstiidnfiname", target = "codename")
	public abstract ComboVo toCombo(B2BService entity);

	@Mapping(source = "id", target = "code")
	@Mapping(expression = "java(entity.getOrgName() + \" (\" + entity.getOrgCode() + \")\")", target = "codename")
	public abstract ComboVo toCombo(PortalOrg entity);

	@Mapping(expression = "java(approvalType.name())", target = "code")
	@Mapping(source = "description", target = "codename")
	public abstract ComboVo toCombo(ApprovalType approvalType);

	@Mapping(source = "clientid", target = "code")
	@Mapping(source = "clientname", target = "codename")
	public abstract ComboVo toCombo(ClientEntity entity);

	@Autowired
	public void setLocaleMessage(LocaleMessage localeMessage) {
		this.localeMessage = localeMessage;
	}

}
