package com.njzscloud.common.security.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.njzscloud.common.security.contant.AuthWay;
import com.njzscloud.common.security.contant.Constants;
import com.njzscloud.common.security.support.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Set;

/**
 * 获取认证信息工具
 */
public class SecurityUtil {
    /**
     * 获取当前登录主体信息
     *
     * @return UserAuthPrincipal
     */
    public static UserDetail loginUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetail userDetail = null;
        if (authentication != null) userDetail = (UserDetail) authentication.getPrincipal();
        return userDetail == null ? Constants.ANONYMOUS_USER : userDetail;
    }

    /**
     * 是否是管理员
     *
     * @return true/false
     */
    public static boolean isAdmin() {
        UserDetail userDetail = SecurityUtil.loginUser();
        return userDetail.getRoles().contains(Constants.ROLE_ADMIN);
    }

    /**
     * 获取当前登录用户 ID
     *
     * @return 用户 ID
     */
    public static Long currentUserId() {
        UserDetail userDetail = SecurityUtil.loginUser();
        return userDetail.getUserId();
    }

    /**
     * 保存用户信息和 TOKEN
     *
     * @param userDetail 用户信息
     */
    public static void registrationToken(UserDetail userDetail) {
        SpringUtil.getBean(ITokenService.class).saveToken(userDetail);
    }

    /**
     * 解析 TOKEN
     *
     * @param tokenStr TOKEN 字符串
     * @return Token
     * @see Token
     */
    public static UserDetail parseToken(String tokenStr) {
        return SpringUtil.getBean(ITokenService.class).loadUser(tokenStr);
    }

    /**
     * 解析 TOKEN
     *
     * @param token TOKEN
     * @see Token
     */
    public static void removeToken(Token token) {
        SpringUtil.getBean(ITokenService.class).removeToken(token);
    }


    public static void removeToken(Long userId) {
        SpringUtil.getBean(ITokenService.class).removeToken(userId);
    }

    public static String registrationToken(String account, AuthWay authWay) {
        IUserService userService = SpringUtil.getBean(IUserService.class);
        UserDetail userDetail = userService.selectUser(account, authWay);
        Long userId = userDetail.getUserId();
        Set<String> roles = SpringUtil.getBean(IRoleService.class).selectRoleByUserId(userId);
        if (CollUtil.isEmpty(roles)) {
            roles.add(Constants.ROLE_ANONYMOUS);
            roles.add(Constants.ROLE_AUTHENTICATED);
        }

        Resource resource = SpringUtil.getBean(IResourceService.class).selectResourceByUserId(userId);
        Token token = Token.create(userDetail.getUserId(), userDetail.getAccountId(), authWay);
        userDetail.setAuthWay(authWay)
                .setRoles(roles)
                .setResource(resource)
                .setToken(token);
        SecurityUtil.registrationToken(userDetail);
        return token.toString();
    }
}
