package com.fdkankan.common.user;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.constant.LoginType;
import com.fdkankan.redis.constant.RedisKey;
import com.fdkankan.redis.util.RedisUtil;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class SSOLoginStore {

    @Resource
    private RedisUtil redisUtil;
    /**
     * get
     * @param userName
     * @return
     */
    public  SSOUser get(String userName) {
        String redisKey = getSsoUserRedisKey(userName);
        String objectValue =  redisUtil.get(redisKey);
        if (objectValue != null) {
            return JSONObject.parseObject(objectValue, SSOUser.class);
        }
        return null;
    }


    @Resource
    @Qualifier("redisTemplate2")
    private RedisTemplate redisTemplate2;

    public  com.fdkankan.common.model.SSOUser getV3(String token) {
        String redisKey = "token#".concat(token);
        Object obj = redisTemplate2.opsForValue().get(redisKey);
        if(Objects.nonNull(obj)){
            return (com.fdkankan.common.model.SSOUser)obj;
        }
        return null;
    }

    /**
     * remove
     * @param loginType     登录类型
     * @param userName      登录用户名
     */
    public  void remove(String loginType,String userName) {
        String redisKey = getTokenRedisKey(loginType,userName);
        if(redisUtil.hasKey(redisKey)){
            redisUtil.del(redisKey);
        }
        if(LoginType.USER.code().equals(loginType)  ){
            String ssoUserRedisKey = getSsoUserRedisKey(userName);
            if(redisUtil.hasKey(ssoUserRedisKey)){
                redisUtil.del(ssoUserRedisKey);
            }
        }
    }

    /**
     *  put
     * @param loginType     登录类型
     * @param userName      登录用户名
     * @param token         token
     * @param time          过期时间，时间单位为秒，默认为 6小时 21600
     * @param ssoUser       sooUser
     */
    public  void put(String loginType, String userName, String token,Integer time, SSOUser ssoUser) {
        if(time == null || time <=0 ){
            time = RedisKey.USER_EXPIRE_TIME;
        }
        String redisKey = getTokenRedisKey(loginType,userName);
        redisUtil.set(redisKey, token, time);
        if(LoginType.USER.code().equals(loginType) && ssoUser!=null ){
            redisUtil.set(getSsoUserRedisKey(userName),JSONObject.toJSONString(ssoUser),time);
        }
    }

    /**
     * 获取token key
     */
    private static String getTokenRedisKey(String loginType,String userName){
        return String.format(RedisKey.TOKEN_USER, loginType,userName);
    }
    private static String getSsoUserRedisKey(String userName){
        return String.format(RedisKey.SSO_USER,userName);
    }


}
