package com.apobates.forum.member;

import com.apobates.forum.member.entity.Member;
import com.apobates.forum.member.entity.MemberGroupEnum;
import com.apobates.forum.member.entity.MemberRoleEnum;
import com.apobates.forum.utils.Commons;
import java.io.Serializable;
import java.util.Map;

/**
 * 会员个人信息
 *
 * @author xiaofanku
 * @since 20200510
 */
public final class MemberProfileBean implements Serializable {
    //会员ID
    private final long id;
    private final String nickname;
    //会员所在组名称
    private final String groupName;
    //会员所有角名称
    private final String roleName;
    //用以关联CSS样式定义:Member.getStyle
    private final String label;
    //会员的签名
    private final String signature;
    //主题数量
    private final long threads;
    //回复数
    private final long replies;
    //等级名称:小白
    private final String level;
    //等级数字:1级
    private final int levelNo;
    //根据主题的操作行为计算所得积分
    private final double score;
    
    public long getThreads() {
        return threads;
    }
    
    public long getReplies() {
        return replies;
    }
    
    public String getLevel() {
        return level;
    }
    
    public int getLevelNo() {
        return levelNo;
    }
    
    public double getScore() {
        return score;
    }
    //
    public long getId() {
        return id;
    }
    public String getNickname() {
        return nickname;
    }
    public String getGroupName() {
        return groupName;
    }
    public String getRoleName() {
        return roleName;
    }
    public String getLabel() {
        return label;
    }
    public String getSignature() {
        return signature;
    }
    
    private MemberProfileBean(long id, String nickname, String memberGroupTitle, String memberRoleTitle, String memberStyle, long threads, long replies, String level, int levelNo, double score, String signature) {
        this.id = id;
        this.nickname = nickname;
        this.groupName = memberGroupTitle;
        this.roleName = memberRoleTitle;
        this.label = memberStyle;
        this.signature = signature;
        //
        this.threads = threads;
        this.replies = replies;
        this.level = level;
        this.score = score;
        this.levelNo = levelNo;
    }
    
    /**
     * 返回一个不可变的map,不可以在结果基础上put
     * 
     * @return 
     */
    public Map<String, String> toMap() {
        return Map.ofEntries(Map.entry("id", getId() + ""), 
                    Map.entry("nickname", getNickname()), 
                    Map.entry("groupNames", getGroupName()), 
                    Map.entry("roleNames", getRoleName()), 
                    Map.entry("style", getLabel()), 
                    Map.entry("signature", getSignature()), 
                    Map.entry("level", getLevel()), 
                    Map.entry("levelNo", getLevelNo() + ""), 
                    Map.entry("threads", getThreads() + ""), 
                    Map.entry("replies", getReplies() + ""), 
                    Map.entry("score", getScore() + ""));
    }
    
    /**
     * 若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);
    }
    
    public static MemberProfileBean guest() {
        return new MemberProfileBean(-1L, Member.GUEST_NAMES, MemberGroupEnum.GUEST.getTitle(), MemberRoleEnum.NO.getTitle(), "guest", 0, 0, "-", 0, 0, "");
    }
    
    public static MemberProfileBean empty(long id, String names, String memberGroupTitle, String memberRoleTitle, String memberStyle, String signature) {
        return new MemberProfileBean(id, names, memberGroupTitle, memberRoleTitle, memberStyle, 0, 0, "-", 0, 0, signature);
    }
    
    public static class MemberProfileBuilder {
        private final long id;
        private final String nickname;
        private final String groupName; //组名
        private final String roleName; //角色名
        private final String label;
        private String level = "VA";
        private int levelNo = 0;
        private long threads = 0L;
        private long replies = 0L;
        private double score = 0;
        private String signature = "No Signature";
        
        public MemberProfileBuilder(long id, String nickname, String memberGroupTitle, String memberRoleTitle, String memberStyle, String signature) {
            this.id = id;
            this.nickname = nickname;
            this.groupName = memberGroupTitle;
            this.roleName = memberRoleTitle;
            this.label = memberStyle;
            if (Commons.isNotBlank(signature)) {
                this.signature = signature;
            }
        }
        
        public MemberProfileBean build() {
            return new MemberProfileBean(id, nickname, groupName, roleName, label, getThreads(), getReplies(), getLevel(), getLevelNo(), getScore(), signature);
        }
        
        public MemberProfileBuilder setLevel(String level, int levelNo) {
            this.level = level;
            this.levelNo = levelNo;
            return this;
        }
        
        public MemberProfileBuilder setThreads(long threads) {
            this.threads = threads;
            return this;
        }
        
        public MemberProfileBuilder setReplies(long replies) {
            this.replies = replies;
            return this;
        }
        
        public MemberProfileBuilder setScore(double score) {
            this.score = score;
            return this;
        }
        
        public String getLevel() {
            return level;
        }
        
        private long getThreads() {
            return threads;
        }
        
        private long getReplies() {
            return replies;
        }
        
        public double getScore() {
            return score;
        }
        
        public int getLevelNo() {
            return levelNo;
        }
        //
        public long getId() {
            return id;
        }
        
        public String getNickname() {
            return nickname;
        }
        
        public String getGroupName() {
            return groupName;
        }
        
        public String getRoleName() {
            return roleName;
        }
        
        public String getLabel() {
            return label;
        }
        
        public String getSignature() {
            return signature;
        }
    }
}