package com.zcxk.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.*;

@Data
public class LoginUser implements UserDetails {

    private static final long serialVersionUID = 2688116832144932826L;

    @ApiModelProperty(value="id")
    private Integer id;

    @ApiModelProperty(value="用户名")
    private String username;

    @ApiModelProperty(value="姓名")
    private String name;

    @ApiModelProperty(value="手机号码")
    private String phoneNumber;

    @ApiModelProperty(value="类型,1超级管理员，-999站点管理员，2普通用户")
    private String type;
    @ApiModelProperty(value="公司id")
    private Integer companyId;
    @ApiModelProperty(value="部门id")
    private Integer departmentId;


    @ApiModelProperty(value="租户标识")
    private String tenantId;
    @ApiModelProperty(value="用户权限类型")
    private Integer permissonType;
    @ApiModelProperty(value="用户角色id")
    private Integer roleId;

    @JsonIgnore
    private String LoginToken;

    //数据权限
    private List<ProgramItem> programItemList;
    @JsonIgnore
    //同一个手机号下的所有用户信息
    private String userInfos;
    private List<String>appIds;



    //权限
    private Set<GrantedAuthority> authorities;

    public LoginUser(Set<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    public LoginUser(Integer id, String username, String phoneNumber, String type, String tenantId, Set<GrantedAuthority> authorities) {
        this.id = id;
        this.username = username;
        this.phoneNumber = phoneNumber;
        this.type = type;
        this.tenantId = tenantId;
        this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return null;
    }

    private static SortedSet<GrantedAuthority> sortAuthorities(Collection<? extends GrantedAuthority> authorities) {
        Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection");
        SortedSet<GrantedAuthority> sortedAuthorities = new TreeSet(new AuthorityComparator());
        Iterator var2 = authorities.iterator();

        while(var2.hasNext()) {
            GrantedAuthority grantedAuthority = (GrantedAuthority)var2.next();
            Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements");
            sortedAuthorities.add(grantedAuthority);
        }

        return sortedAuthorities;
    }

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

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

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

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }

    private static class AuthorityComparator implements Comparator<GrantedAuthority>, Serializable {
        private static final long serialVersionUID = 500L;

        private AuthorityComparator() {
        }

        public int compare(GrantedAuthority g1, GrantedAuthority g2) {
            if (g2.getAuthority() == null) {
                return -1;
            } else {
                return g1.getAuthority() == null ? 1 : g1.getAuthority().compareTo(g2.getAuthority());
            }
        }
    }

    public void setAuthorities(Set<GrantedAuthority> authorities) {
        this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
    }
}
