package cn.qinys.platform.base.security.utils;


import cn.qinys.platform.base.redis.RedisKeyEnum;
import cn.qinys.platform.base.security.constants.Constants;
import cn.qinys.platform.base.utils.RedisUtils;
import cn.qinys.platform.base.utils.SpringUtil;
import cn.qinys.platform.base.vo.LoginUserInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author tanlie
 * Date: 2024/1/8 19:16
 */

public class CurrentUtils {


    private CurrentUtils() {

    }

    /**
     * 从token 中获取获取用id
     *
     * @return
     */
    public static String getCurrentUserId() {
        String token = getToken();
        if (token == null) {
            return "0";
        }
        RedisUtils redisUtils = SpringUtil.getBean(RedisUtils.class);
        LoginUserInfo userInfo = (LoginUserInfo) redisUtils.get(RedisKeyEnum.CURRENT_USER.getCode() + token);
        return userInfo == null ? "0" : userInfo.getId();
    }


    /**
     * 从token 中获取获取用id
     *
     * @return
     */
    public static String getCurrentWxId() {
        String token = getToken();
        if (token == null) {
            return null;
        }
        RedisUtils redisUtils = SpringUtil.getBean(RedisUtils.class);
        LoginUserInfo userInfo = (LoginUserInfo) redisUtils.get(RedisKeyEnum.CURRENT_USER.getCode() + token);
        return userInfo == null ? null : userInfo.getWxId();
    }

    /**
     * 从token中获取用户名称
     *
     * @return
     */
    public static String getCurrentUsername() {
        String token = getToken();
        if (token == null) {
            return null;
        }
        RedisUtils redisUtils = SpringUtil.getBean(RedisUtils.class);
        LoginUserInfo userInfo = (LoginUserInfo) redisUtils.get(RedisKeyEnum.CURRENT_USER.getCode() + token);
        return userInfo == null ? "" : userInfo.getUsername();
    }

    /**
     * 登录缓存
     *
     * @param userInfo
     */
    public static void setCurrentUserInfo(LoginUserInfo userInfo, String token) {
        String loginKey = RedisKeyEnum.CURRENT_USER.getCode() + token;
        RedisUtils redisUtils = SpringUtil.getBean(RedisUtils.class);
        redisUtils.set(loginKey, userInfo, RedisKeyEnum.CURRENT_USER.getExpireTime());
    }

    /**
     * 刷新登录缓存
     *
     * @param userInfo
     */
    public static void refresh(LoginUserInfo userInfo) {
        String loginKey = RedisKeyEnum.CURRENT_USER.getCode() + getToken();
        RedisUtils redisUtils = SpringUtil.getBean(RedisUtils.class);
        redisUtils.set(loginKey, userInfo, RedisKeyEnum.CURRENT_USER.getExpireTime());
    }

    /**
     * 从token中获取用户名称
     *
     * @return
     */
    public static LoginUserInfo getCurrentUserInfo() {
        String token = getToken();
        if (token == null) {
            return null;
        }
        RedisUtils redisUtils = SpringUtil.getBean(RedisUtils.class);
        return (LoginUserInfo) redisUtils.get(RedisKeyEnum.CURRENT_USER.getCode() + token);
    }


    /**
     * 从token中获取用户名称
     *
     * @return
     */
    public static List<String> getPermissionCodes() {
        String token = getToken();
        if (token == null) {
            return null;
        }
        RedisUtils redisUtils = SpringUtil.getBean(RedisUtils.class);
        LoginUserInfo userInfo = (LoginUserInfo) redisUtils.get(RedisKeyEnum.CURRENT_USER.getCode() + token);
        return userInfo == null || userInfo.getPermissionCodes() == null ? new ArrayList<>() : userInfo.getPermissionCodes();
    }


    public static String getToken() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return null;
        }
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader(Constants.JwtEnum.AUTHORIZATION.getCode());
        return token == null ? null : token.replaceFirst(Constants.JwtEnum.BEARER.getCode(), "").trim();
    }
}
