package com.apobates.forum.letterbox.entity.proxy;

import com.apobates.forum.letterbox.entity.ForumLetter;
import com.apobates.forum.letterbox.entity.ForumLetterReceiver;
import com.apobates.forum.letterbox.entity.ForumLetterStatus;
import com.apobates.forum.letterbox.entity.ForumLetterTypeEnum;
import com.apobates.forum.utils.lang.ForumReplicable;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 * @author xiaofanku
 * @since 20200527
 */
public final class ForumLetterReplica extends ForumLetter implements ForumReplicable<ForumLetter,ForumLetterReplica>{
    private Set<ForumLetterReceiver> receivers = new HashSet<>();
    //Inbox的状态{readable|reply|usable}
    private ForumLetterStatus status;
    public static Function<ForumLetter, ForumLetterReplica> copyLetter = (ForumLetter letter) -> {
        return ForumReplicable.replica(letter, ForumLetterReplica.class).orElseGet(ForumLetterReplica::new);
    };
    
    public ForumLetterReplica(){
        super();
    }
    
    public Set<ForumLetterReceiver> getReceivers() {
        return receivers;
    }
    
    public void setReceivers(Set<ForumLetterReceiver> receivers) {
        this.receivers = receivers;
    }
    
    public ForumLetterStatus getStatus() {
        return status;
    }
    
    public void setStatus(ForumLetterStatus status) {
        this.status = status;
    }
    
    
    public String getTargetReceiverNicknames() {
        if (getReceivers().isEmpty()) {
            return ForumLetterTypeEnum.ALL.getTitle();
        }
        return getReceivers().stream().map(ForumLetterReceiver::getMemberNickname).collect(Collectors.joining(","));
    }

    /**
     * 输出事件需要的ForumLetter实例
     * @return
     */
    public ForumLetter toEventPayload(){
        ForumLetter fl = new ForumLetter();
        fl.setId(this.getId());
        fl.setNickname(this.getNickname());
        fl.setAuthor(this.getAuthor());
        return fl;
    }
}