package top.imwork.window.silos.security.details;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import top.imwork.window.silos.entity.upms.SysRole;
import top.imwork.window.silos.entity.upms.SysUser;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * Copyright (C), 2015-2025
 * FileName: SecurityUserDetails
 * Author<作者姓名>:   stars
 * CreateTime<创建时间>:   2025/12/10 13:06
 * UpdateTime<修改时间>:   2025/12/10 13:06
 * Description〈功能简述〉: 封装用户登录信息 SecurityUserDetails 一定要有一个类，实现UserDetails接口，供程序调用
 * History<历史描述>:
 * Since<版本号>: 1.0.0
 */
public class SecurityUserDetails implements UserDetails {
    private String username;

    private String password;

    private SysUser sysUser;

    //包含着用户对应的所有角色 Role，在使用时调用者给对象注入roles
    private List<SysRole>  sysRoles;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public SysUser getSysUser() {
        return sysUser;
    }

    public void setSysUser(SysUser sysUser) {
        this.sysUser = sysUser;
    }

    public List<SysRole> getSysRoles() {
        return sysRoles;
    }

    public void setSysRoles(List<SysRole> sysRoles) {
        this.sysRoles = sysRoles;
    }

    //无参构造
    private SecurityUserDetails() {}
    //SysUser构造
    public SecurityUserDetails(SysUser sysUser) {
        this.username = sysUser.getLoginName();
        this.password = sysUser.getLoginPassword();
        this.sysUser = sysUser;
    }
    //用User和List<Role>构造
    public SecurityUserDetails(SysUser user, List<SysRole> roles) {
        this.username = user.getLoginName();
        this.password = user.getLoginPassword();
        this.sysUser = user;
        this.sysRoles = roles;
    }

    public static SecurityUserDetails createSecurityUserDetails() {
        return new SecurityUserDetails();
    }

    //返回用户所有角色的封装，一个Role对应一个GrantedAuthority
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        for(SysRole role : sysRoles) {
            authorities.add(new SimpleGrantedAuthority(role.getRoleName()));
        }
        return authorities;
    }

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

    @Override
    public String getUsername() {
        return username;
    }
    //判断账号是否已经过期，默认没有过期
    @Override
    public boolean isAccountNonExpired() {
        return UserDetails.super.isAccountNonExpired();
    }
    //判断账号是否被锁定，默认没有锁定
    @Override
    public boolean isAccountNonLocked() {
        return UserDetails.super.isAccountNonLocked();
    }
    //判断信用凭证是否过期，默认没有过期
    @Override
    public boolean isCredentialsNonExpired() {
        return UserDetails.super.isCredentialsNonExpired();
    }
    //判断账号是否可用，默认可用
    @Override
    public boolean isEnabled() {
        return UserDetails.super.isEnabled();
    }

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SecurityUserDetails that = (SecurityUserDetails) o;
        return username.equals(that.username);
    }

    @Override
    public int hashCode() {
        return Objects.hash(username);
    }
}