package com.eactive.eai.rms.onl.loader.intf.mapper;

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.bizkey.BizKeyService;
import com.eactive.eai.rms.data.entity.onl.stdmessage.StandardMessageInfoService;
import com.eactive.eai.rms.data.entity.onl.submessage.SubMessageInfoService;
import com.eactive.eai.rms.onl.loader.intf.deploy.BizKeyDeploy;
import com.eactive.eai.rms.onl.loader.intf.deploy.EAIMessageDeploy;
import com.eactive.eai.rms.onl.loader.intf.deploy.StandardMessageInfoDeploy;
import com.eactive.eai.rms.onl.loader.intf.deploy.SubMessageInfoDeploy;
import org.mapstruct.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;

@Mapper(config = BaseMapperConfig.class, uses = ServiceMessageDeployMapper.class)
public abstract class EAIMessageDeployMapper implements GenericMapper<EAIMessageDeploy, EAIMessageEntity> {

    @Autowired private BizKeyService bizKeyService;
    @Autowired private BizKeyDeployMapper bizKeyDeployMapper;
    @Autowired private StandardMessageInfoService standardMessageInfoService;
    @Autowired private StandardMessageInfoDeployMapper standardMessageInfoDeployMapper;
    @Autowired private SubMessageInfoService subMessageInfoService;
    @Autowired private SubMessageInfoDeployMapper subMessageInfoDeployMapper;

    @Mapping(source = "serviceMessages", target = "serviceMessages")
    public abstract EAIMessageDeploy toVo(EAIMessageEntity eaiMessageEntity);

    @InheritInverseConfiguration
    public abstract EAIMessageEntity toEntity(EAIMessageDeploy eaiMessageDeploy);

    @AfterMapping
    public void mapAfterToVo(EAIMessageEntity entity, @MappingTarget EAIMessageDeploy vo) {
        ArrayList<BizKeyDeploy> bizKeyDeploys = new ArrayList<>();
        bizKeyService.findByIdEaiSvcName(entity.getId()).forEach(bizEntity -> bizKeyDeploys.add(bizKeyDeployMapper.toVo(bizEntity)));
        vo.getBizKeyDeploys().addAll(bizKeyDeploys);

        ArrayList<StandardMessageInfoDeploy> standardMessageDeploys = new ArrayList<>();
        standardMessageInfoService.findByEaiSvcCode(entity.getId()).forEach(stdMsgEntity -> standardMessageDeploys.add(standardMessageInfoDeployMapper.toVo(stdMsgEntity)));
        vo.getStandardMessageInfoDeploys().addAll(standardMessageDeploys);

        ArrayList<SubMessageInfoDeploy> subMessageDeploys = new ArrayList<>();
        subMessageInfoService.findAll(entity.getId()).forEach(subMessageInfo -> subMessageDeploys.add(subMessageInfoDeployMapper.toVo(subMessageInfo)));
        vo.getSubMessageInfoDeploys().addAll(subMessageDeploys);
    }

    @AfterMapping
    public void mapAfterToEntity(EAIMessageDeploy eaiMessageDeploy, @MappingTarget EAIMessageEntity eaiMessageEntity) {
        eaiMessageEntity.getServiceMessages().forEach(serviceMessageEntity -> serviceMessageEntity.setEaimessage(eaiMessageEntity));
    }

}
