package org.springblade.modules.app.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springblade.common.auth.UserContext;
import org.springblade.common.auth.UserContextUtil;
import org.springblade.common.auth.support.AuthErrorConstants;
import com.wn.common.validator.Validator;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.modules.app.common.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.app.service.IAppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName: UserUtils
 * @Description:
 * @author: fxs
 * @createTime: 2021-11-08-9:33
 */

@Slf4j
@Component
public class UserUtils {

    @Autowired
    private BladeRedis redis;

    @Autowired
    private IAppUserService userService;

    /**
     * 获取user
     */
//    public static UserDTO getUserDTO() {
//        UserContext userContext = UserContextUtil.getUserContext();
//        Validator.notNull(userContext, ErrorMsg.NEW(AuthErrorConstants.NOT_LOGIN.getCode(), AuthErrorConstants.NOT_LOGIN.getMessage()));
//        String jsonObject = userContext.getJsonObject();
//        UserDTO userDTO = JSON.parseObject(jsonObject, UserDTO.class);
//        Validator.notNull(userDTO, ErrorMsg.NEW(AuthErrorConstants.NOT_LOGIN.getCode(), AuthErrorConstants.NOT_LOGIN.getMessage()));
//        return userDTO;
//    }

    public UserDTO getUser(String id) {
        String token = redis.get(id);
        String key = "xumutong";
        JWT jwt = JWTUtil.parseToken(token);
        Integer exp = (Integer) jwt.getPayload("exp");
        Long now = new Date().getTime()/1000;
		Validator.isTrue(exp.longValue() > now, ErrorMsg.NEW(AuthErrorConstants.NOT_LOGIN.getCode(), AuthErrorConstants.NOT_LOGIN.getMessage()));


		Object object =jwt.getPayload("userDto");
		UserDTO userDTO = JSON.parseObject(object.toString(), UserDTO.class);
		Validator.notNull(userDTO, ErrorMsg.NEW(AuthErrorConstants.NOT_LOGIN.getCode(), AuthErrorConstants.NOT_LOGIN.getMessage()));

		userDTO = userService.getUserById(Long.valueOf(id));
        return userDTO;


    }

    public UserDTO getUserInfo(String token) {
        JWT jwt = JWTUtil.parseToken(token);
        Integer exp = (Integer) jwt.getPayload("exp");
        Long now = new Date().getTime()/1000;
		Validator.isTrue(exp > now, ErrorMsg.NEW(AuthErrorConstants.NOT_LOGIN.getCode(), AuthErrorConstants.NOT_LOGIN.getMessage()));


		Object object =jwt.getPayload("userDto");
		UserDTO userDTO = JSON.parseObject(object.toString(), UserDTO.class);
		Validator.notNull(userDTO, ErrorMsg.NEW(AuthErrorConstants.NOT_LOGIN.getCode(), AuthErrorConstants.NOT_LOGIN.getMessage()));
        return userDTO;


    }

    public static UserDTO getUserDTO(String token) {
        String key = "xumutong";
        JWT jwt = JWTUtil.parseToken(token);
        Integer exp = (Integer) jwt.getPayload("exp");
        Long now = new Date().getTime()/1000;
		Validator.isTrue(exp > now, ErrorMsg.NEW(AuthErrorConstants.NOT_LOGIN.getCode(), AuthErrorConstants.NOT_LOGIN.getMessage()));


		Object object =jwt.getPayload("userDto");
		UserDTO userDTO = JSON.parseObject(object.toString(), UserDTO.class);
		Validator.notNull(userDTO, ErrorMsg.NEW(AuthErrorConstants.NOT_LOGIN.getCode(), AuthErrorConstants.NOT_LOGIN.getMessage()));
        return userDTO;


    }



}
