package com.apobates.forum.member.storage.core;

import com.apobates.forum.utils.Commons;
import com.apobates.forum.utils.DateTimeUtils;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;

/**
 * MemberSessionBean与Map<String,String>的转换器
 * 
 * @author xiaofanku
 * @since 20200905
 */
public class MemberSessionBeanConverter {
    
    public static Map<String,String> toMap(MemberSessionBean msb){
        return Map.ofEntries(Map.entry("mid", msb.getMid() + ""), 
                    Map.entry("names", msb.getNames()), 
                    Map.entry("nickname", msb.getNickname()), 
                    Map.entry("signature", msb.getSignature()), 
                    Map.entry("status", msb.getStatus().getSymbol()+""), 
                    Map.entry("group", msb.getGroup().getSymbol()+""), 
                    Map.entry("role", msb.getRole().getSymbol()+""), 
                    Map.entry("avatar", msb.getAvatar()), 
                    Map.entry("birth", DateTimeUtils.getRFC3339(msb.getRegisteDateTime())));
    }
    
    /**
     * 生成不可变的Map集合
     * @param msb
     * @param expireDateTime
     * @return 
     */
    public static Map<String,String> toMap(MemberSessionBean msb, LocalDateTime expireDateTime){
        return Map.ofEntries(Map.entry("mid", msb.getMid() + ""), 
                    Map.entry("names", msb.getNames()), 
                    Map.entry("nickname", msb.getNickname()), 
                    Map.entry("signature", msb.getSignature()), 
                    Map.entry("status", msb.getStatus().getSymbol()+""), 
                    Map.entry("group", msb.getGroup().getSymbol()+""), 
                    Map.entry("role", msb.getRole().getSymbol()+""), 
                    Map.entry("avatar", msb.getAvatar()), 
                    Map.entry("birth", DateTimeUtils.getRFC3339(msb.getRegisteDateTime())), 
                    Map.entry("expire", DateTimeUtils.getRFC3339(expireDateTime)));
    }
    
    public static Optional<MemberSessionBean> toInstance(Map<String,String> data, String ipAddr, String sentinel){
        if(null == data || data.isEmpty()){
            return Optional.empty();
        }
        MemberSessionBeanBuilder msbb = MemberSessionBeanBuilder.empty();
        //
        long mid = Commons.stringToLong(data.getOrDefault("mid", null), -1L);
        if (mid > 0) {
            msbb = msbb.setId(mid);
        }
        //
        msbb = msbb.setNames(data.getOrDefault("names", null));
        msbb = msbb.setNickname(data.getOrDefault("nickname", null));
        msbb = msbb.setSignature(data.getOrDefault("signature", null));
        //
        int statusEnumSymbol = Commons.stringToInteger(data.getOrDefault("status", null), -1);
        msbb = msbb.setStatus(statusEnumSymbol);
        //
        int groupEnumSymbol = Commons.stringToInteger(data.getOrDefault("group", null), -3);
        msbb = msbb.setGroup(groupEnumSymbol);
        //
        int roleEnumSymbol = Commons.stringToInteger(data.getOrDefault("role", null), 0);
        msbb = msbb.setRole(roleEnumSymbol);
        //
        msbb = msbb.setBirthday(data.getOrDefault("birth", null));
        msbb = msbb.setAvatar(data.getOrDefault("avatar", null));
        //
        return Optional.of(msbb.build(ipAddr, sentinel));
    }
}