package com.ningdatech.pmapi.user.security.auth.model;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.ningdatech.basic.auth.AbstractLoginUser;
import com.ningdatech.pmapi.common.constant.RegionConst;
import com.ningdatech.pmapi.sys.model.entity.Role;
import com.ningdatech.pmapi.user.entity.enumeration.RoleEnum;
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.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

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

    private static final long serialVersionUID = -8219907690673456440L;

    private String realName;

    private String password;

    /**
     * 所属区域编码
     */
    private String regionCode;

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

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

    /**
     * 浙政钉组织code
     */
    private String organizationCode;

    /**
     * 浙政钉组织名称
     */
    private String organizationName;

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

    /**
     * 任职所在单位code
     */
    private String empPosUnitCode;

    /**
     * 任职所在单位
     */
    private String empPosUnitName;


    private String mobile;


    /**
     * 获取用户权限
     *
     * @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 new ArrayList<>();
    }

    @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() {
        if (CollUtil.isNotEmpty(this.userRoleList)) {
            for (Role role : this.userRoleList) {
                RoleEnum roleEnum = RoleEnum.mathByName(role.getCode());
                if (Objects.nonNull(roleEnum) && roleEnum.eq(RoleEnum.COMPANY_MANAGER.name())) {
                    return Boolean.TRUE;
                }
            }
        }
        return Boolean.FALSE;
    }

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

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

    public Boolean getIsMunicipalOrg() {
        //如果是丽水市本级的code 就是
        if (RegionConst.RC_LS.equals(this.regionCode)) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
