package com.eactive.eai.rms.onl.manage.rule.transform2.mapper;

import org.mapstruct.AfterMapping;
import org.mapstruct.BeanMapping;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValuePropertyMappingStrategy;

import com.eactive.eai.data.entity.onl.transformer.transform.TransformEntity;
import com.eactive.eai.data.mapper.BaseMapperConfig;
import com.eactive.eai.data.mapper.GenericMapper;
import com.eactive.eai.rms.onl.manage.rule.transform2.ui.TransformUI;

@Mapper(config = BaseMapperConfig.class, uses = { TransformItemUIMapper.class })
public interface TransformUIMapper extends GenericMapper<TransformUI, TransformEntity> {

	@Mapping(source = "cnvsnname", target = "cnvsnName")
	@Mapping(source = "cnvsndesc", target = "cnvsnDesc")
	@Mapping(source = "lastamndhms", target = "lastamndhms")
	@Mapping(source = "author", target = "author")
	@Mapping(source = "eaisevrdstcd", target = "eaiSevrDStcd")
	@Mapping(source = "modfimgtstusdstcd", target = "modfimgtstusdstcd")
	@Mapping(source = "useyn", target = "useYn")
	@Mapping(source = "verinfo", target = "verInfo")
	TransformUI toVo(TransformEntity entity);

	@Mapping(source = "gridData", target = "transformItemEntities")
	@InheritInverseConfiguration
	TransformEntity toEntity(TransformUI vo);

	@Mapping(source = "gridData", target = "transformItemEntities")
	@InheritInverseConfiguration
	@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
	void updateEntity(TransformUI transformUI,  @MappingTarget TransformEntity transformEntity);
	
	@AfterMapping
	default void after(@MappingTarget TransformEntity entity) {
		if (entity.getTransformItemEntities() == null) {
			return;
		}
		entity.getTransformItemEntities().forEach(item -> {
			item.getId().setCnvsnname(entity.getCnvsnname());
			item.setTransformEntity(entity);
		});
	}
}
