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

import cn.reghao.jutil.jdk.db.BaseObject;
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;

/**
 * 用户账号
 * 实现的 UserDetails 接口提供了 Spring Security 需要的用户认证信息
 *
 * @author reghao
 * @date 2021-10-30 17:49:07
 */
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class UserAccount extends BaseObject<Integer> implements UserDetails {
    @NotNull
    private Long userId;
    @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 String screenName;
    private String avatarUrl;
    private transient int loginType;
    private transient int plat;

    // Spring Security 使用的字段
    private transient Set<UserAuthority> authorities;
    private Boolean enabled;
    private Boolean locked;

    public UserAccount(long userId, String email, String mobile, String avatarUrl) {
        this.userId = userId;
        this.username = String.format("tnb_%s", userId);
        this.email = email;
        this.mobile = mobile;
        this.encodedPassword = "";
        this.salt = "";
        this.createAt = LocalDateTime.now();
        this.enabled = true;
        this.locked = false;
        this.screenName = this.username;
        this.avatarUrl = avatarUrl;
        this.authorities = Set.of(new UserAuthority());
    }

    public UserAccount(long userId, String email, String mobile, String encodedPassword, String salt, String avatarUrl) {
        this.userId = userId;
        this.username = String.format("tnb_%s", userId);
        this.mobile = mobile;
        this.email = email;
        this.encodedPassword = encodedPassword;
        this.salt = salt;
        this.createAt = LocalDateTime.now();
        this.enabled = true;
        this.locked = false;
        this.screenName = this.username;
        this.avatarUrl = avatarUrl;
        this.authorities = Set.of(new UserAuthority());
    }

    @Deprecated
    public UserAccount(long userId, String email, String username, String screenName, String avatarUrl) {
        this.userId = userId;
        this.username = username;
        this.email = email;
        this.createAt = LocalDateTime.now();
        this.enabled = true;
        this.locked = false;
        this.screenName = screenName;
        this.avatarUrl = avatarUrl;
        this.authorities = Set.of(new UserAuthority());
    }

    public Long getUserId() {
        return userId;
    }

    public String getEncodedPassword() {
        return encodedPassword;
    }

    public String getSalt() {
        return salt;
    }

    public void setAuthorities(Set<UserAuthority> authorities) {
        this.authorities = authorities;
    }

    public void setLoginType(int loginType) {
        this.loginType = loginType;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public String getPassword() {
        return this.encodedPassword;
    }

    /**
     * 账号是否未过期
     *
     * @param
     * @return
     * @date 2021-11-16 上午9:56
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 密码是否未过期
     *
     * @param
     * @return
     * @date 2021-11-16 上午9:56
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账号是否可用
     *
     * @param
     * @return
     * @date 2021-11-16 上午9:56
     */
    @Override
    public boolean isEnabled() {
        return this.enabled;
    }

    /**
     * 账号是否锁定
     *
     * @param
     * @return
     * @date 2021-11-16 上午9:56
     */
    @Override
    public boolean isAccountNonLocked() {
        return !this.locked;
    }

    /**
     * 权限列表
     *
     * @param
     * @return
     * @date 2021-11-16 上午9:56
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }
}
