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

import cn.reghao.tnb.account.app.model.dto.AccountDetail;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;

/**
 * 仅供 Spring Security 使用的用户认证信息
 *
 * @author reghao
 * @date 2021/11/16 09:12:48
 */
@Setter
@Getter
public class UserDetail implements UserDetails {
    private Long userId;
    private String username;
    private String encodedPassword;
    private String salt;
    private Boolean enabled;
    private Boolean locked;
    private final Set<GrantedAuthority> authorities;
    private int loginType;
    private int plat;

    public UserDetail(AccountDetail accountDetail) {
        this.userId = accountDetail.getUserId();
        this.username = accountDetail.getUsername();
        this.encodedPassword = accountDetail.getEncodedPassword();
        this.salt = accountDetail.getSalt();
        this.enabled = accountDetail.getEnabled();
        this.locked = accountDetail.getLocked();
        this.authorities = Set.of(new UserAuthority());
    }

    public UserDetail() {
        this.authorities = Set.of(new UserAuthority());
    }

    public String getSalt() {
        return this.salt;
    }

    @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;
    }
}
