package com.hz.pm.api.user.security.model;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hz.pm.api.sys.model.entity.Role;
import com.hz.pm.api.user.model.enumeration.RoleEnum;
import com.ningdatech.basic.auth.AbstractLoginUser;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LiuXinXin
 * @since 2022/8/1 下午3:32
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class UserInfoDetails extends AbstractLoginUser implements UserDetails {

    private static final long serialVersionUID = -8219907690673456440L;

    private String password;

    /**
     * 所属区域层级
     */
    private Integer regionLevel;

    /**
     * 浙政钉扫码 员工code
     */
    private String employeeCode;

    /**
     * 用户所属单位
     */
    private String mhUnitName;

    /**
     * 用户所属单位ID
     */
    private Long mhUnitId;

    private String mhUserId;

    /**
     * 用户角色
     */
    private List<Role> userRoleList;


    private String mobile;

    public String getUserIdStr() {
        return String.valueOf(getUserId());
    }

    public String getMhUnitIdStr() {
        return String.valueOf(getMhUnitId());
    }


    /**
     * 获取用户权限
     *
     * @return \
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (CollectionUtil.isNotEmpty(this.userRoleList)) {
            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            for (Role role : this.userRoleList) {
                authorities.add(new SimpleGrantedAuthority(role.getCode()));
            }
            return authorities;
        }
        return Collections.emptyList();
    }

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

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

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

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

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

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

    /**
     * 取最高的权限
     *
     * @return \
     */
    public RoleEnum getRoleCode() {
        if (CollUtil.isNotEmpty(this.userRoleList)) {
            return RoleEnum.checkHigherRole(this.userRoleList.stream()
                    .map(Role::getCode).collect(Collectors.toList()));
        }
        return null;
    }

    public boolean getIsOrgAdmin() {
        return containsRole(RoleEnum.COMPANY_MANAGER);
    }

    public boolean getSuperAdmin() {
        return containsRole(RoleEnum.SUPER_ADMIN);
    }

    public boolean getRegionAdmin() {
        return containsRole(RoleEnum.REGION_MANAGER);
    }

    private boolean containsRole(RoleEnum targetRole) {
        if (CollUtil.isNotEmpty(this.userRoleList)) {
            for (Role role : this.userRoleList) {
                RoleEnum roleEnum = RoleEnum.mathByName(role.getCode());
                if (Objects.nonNull(roleEnum) && roleEnum.eq(targetRole.name())) {
                    return Boolean.TRUE;
                }
            }
        }
        return Boolean.FALSE;
    }

}
