package cn.reghao.bnt.web.admin.model.po;

import cn.reghao.bnt.web.util.BaseEntity;
import lombok.*;
import org.hibernate.validator.constraints.Length;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
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
@Getter
@Setter
@Table(name = "rbac_user")
@Entity
public class User extends BaseEntity implements UserDetails {
    @NotBlank
    @Column(nullable = false, unique = true)
    @Length(min = 3, max = 12, message = "用户名仅可包含数字,大小写字母和 '-' 等字符, 长度为 3 ～ 12 个字符")
    private String username;
    @NotBlank(message = "密码不能为空白字符串")
    private String encodedPassword;
    private String salt;
    private LocalDateTime createAt;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "rbac_user_role")
    private Set<String> role;
    //@OneToOne
    //private Role role;
    // Spring Security 使用的字段
    private Boolean enabled;
    private Boolean locked;

    private String screenName;
    private String avatarUrl;
    private String mobile;

    @Deprecated
    public User(String username, String password, String salt, Role role) {
        this.username = username;
        this.encodedPassword = password;
        this.salt = salt;
        this.createAt = LocalDateTime.now();
        this.role = Set.of(role.getName());
        this.enabled = true;
        this.locked = false;
        this.screenName = username;
        this.avatarUrl = "/dist/images/avatar.jpg";
    }

    public User(String username, String password, String salt, Set<String> roles) {
        this.username = username;
        this.encodedPassword = password;
        this.salt = salt;
        this.createAt = LocalDateTime.now();
        this.role = roles;
        this.enabled = true;
        this.locked = false;
        this.screenName = username;
        this.avatarUrl = "/dist/images/avatar.jpg";
    }

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

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

    /**
     * 账户是否未过期
     *
     * @param
     * @return
     * @date 2019-05-29 下午2:05
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 密码是否未过期
     *
     * @param
     * @return
     * @date 2019-05-29 下午2:05
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账户是否可用
     *
     * @param
     * @return
     * @date 2019-05-29 下午2:05
     */
    @Override
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * 账户是否锁定
     *
     * @param
     * @return
     * @date 2019-05-29 下午2:05
     */
    @Override
    public boolean isAccountNonLocked() {
        return locked;
    }

    /**
     * 权限列表
     *
     * @param
     * @return
     * @date 2019-05-29 下午2:22
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return role.stream().map(UserAuthority::new).collect(Collectors.toSet());
        /*UserAuthority userAuthority = new UserAuthority(role.getName());
        return Set.of(userAuthority);*/
    }
}
