package com.loan.system.service.Impl;

import com.loan.system.domain.enums.ExceptionEnum;
import com.loan.system.exception.DescribeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component("pms")
public class PermissionService {
    private static final Logger logger = LoggerFactory.getLogger(PermissionService.class);

    public boolean hasRole(String role){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            logger.error("PermissionService | method = hasRole() | 登录过期");
            throw new DescribeException(ExceptionEnum.LOGIN_EXPIRED);
        }
//        List<String> authorities = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
//        String roles = authorities.get(0);
//        boolean contains = roles.contains(role) || roles.contains(auth);
//        if (contains){
//            return true;
//        } else {
//            logger.error("PermissionService | method = hasRole() | 没有操作权限");
//            throw new DescribeException(ExceptionEnum.PERMISSION_DENIED);
//        }
        Set<String> userRoles = getUserRoles(authentication);
        // 检查角色
        boolean hasRole = userRoles.contains(role);
        if (hasRole){
            return true;
        } else {
            logger.error("PermissionService | method = hasRole() | 没有操作权限，所需角色: {}, 用户角色: {}",
                    role, userRoles);
            throw new DescribeException(ExceptionEnum.PERMISSION_DENIED);
        }
    }

    public boolean hasAnyRoles(String...roles){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication == null) {
            logger.error("PermissionService | method = hasRole() | 登录过期");
            throw new DescribeException(ExceptionEnum.LOGIN_EXPIRED);
        }
//        boolean contains = false;
//        List<String> strings = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
//        for(String role:roles){
//            if(strings.contains(role)){
//                contains = true;
//                break;
//            }
//        }
//        if (contains){
//            return true;
//        } else {
//            logger.error("PermissionService | method = hasRole() | 没有操作权限");
//            throw new DescribeException(ExceptionEnum.PERMISSION_DENIED);
//        }
        // 获取用户所有角色
        Set<String> userRoles = getUserRoles(authentication);

        // 检查是否有任意一个角色
        for(String role : roles){
            if(userRoles.contains(role)){
                return true;
            }
        }

        logger.error("PermissionService | method = hasAnyRoles() | 没有操作权限，所需角色: {}, 用户角色: {}",
                Arrays.toString(roles), userRoles);
        throw new DescribeException(ExceptionEnum.PERMISSION_DENIED);
    }
    private Set<String> getUserRoles(Authentication authentication) {
        Set<String> roles = new HashSet<>();
        for (GrantedAuthority authority : authentication.getAuthorities()) {
            roles.add(authority.getAuthority());
        }
        return roles;
    }
}
