package cn.axzo.msg.center.message.service.card.domain;

import cn.axzo.msg.center.domain.entity.Card;
import cn.axzo.msg.center.message.domain.vo.GeneralMessagePushVO;
import cn.axzo.msg.center.message.service.card.CardRequestContext;
import cn.axzo.msg.center.message.service.card.CardTemplate;
import cn.axzo.msg.center.service.pending.request.CardSendRequest;
import cn.axzo.msg.center.service.pending.response.v3.model.ParsedModelV3;
import cn.axzo.msg.center.service.pending.response.v3.model.ParsedTemplateV3;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * @author yanglin
 */
@Setter
@Getter
@RequiredArgsConstructor
public class CardSendModel {

    private final CardRequestContext<CardSendRequest> requestContext;
    private final CardTemplate cardTemplate;
    private final List<Card> cards = Collections.synchronizedList(new ArrayList<>());

    private GeneralMessagePushVO cardContent;

    public void addCard(Card card) {
        cards.add(card);
    }

    public Collection<CardGroup> getCardGroups() {
        HashMap<CardGroupKey, CardGroup> groups = new HashMap<>();
        for (Card card : cards) {
            CardGroupKey key = new CardGroupKey(
                    card.getReceiverOuId(),
                    card.getReceiverWorkspaceId(),
                    card.getReceiverAppType());
            groups.computeIfAbsent(key, CardGroup::new)
                    .addCard(card);
        }
        return groups.values();
    }

    public CardSendRequest getRequest() {
        return requestContext.getRequest();
    }

    public ParsedModelV3 getParsedModel() {
        return cardTemplate.getParsedModel();
    }

    public ParsedTemplateV3 getTemplate() {
        return cardTemplate.getTemplate();
    }
}