package cn.axzo.msg.center.message.service.todo;

import cn.axzo.msg.center.message.domain.param.PendingMessagePushParam;
import cn.axzo.msg.center.message.service.card.CardManager;
import cn.axzo.msg.center.service.dto.PeerPerson;
import cn.axzo.msg.center.service.dto.PersonDTO;
import cn.axzo.msg.center.service.enums.CardBizState;
import cn.axzo.msg.center.service.enums.CardState;
import cn.axzo.msg.center.service.pending.request.CardSendRequest;
import cn.axzo.msg.center.service.pending.request.CardStateInfo;
import cn.axzo.msg.center.service.pending.request.CardUpdateStateRequest;
import cn.axzo.msg.center.service.pending.request.CompletePendingMessageRequest;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Sets;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author xudawei@axzo.cn
 * @date 2024/12/12
 * @desc 待办与卡片关联包装
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TodoWithCardWrapper {

    private final CardManager cardManager;

    private static final String APP_CODE = "msg-center";

    public void send(PendingMessagePushParam param) {
        //1 构建对象
        CardSendRequest cardSendRequest = this.buildCardSendRequest(param);
        //2 发送
        cardManager.send(cardSendRequest);
    }

    /**
     * 卡片更新完成状态
     */
    public void cardCompleteState(CompletePendingMessageRequest request) {
        //1 构建对象
        CardUpdateStateRequest updateStateRequest = this.buildCardCompleteState(request);
        //2 更新状态
        cardManager.updateState(updateStateRequest);
    }

    /**
     * 构建卡片完成状态
     */
    public CardUpdateStateRequest buildCardCompleteState(CompletePendingMessageRequest request) {
        CardUpdateStateRequest updateStateRequest = new CardUpdateStateRequest();

        CardStateInfo cardStateInfo
                = CardStateInfo.buildCardStateInfo(CardBizState.END.getCode(), CardState.COMPLETED);//TODO
        updateStateRequest.setStateInfo(cardStateInfo);

        updateStateRequest.setAppCode(APP_CODE);
        updateStateRequest.setTemplateCode(request.getTemplateCode());
        updateStateRequest.setBizCode(request.getBizCode());
        updateStateRequest.setSubBizCode("");//TODO
        updateStateRequest.setReceivers(Sets.newHashSet());//TODO

        return updateStateRequest;
    }

//    private

    /**
     * 构建对象
     */
    private CardSendRequest buildCardSendRequest(PendingMessagePushParam param) {
        CardSendRequest sendRequest = new CardSendRequest();
        sendRequest.setAppCode(APP_CODE);
        sendRequest.setTemplateCode(param.getTemplateCode());
        sendRequest.setBizCode(param.getBizCode());
        sendRequest.setSubBizCode(param.getSubBizCode());
        sendRequest.setSender(PeerPerson.newPeerPerson(param.getPromoter().getId()
                , param.getPromoterOuId()
                , param.getPromoterWorkspaceId()));
        sendRequest.setReceivers(this.buildReceivers(param.getExecutor(), param.getOuId(), param.getWorkspaceId()));
        if (StringUtils.isNotBlank(param.getBizExtParams())) {
            sendRequest.setBizParam(JSON.parseObject(param.getBizExtParams()));
        }
        if (StringUtils.isNotBlank(param.getRouterParams())) {
            sendRequest.setRouterParam(JSON.parseObject(param.getRouterParams()));
        }
        sendRequest.setStateInfo(CardStateInfo.buildCardStateInfo(CardBizState.PENDING.getCode(), CardState.CREATED));

        return sendRequest;
    }

    private Set<PeerPerson> buildReceivers(List<PersonDTO> personDTOS, Long ouId, Long workspaceId) {
        if (CollectionUtils.isNotEmpty(personDTOS)) {
            return Sets.newHashSet();
        }
        return personDTOS.stream().map(item -> PeerPerson.newPeerPerson(item.getId(), ouId, workspaceId)).collect(Collectors.toSet());
    }


}
