package com.sonic.frog.utils;

import com.alibaba.fastjson.JSON;
import com.sonic.common.AppRuntime;
import com.sonic.frog.enums.AgeTypeEnum;
import com.sonic.frog.enums.DeductionTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 统一的 redis key 管理工具类（方便后续的维护和查找）
 *
 * @Author zzhan
 * @Date 2021/9/24
 * @Version 1.0
 */
@Slf4j
@Service
public class RedisKeyUtils {

    @Autowired
    private AppRuntime appRuntime;
    @Value("${spring.profiles.active}")
    private String runMode;


    /**
     * AI字典信息缓存RedisKey
     *
     * @return
     */
    public String aiDictCacheKey() {
        return appRuntime.buildPrefixKey("aiDictCache");
    }

    /**
     * AI音色字典缓存RedisKey
     *
     * @return
     */
    public String aiTimbreDictCacheKey() {
        return appRuntime.buildPrefixKey("aiTimbreDictCache", "v2");
    }

    /**
     * AI用户创建或编辑锁RedisKey
     *
     * @param userId
     * @return
     */
    public String aiUserCreateEditLockKey(Long userId) {
        return appRuntime.buildPrefixKey("lock", "aiUserCreateEdit", userId);
    }

    /**
     * 背景添加锁RedisKey
     *
     * @param userId
     * @return
     */
    public String backgroundAddLockKey(Long userId) {
        return appRuntime.buildPrefixKey("background", "add", userId);
    }

    /**
     * AI用户基础信息key 在sonic_cow服务中使用，构建聊天提示词,图片生成提示词等
     *
     * @param aiId
     * @return
     */
    public String aiUserCacheInfoKey(Long aiId) {
        return "aiUserCacheInfo" + aiId;
    }

    /**
     * 关系等级缓存
     *
     * @param userId
     * @param aiId
     * @return
     */
    public String aiUserHeartbeatLevelCacheKey(Long userId, Long aiId) {
        return appRuntime.buildPrefixKey("cache", "heartbeatLevel", userId, aiId);
    }


    /**
     * 用户购买心动值锁RedisKey
     *
     * @param userId
     * @return
     */
    public String buyHeartbeatValLockKey(Long userId) {
        return appRuntime.buildPrefixKey("lock", "buyHeartbeatVal", userId);
    }

    /**
     * 用户发送礼物锁RedisKey
     *
     * @param userId
     * @return
     */
    public String sendGiftLockKey(Long userId) {
        return appRuntime.buildPrefixKey("lock", "sendGift", userId);
    }

    /**
     * 计算用户心动等级锁RedisKey
     *
     * @param userId
     * @param aiId
     * @return
     */
    public String calcHeartbeatLevelLockKey(Long userId, Long aiId) {
        return appRuntime.buildPrefixKey("lock", "calcHeartbeatLevel", userId);
    }

    /**
     * 计算用户心动等级锁RedisKey
     *
     * @param userId
     * @return
     */
    public String calcHeartbeatRankLockKey(Long userId) {
        return appRuntime.buildPrefixKey("lock", "calcHeartbeatRank", userId);
    }

    /**
     * 计算用户心动分数限流RedisKey
     *
     * @param userId
     * @param aiId
     * @return
     */
    public String calcHeartbeatScoreLimitKey(Long userId, Long aiId) {
        return appRuntime.buildPrefixKey("limit", "calcHeartbeatScore", userId, aiId);
    }

    /**
     * 和AI聊天时的上下文缓存ID（eg: 1010:chat:responseId:xx:xx）
     *
     * @param userId
     * @param aiId
     * @return
     */
    public String chatResponseIdCacheKey(Long userId, Long aiId) {
        if (runMode.equals("test")) {
            return "1010:test:chat:responseId:" + userId + ":" + aiId;
        }
        return "1010:chat:responseId:" + userId + ":" + aiId;
    }

    /**
     * 和AI聊天时的上下文缓存清理任务（eg: 1010:chat:responseId:clear:xx:xx）
     *
     * @param userId
     * @param aiId
     * @return
     */
    public String chatResponseIdClearTaskKey(Long userId, Long aiId) {
        if (runMode.equals("test")) {
            return "1010:test:chat:responseId:clear:" + userId + ":" + aiId;
        }
        return "1010:chat:responseId:clear:" + userId + ":" + aiId;
    }

    /**
     * ai聊天会话次数key
     * 每20次更新到数据库，同时清0
     *
     * @return
     */
    public String aiChatNumKey() {
        return appRuntime.buildPrefixKey("aiChatNum");
    }

    /**
     * 用户心动值总排行榜key
     *
     * @return
     */
    public String heartbeatValTotalRankKey() {
        return appRuntime.buildPrefixKey("rank", "heartbeatValTotal");
    }

