package com.apobates.forum.thrones.vo;

import com.apobates.forum.core.entity.Topic;
import com.apobates.forum.core.entity.proxy.TopicReplica;
import com.apobates.forum.utils.Commons;
import com.apobates.forum.utils.DateTimeUtils;
import java.io.Serializable;
import java.util.Map;

/**
 * 右侧的话题摘要[Topic.VO.3]
 *
 * @author xiaofanku
 * @since 20200522
 */
public final class ForumThreadsDigest implements Serializable {
    private static final long serialVersionUID = 301467155856214156L;
    //话题的标题
    private final String title;
    //话题的连接
    private final String link;
    //话题的作者
    private final long author;
    //话题的作者
    private final String authorNames;
    //发布的日期
    private final String date;
    
    public ForumThreadsDigest(Topic topic) {
        super();
        this.title = topic.getTitle();
        this.link = String.format("/topic/%s.xhtml", TopicReplica.generateConnectString(topic.getId(), topic.getBoardId(), topic.getVolumesId()));
        this.author = topic.getMemberId();
        this.authorNames = topic.getMemberNickname();
        this.date = DateTimeUtils.formatClock(topic.getEntryDateTime());
    }
    
    public ForumThreadsDigest(TopicReplica topic) {
        super();
        this.title = topic.getTitle();
        this.link = String.format("/topic/%s.xhtml", topic.getConnect());
        this.author = topic.getMemberId();
        this.authorNames = topic.getMemberNickname();
        this.date = DateTimeUtils.formatClock(topic.getEntryDateTime());
    }
    
    public String getTitle() {
        return title;
    }
    
    public String getDate() {
        return date;
    }
    
    public String getLink() {
        return link;
    }
    
    public long getAuthor() {
        return author;
    }
    
    public String getAuthorNames() {
        return authorNames;
    }
    
    /**
     * 返回一个不可变的map,不可以在结果基础上put
     * 
     * @return 
     */
    public Map<String, String> toMap() {
        return Map.ofEntries(
                Map.entry("title", getTitle()), 
                Map.entry("link", getLink()), 
                Map.entry("author", getAuthor()+""), 
                Map.entry("authorNames", getAuthorNames()), 
                Map.entry("date", getDate()));
    }
    
    /**
     * 若extInfoMap中的key与toMap的key冲突,以toMap为准
     * 
     * @param extInfoMap 扩展的会员信息
     * @return 
     */
    public Map<String, String> toMergeMap(Map<String, String> extInfoMap) {
        return Commons.mergeMap(toMap(), extInfoMap, (oldValue, newValue)-> oldValue);
    }
}