package cn.exploring.engine.server.wx.service;

import cn.exploring.engine.server.core.util.CharUtil;
import cn.exploring.engine.server.db.service.WxUserActionService;
import cn.exploring.engine.server.db.util.RedisUtil;
import cn.exploring.engine.server.wx.dao.UserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 维护用户token
 */
@Component
public class UserTokenManager {
    private static final String PREFIX_TOKEN2OBJ = "bi-token2obj-";  // redis中存储的token：usertokenjson前缀
    private static final String PREFIX_ID2OBJ = "bi-id2obj-"; // redis中存储的userid：usertokenjson前缀
    private static final Long REDIS_EXPIRETIME = 365L;  // token过期时间
    private static final TimeUnit REDIS_TIMEUNIT = TimeUnit.DAYS;  // 过期时间类型

    private static RedisUtil redisUtil;
    @Autowired
    public void setRedisUtil(RedisUtil redisUtil) {
        UserTokenManager.redisUtil = redisUtil;
    }
    private static WxUserActionService wxUserActionService;
    @Autowired
    public void setWxUserActionService(WxUserActionService wxUserActionService) {
        UserTokenManager.wxUserActionService = wxUserActionService;
    }

    /**
     * 获取token,并更新redis中的token过期时间
     * @param token
     * @return
     */
    public static Integer getUserId(String token) {
        Object userTokenJsonObj = redisUtil.getCache(PREFIX_TOKEN2OBJ + token);
        if (userTokenJsonObj == null) {
            return null;
        }
        UserToken userToken = UserToken.toObj(userTokenJsonObj);
        if (userToken == null) {
            return null;
        }

        wxUserActionService.action(userToken.getUpdateTime(), userToken.getUserId());

        // 更新过期时间
        redisUtil.setCache(PREFIX_TOKEN2OBJ + token, userToken.toString(), REDIS_EXPIRETIME, REDIS_TIMEUNIT);
        redisUtil.setCache(PREFIX_ID2OBJ + userToken.getUserId(), userToken.toString(), REDIS_EXPIRETIME, REDIS_TIMEUNIT);

        return userToken.getUserId();
    }

    /**
     * 生成token
     * @param id
     * @return
     */
    public static UserToken generateToken(Integer id, String sessionKey) {
        UserToken userToken = null;

        String token = CharUtil.getRandomString(32);
        while (redisUtil.exists(PREFIX_TOKEN2OBJ + token)) {
            token = CharUtil.getRandomString(32);
        }

        userToken = new UserToken();
        userToken.setToken(token);
        userToken.setUserId(id);
        userToken.setSessionKey(sessionKey);

        wxUserActionService.action(userToken.getUpdateTime(), userToken.getUserId());

        redisUtil.setCache(PREFIX_TOKEN2OBJ + token, userToken.toString(), REDIS_EXPIRETIME, REDIS_TIMEUNIT);
        redisUtil.setCache(PREFIX_ID2OBJ + id, userToken.toString(), REDIS_EXPIRETIME, REDIS_TIMEUNIT);

        return userToken;
    }

    /**
     * 删除token
     * @param userId
     */
    public static void removeToken(Integer userId) {
        Object userTokenJsonObj = redisUtil.getCache(PREFIX_ID2OBJ + userId);
        if (userTokenJsonObj != null) {
            UserToken userToken = UserToken.toObj(userTokenJsonObj);
            if (userToken != null) {
                redisUtil.remove(PREFIX_TOKEN2OBJ + userToken.getToken());
            }

            redisUtil.remove(PREFIX_ID2OBJ + userId);
        }
    }
}