    /**
     * meet滑动请求次数
     *
     * @param userId
     * @return
     */
    public String meetSdCount(Long userId) {
        return appRuntime.buildPrefixKey("meetSdCount", userId);
    }

    /**
     * meet成功的概率
     *
     * @param userId
     * @return
     */
    public String meetRateBoKey(Long userId) {
        return appRuntime.buildPrefixKey("meetRateBo", userId);
    }

    /**
     * 限流
     *
     * @param userId
     * @return
     */
    public String meetRcLimit(Long userId) {
        return appRuntime.buildPrefixKey("limit", "meetRc", userId);
    }

    /**
     * 当前用户是否有上报meet成功的次数
     *
     * @param userId
     * @param aiId
     * @return
     */
    public String uploadMeet(Long userId, Long aiId) {
        return appRuntime.buildPrefixKey("uploadMeet", userId, aiId);
    }

    /**
     * 签到时使用的redis锁
     *
     * @param userId
     * @return
     */
    public String signInLockKey(Long userId) {
        return appRuntime.buildPrefixKey("lock", "signIn", userId);
    }

    /**
     * 首页不同分类缓存RedisKey
     *
     * @param codeStr
     * @return
     */
    public String homeClassificationCacheKey(String codeStr, List<Integer> sexList, List<AgeTypeEnum> ageList, int minLiked, int maxLiked) {
        return appRuntime.buildPrefixKey("homeClassificationCache", codeStr, JSON.toJSONString(sexList), JSON.toJSONString(ageList), minLiked, maxLiked);
    }

    /**
     * 热聊榜单
     *
     * @return
     */
    public String chatRankKey() {
        return appRuntime.buildPrefixKey("rank", "chat");
    }

    /**
     * 心动榜单
     *
     * @return
     */
    public String aiHeartbeatRankKey() {
        return appRuntime.buildPrefixKey("rank", "aiHeartbeat");
    }

    /**
     * 礼物榜单
     *
     * @return
     */
    public String aiGiftRankKey() {
        return appRuntime.buildPrefixKey("rank", "gift");
    }

    /**
     * 购买创建图片次数锁RedisKey
     *
     * @param userId
     * @return
     */
    public String buyCreateImageCountLockKey(Long userId) {
        return appRuntime.buildPrefixKey("buyCreateImageCount", userId);
    }

    /**
     * 解锁爱慕者锁RedisKey
     *
     * @param userId
     * @return
     */
    public String unlockLikeYouLockKey(Long userId) {
        return appRuntime.buildPrefixKey("unlockLikeYou", userId);
    }

    /**
     * 用户文本，语音，语音通话预扣款统计锁RedisKey
     *
     * @param userId
     * @param aiId
     * @param deductionType
     */
    public String userDeductionsStatLockKey(Long userId, Long aiId, DeductionTypeEnum deductionType) {
        return appRuntime.buildPrefixKey("userDeductionsStat", userId, aiId, deductionType.name());
    }

    /**
     * meet上报的锁
     *
     * @param userId
     * @return
     */
    public String meetSdLockKey(Long userId) {
        return appRuntime.buildPrefixKey("lock", "meetSd", userId);
    }

    /**
     * 用户余额不足时，检查 checkout
     *
     * @param userId
     * @return
     */
    public String userBalanceInsufficientCheckoutLockKey(Long userId) {
        return appRuntime.buildPrefixKey("userBalanceInsufficientCheckout", userId);
    }

    /**
     * 相册图片解锁锁键
     *
     * @param userId
     * @return
     */
    public String unlockAlbumImgLockKey(Long userId, Long albumId) {
        return appRuntime.buildPrefixKey("unlockAlbumImg", userId, albumId);
    }

    /**
     * ai轮播列表缓存RedisKey
     */
    public String aiCarouselListKey() {
        return appRuntime.buildPrefixKey("aiCarouselList");
    }

    /**
     * 更新最后聊天时间判断创建人关系
     *
     * @param userId
     * @return
     */
    public String creatorAiUserRelationKey(Long userId, Long aiId) {
        return appRuntime.buildPrefixKey("limit", "updateLastChatTime", userId, aiId);
    }

    /**
     * 更新最后一次聊天时间
     * @param aiId
     * @return
     */
    public String updateLastChatTimeLimitKey(Long aiId) {
        return appRuntime.buildPrefixKey("limit", "updateLastChatTime", aiId);
    }

    /**
     * 首页数据缓存
     * @return
     */
    public String homeRecommendV2CacheKey() {
        return appRuntime.buildPrefixKey("recommend", "cache", "v2", "data");
    }

}
