package com.zsc.edu.gateway.framework.security;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.zsc.edu.gateway.modules.system.entity.Authority;
import com.zsc.edu.gateway.modules.system.entity.Dept;
import com.zsc.edu.gateway.modules.system.entity.Role;
import com.zsc.edu.gateway.modules.system.entity.User;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author harry yao
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonIgnoreProperties("password")
public class UserDetailsImpl implements UserDetails {

    public Long id;
    public String username;
    public String password;
    public Boolean enableState;
    public String name;
    public Dept dept;
    public Role role;
    public List<Role> roles;
    public Set<Authority> authorities;
    public Set<String> permissions;
    public Set<Long> dataScopeDeptIds;
    public Long deptId;
    public Long createId;

    public UserDetailsImpl(Long id, String username, String password, String name, Boolean enableState, Dept dept, Set<Long> dataScopeDeptIds, Role role, Set<Authority> authorities, Set<String> permissions, List<Role> roles, Long deptId, Long createId) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.enableState = enableState;
        this.name = name;
        this.dept = dept;
        this.dataScopeDeptIds = dataScopeDeptIds;
        this.role = role;
        this.authorities = authorities;
        this.permissions = permissions;
        this.roles = roles;
    }

    public static UserDetailsImpl from(User user, Set<String> permissions) {
        return new UserDetailsImpl(
                user.id,
                user.username,
                user.password,
                user.name,
                user.enableState,
                user.dept,
                user.dataScopeDeptIds,
                user.role,
                user.role.authorities,
                permissions,
                user.roles,
                user.deptId,
                user.createId
        );
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
//        return authorities.stream().map(authority -> new SimpleGrantedAuthority(authority.getName())).collect(Collectors.toSet());
        return permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enableState;
    }

}
