package com.eactive.eai.transformer.layout.mapper;

import org.mapstruct.AfterMapping;
import org.mapstruct.InheritConfiguration;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.ReportingPolicy;

import com.eactive.eai.data.entity.onl.transformer.layout.LayoutEntity;
import com.eactive.eai.data.mapper.GenericMapper;
import com.eactive.eai.transformer.layout.Layout;
import com.eactive.eai.transformer.message.Message;

@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE, uses = { LayoutTypeMapper.class,
		LayoutItemMapper.class })
public interface LayoutMapper extends GenericMapper<Layout, LayoutEntity> {

	@Mapping(source = "loutname", target = "name")
	@Mapping(source = "loutdesc", target = "description")
	@Mapping(source = "loutptrnname", target = "layoutType")
	@Mapping(source = "layoutItemEntities", target = "rootItem")
	@Mapping(source = "useyn", target = "useYn")
	@Override
	Layout toVo(LayoutEntity entity);

	@InheritInverseConfiguration(name = "toVo")
	@Mapping(source = "lastUpdateDate", target = "lastamndhms")
	@Mapping(source = "bzwkDstcd", target = "eaibzwkdstcd")
	@Mapping(source = "applicationName", target = "uapplname")
	@Mapping(source = "systemInterface", target = "sysintfacname")
	@Mapping(source = "eaiSvrDstcd", target = "eaisevrdstcd")
	@Mapping(source = "modifImgTstusDstcd", target = "modfimgtstusdstcd")
	@Mapping(source = "useYn", target = "useyn")
	@Override
	LayoutEntity toEntity(Layout vo);

	@InheritConfiguration(name = "toEntity")
	void updateLayoutEntity(Layout vo, @MappingTarget LayoutEntity entity);

	@InheritConfiguration(name = "toVo")
	void updateLayout(LayoutEntity entity, @MappingTarget Layout vo);

	/**
	 * LayoutItemEntity 에 LayoutEntity를 넣는다
	 * 
	 * @param vo
	 * @param entity
	 */
	@AfterMapping
	default void mapEntityAfterMappingLayoutItemEntities(Layout vo, @MappingTarget LayoutEntity entity) {
		entity.getLayoutItemEntities().forEach(layoutItemEntity -> layoutItemEntity.setLayoutEntity(entity));
	}

	/**
	 * RootItem에 layout을 넣는다
	 */
	@AfterMapping
	default void mapLayoutAfterMappingRootItem(LayoutEntity entity, @MappingTarget Layout vo) {
		((Message) vo.getRootItem()).setLayout(vo);
	}

}
