package com.kmall.api.cache;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.kmall.api.entity.TokenEntity;
import com.kmall.api.entity.UserVo;
import com.kmall.common.utils.StringUtils;
import com.kmall.manager.manager.redis.JedisUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 会员缓存
 *
 * @author Scott
 */
public class UserTokenCache {
    private static Log logger = LogFactory.getLog(UserTokenCache.class);
    static SimplePropertyPreFilter filter = new SimplePropertyPreFilter();

    /**
     * 存放user和token的关联关系
     */
    public static final String XCX_TOKEN_USER_PREFIXX = "xcx_token_user_";
    public static final String XCX_USER_TOKEN_PREFIXX = "xcx_user_token_";
    public static final String XCX_USER_INFO_PREFIXX = "xcx_user_info_";
    public static final String XCX_TOKEN_STORE_PREFIXX = "xcx_store_token_";
    public static int wsTokenExpireTime = 3600 * 24 * 30;

    /**
     * 根据用户Id
     *
     * @param userId
     * @return
     */
    public static TokenEntity getUserTokenByUserId(Long userId) {
        String temp = JedisUtil.get(toUserTokenKey(userId));
        if (!org.springframework.util.StringUtils.isEmpty(temp)) {
            TokenEntity d = JSON.parseObject(temp, TokenEntity.class);
            return d;
        } else {
            return null;
        }
    }

    /**
     * 根据token
     *
     * @param token
     * @return
     */
    public static TokenEntity getUserInfoByToken(String token) {
        String temp = JedisUtil.get(toTokenUserKey(token));
        if (StringUtils.isNullOrEmpty(temp)) {
            return null;
        }
        String userID = toUserTokenKey(Long.valueOf(temp));
        String tokenEntityJson = JedisUtil.get(userID);
        if (!org.springframework.util.StringUtils.isEmpty(tokenEntityJson)) {
            TokenEntity d = JSON.parseObject(tokenEntityJson, TokenEntity.class);
            return d;
        } else {
            return null;
        }
    }


    /**
     * 当用户拒绝授权时，根据门店token获取保存的信息
     * @param token
     * @return
     */
    public static TokenEntity getStoreByTokenByRefused(String token) {
        String tokenKey = toTokenStoreKey(token);
        String temp = JedisUtil.get(tokenKey);
        if (StringUtils.isNullOrEmpty(temp)) {
            return null;
        }
//        String userID = toUserTokenKey(Long.valueOf(temp));
//        String tokenEntityJson = JedisUtil.get(temp);
        if (!org.springframework.util.StringUtils.isEmpty(temp)) {
            TokenEntity d = JSON.parseObject(temp, TokenEntity.class);
            return d;
        } else {
            return null;
        }
    }

    public static void del(Long userId) {
        JedisUtil.del(toUserTokenKey(userId));
    }

    /**
     * 放置用户Id和token关系
     *
     * @param tokenEntity
     */
    public static void putUserToken(TokenEntity tokenEntity) {
        if(tokenEntity.getUserId() != null && (tokenEntity.getIsRefusedLogin()==null||!tokenEntity.getIsRefusedLogin().equalsIgnoreCase("true"))) {
            JedisUtil.set(toUserTokenKey(tokenEntity.getUserId()), fromTokenCacheString(tokenEntity), wsTokenExpireTime);
        }
        if(tokenEntity.getToken() != null && tokenEntity.getUserId() != null && (tokenEntity.getIsRefusedLogin()==null ||
                !tokenEntity.getIsRefusedLogin().equalsIgnoreCase("true"))) {
            JedisUtil.set(toTokenUserKey(tokenEntity.getToken()), tokenEntity.getUserId().toString(), wsTokenExpireTime);
        }
        if(tokenEntity.getIsRefusedLogin()!=null){
            if(tokenEntity.getStoreId() != null && tokenEntity.getIsRefusedLogin().equalsIgnoreCase("true")){
                String tokenKey = toTokenStoreKey(tokenEntity.getToken());
                JedisUtil.set(tokenKey, fromTokenCacheString(tokenEntity), wsTokenExpireTime);
            }
        }
    }

    private static String fromTokenCacheString(TokenEntity d) {
        if (d == null)
            return null;
        return JSON.toJSONString(d, filter);
    }

    private static String toTokenStoreKey(String token) {
        return XCX_TOKEN_STORE_PREFIXX + token;
    }

    private static String toUserTokenKey(Long userId) {
        return XCX_TOKEN_USER_PREFIXX + userId;
    }

    private static String toTokenUserKey(String token) {
        return XCX_USER_TOKEN_PREFIXX + token;
    }

    private static String toUserInfoKey(Long userId) {
        return XCX_USER_INFO_PREFIXX + userId;
    }

    public static UserVo getUserInfo(Long userId) {
//        logger.error("getUserInfo key:" + toUserInfoKey(userId));
        String temp = JedisUtil.get(toUserInfoKey(userId));
//        logger.error("getUserInfo temp:" + temp);
        if (!org.springframework.util.StringUtils.isEmpty(temp)) {
            UserVo d = JSON.parseObject(temp, UserVo.class);
            return d;
        } else {
            return null;
        }
    }

    public static void putUserInfo(UserVo userVo) {
        JedisUtil.set(toUserInfoKey(userVo.getId()), fromUserCacheString(userVo), wsTokenExpireTime);
    }

    private static String fromUserCacheString(UserVo d) {
        if (d == null)
            return null;
        return JSON.toJSONString(d, filter);
    }
}
