package cn.reghao.tnb.account.app.model.po;

import cn.reghao.jutil.jdk.db.BaseObject;
import cn.reghao.tnb.account.app.model.constant.AccountRole;
import cn.reghao.tnb.account.app.model.constant.AccountType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户账号
 *
 * @author reghao
 * @date 2021-10-30 17:49:07
 */
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Data
public class UserAccount extends BaseObject<Integer> {
    @NotNull
    private Long userId;
    private Integer accountType;
    @NotNull
    @NotBlank
    @Length(min = 4, max = 16, message = "用户名仅可包含数字,大小写字母和 '-' 等字符, 长度为 4 ～ 16 个字符")
    private String username;
    private String email;
    private String mobile;
    @Length(min = 8, max = 20, message = "密码不能为空白字符串, 长度为 8 ～ 20 个字符")
    private String encodedPassword;
    private String salt;
    private LocalDateTime createAt;
    private Boolean notify;

    // Spring Security 使用的字段
    private String role;
    private Boolean enabled;
    private Boolean locked;

    private String screenName;
    private String avatarUrl;
    private String signature;
    private Integer gender;

    public UserAccount(long userId, String email, String mobile, String avatarUrl) {
        this.userId = userId;
        this.accountType = AccountType.tnb.getValue();
        this.username = String.format("tnb_%s", userId);
        this.email = email;
        this.mobile = mobile;
        this.encodedPassword = "";
        this.salt = "";
        this.createAt = LocalDateTime.now();
        this.notify = false;
        this.role = AccountRole.user.getValue();
        this.enabled = true;
        this.locked = false;
        this.screenName = this.username;
        this.avatarUrl = avatarUrl;
        this.gender = 2;
    }

    public UserAccount(long userId, String email, String mobile, String encodedPassword, String salt, String avatarUrl) {
        this.userId = userId;
        this.accountType = AccountType.tnb.getValue();
        this.username = String.format("tnb_%s", userId);
        this.mobile = mobile;
        this.email = email;
        this.encodedPassword = encodedPassword;
        this.salt = salt;
        this.createAt = LocalDateTime.now();
        this.notify = false;
        this.role = AccountRole.user.getValue();
        this.enabled = true;
        this.locked = false;
        this.screenName = this.username;
        this.avatarUrl = avatarUrl;
        this.gender = 2;
    }

    @Deprecated
    public UserAccount(long userId, String email, String username, String screenName, String avatarUrl) {
        this.userId = userId;
        this.accountType = AccountType.bilibili.getValue();
        this.username = username;
        this.email = email;
        this.createAt = LocalDateTime.now();
        this.role = AccountRole.user.getValue();
        this.enabled = true;
        this.locked = false;
        this.screenName = screenName;
        this.avatarUrl = avatarUrl;
    }
}
