package com.sonic.frog.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sonic.frog.dao.ChatBubbleDictDao;
import com.sonic.frog.domain.entity.ChatBubbleDict;
import com.sonic.frog.domain.output.ChatBubbleOutput;
import com.sonic.frog.enums.BubbleUnlockTypeEnum;
import com.sonic.frog.enums.HeartbeatLevelEnum;
import com.sonic.frog.service.AiUserHeartbeatRelationService;
import com.sonic.frog.service.ChatBubbleDictService;
import com.sonic.frog.service.HeartbeatLevelDictService;
import com.sonic.frog.utils.BeanConvert;
import com.sonic.lion.lib.client.SubscribeClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 聊天气泡字典服务实现类
 */
@Service
@Slf4j
public class ChatBubbleDictServiceImpl extends ServiceImpl<ChatBubbleDictDao, ChatBubbleDict> implements ChatBubbleDictService {

    @Autowired
    private SubscribeClient subscribeClient;
    @Autowired
    private AiUserHeartbeatRelationService aiUserHeartbeatRelationService;
    @Autowired
    private HeartbeatLevelDictService heartbeatLevelDictService;

    @Override
    public ChatBubbleDict getChatBubbleDictByCode(String chatBubbleCode) {
        return getOne(Wrappers.<ChatBubbleDict>lambdaQuery().eq(ChatBubbleDict::getCode, chatBubbleCode).last("limit 1"));
    }

    @Override
    public String getBubbleNameByCode(String bubbleCode) {
        ChatBubbleDict chatBubbleDict = getChatBubbleDictByCode(bubbleCode);
        return chatBubbleDict != null ? chatBubbleDict.getName() : null;
    }

    @Override
    public ChatBubbleDict getDefaultChatBubble() {
        return getOne(Wrappers.<ChatBubbleDict>lambdaQuery().eq(ChatBubbleDict::getIsDefault, true).last("limit 1"));
    }

    @Override
    public ChatBubbleDict getUserChatBubble(Long userId, Long aiId, String userSetBubbleCode) {
        //用户设置了聊天气泡，根据聊天气泡解锁类型判断
        if (userSetBubbleCode != null) {
            ChatBubbleDict chatBubbleDict = getChatBubbleDictByCode(userSetBubbleCode);
            if (BubbleUnlockTypeEnum.MEMBER.equals(chatBubbleDict.getUnlockType())) {
                //是否是会员
                Boolean isMember = subscribeClient.queryUserIsSubscribe(userId);
                if (isMember) {
                    //会员才解锁
                    return chatBubbleDict;
                }
            } else if (BubbleUnlockTypeEnum.HEARTBEAT_LEVEL.equals(chatBubbleDict.getUnlockType())) {
                HeartbeatLevelEnum unlockHeartbeatLevel = chatBubbleDict.getUnlockHeartbeatLevel();
                //用户与AI的心动等级
                HeartbeatLevelEnum heartbeatLevelEnum = aiUserHeartbeatRelationService.getHeartbeatLevel(userId, aiId);
                //解锁了哪些等级
                List<HeartbeatLevelEnum> unlockList = heartbeatLevelDictService.getUnlockListByCode(heartbeatLevelEnum);
                //达到对应的心动等级才解锁
                if (unlockList.contains(unlockHeartbeatLevel)) {
                    return chatBubbleDict;
                }
            } else {
                return chatBubbleDict;
            }
        }
        return getDefaultChatBubble();
    }
}