package com.eactive.eai.rms.onl.manage.serviceinfo.stdmessage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

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.stdmessage.StandardMessageInfo;
import com.eactive.eai.data.mapper.BaseMapperConfig;
import com.eactive.eai.data.mapper.GenericMapper;

@Mapper(config = BaseMapperConfig.class, uses = { StdMessageItemUIMapper.class,
		ExtendedColumnDefinitionUIMapper.class })
public interface StdMessageUIMapper extends GenericMapper<StdMessageUI, StandardMessageInfo> {

	@Mapping(source = "bzwksvckeyname", target = "bzwkSvcKeyName")
	@Mapping(source = "eaisvcname", target = "eaiSvcName")
	@Mapping(source = "eaitranname", target = "eaiTranName")
	@Mapping(source = "eaisendrecv", target = "eaiSendRecv")
	@Mapping(source = "eaidirection", target = "eaiDirection")
	@Override
	StdMessageUI toVo(StandardMessageInfo entity);

	@InheritInverseConfiguration
	@Override
	StandardMessageInfo toEntity(StdMessageUI vo);

	@InheritInverseConfiguration
	@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
	void updateEntity(StdMessageUI stdMessageUI, @MappingTarget StandardMessageInfo standardMessageInfo);

	default List<StdMessageItemUI> map(Map<String, Object> paramMap) {
		List<StdMessageItemUI> standardMessageItems = new ArrayList<>();
		String bzwkSvcKeyName = (String) paramMap.get("bzwkSvcKeyName");

		for (Entry<String, Object> entry : paramMap.entrySet()) {
			if (entry.getKey().split("[.]").length > 1) {
				String columnName = entry.getKey();
				String columnValue = (String) entry.getValue();
				if (columnValue == null || "".equals(columnValue.trim())) {
					columnValue = "";
				}

				StdMessageItemUI stdMessageItemUI = new StdMessageItemUI();
				stdMessageItemUI.setBzwksvckeyname(bzwkSvcKeyName);
				stdMessageItemUI.setColumnname(columnName);
				stdMessageItemUI.setColumnvalue(columnValue);

				standardMessageItems.add(stdMessageItemUI);
			}
		}

		return standardMessageItems;
	}
}
