package cn.axzo.msg.center.inside.notices.service.impl.v3.msg;

import cn.axzo.msg.center.api.request.v4.MessageSendBasicInfoV4;
import cn.axzo.msg.center.api.response.v3.MessageSendResultV3;
import cn.axzo.msg.center.api.response.v3.TemplateSendResultV3;
import cn.axzo.msg.center.inside.notices.service.impl.v3.EventMappingProcessor;
import cn.axzo.msg.center.message.domain.dto.TemplateModelV3;
import cn.axzo.msg.center.message.service.card.CardManager;
import cn.axzo.msg.center.service.dto.PeerPerson;
import cn.axzo.msg.center.service.dto.PersonV3DTO;
import cn.axzo.msg.center.service.enums.Channel;
import cn.axzo.msg.center.service.pending.request.CardSendRequest;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * @author yanglin
 */
@Slf4j
@Component
@Scope("prototype")
@RequiredArgsConstructor
public class CardMappingProcessor implements EventMappingProcessor {

    private final CardManager cardManager;

    @Setter
    private MessageSendBasicInfoV4 sendBasicInfo;
    @Setter
    private Channel channel;
    @Setter
    private TemplateModelV3 templateModel;

    @Override
    public void saveRecords() {
        // NOP
    }

    @Override
    public void maybeAsyncSend() {
        PeerPerson cardSender = new PeerPerson();
        cardSender.setOuId(sendBasicInfo.getSenderOuId());
        cardSender.setWorkspaceId(sendBasicInfo.getSenderWorkspaceId());
        PersonV3DTO eventSender = sendBasicInfo.getSender();
        if (eventSender != null)
            cardSender.setPersonId(eventSender.getId());

        Set<PeerPerson> cardReceivers = new HashSet<>();
        for (PersonV3DTO eventReceiver : sendBasicInfo.receivers()) {
            PersonV3DTO.ReceiveModel receiveModel = eventReceiver.getImReceiveModel();
            PeerPerson cardReceiver = new PeerPerson();
            cardReceiver.setPersonId(eventReceiver.getId());
            cardReceiver.setOuId(receiveModel == null
                    ? sendBasicInfo.determineReceiversOuId()
                    : receiveModel.getOuId());
            cardReceiver.setWorkspaceId(receiveModel == null
                    ? sendBasicInfo.determineReceiversWorkspaceId()
                    : receiveModel.getWorkspaceId());
            cardReceivers.add(cardReceiver);
        }

        CardSendRequest cardRequest = new CardSendRequest();
        cardRequest.setAppCode("msg-center:im");
        cardRequest.setTemplateCode(templateModel.getTemplateCode());
        cardRequest.setBizCode(sendBasicInfo.getBizCode());
        cardRequest.setSubBizCode(null);
        cardRequest.setSender(cardSender);
        cardRequest.setReceivers(cardReceivers);
        cardRequest.setBizParam(sendBasicInfo.getBizExtParams());
        cardRequest.setRouterParam(sendBasicInfo.getRouterParams());
        cardRequest.setSubtitle(sendBasicInfo.getSubtitle());
        cardManager.send(cardRequest);
    }

    @Override
    public TemplateSendResultV3 buildTemplateSendResult() {
        TemplateSendResultV3 templateResult = new TemplateSendResultV3(
                templateModel.getTemplateCode(), channel.getCode());
        for (PersonV3DTO receiver : sendBasicInfo.receivers()) {
            templateResult.addResult(new MessageSendResultV3(
                    receiver.getId(), 0L));
        }
        return templateResult;
    }

}