package com.sonic.frog.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.sonic.bear.lib.output.BaseUserInfoOutput;
import com.sonic.frog.domain.entity.AiUserHeartbeatRelation;
import com.sonic.frog.domain.entity.ChatSet;
import com.sonic.frog.domain.entity.Liked;
import com.sonic.frog.domain.entity.UserAiMeet;
import com.sonic.frog.domain.output.AiChatInfoOutput;
import com.sonic.frog.enums.HeartbeatLevelEnum;
import com.sonic.frog.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AiChatInfoServiceImpl implements AiChatInfoService {

    @Autowired
    private ChatSetService chatSetService;
    @Autowired
    private AiUserHeartbeatRelationService aiUserHeartbeatRelationService;
    @Autowired
    private UserService userService;
    @Autowired
    private LikedService likedService;
    @Autowired
    private HeartbeatLevelDictService heartbeatLevelDictService;

    @Override
    public AiChatInfoOutput getAiChatInfo(Long userId, Long aiId) {
        AiChatInfoOutput aiChatInfoOutput = new AiChatInfoOutput();
        //获取当前用户基础信息
        BaseUserInfoOutput baseUserInfo = userService.baseUserInfo(userId);
        //昵称，性别，生日初始用户基础信息
        aiChatInfoOutput.setNickname(baseUserInfo.getNickname());
        aiChatInfoOutput.setSex(baseUserInfo.getSex());
        aiChatInfoOutput.setBirthday(baseUserInfo.getBirthday());
        //查询基础 信息
        ChatSet chatSet = chatSetService.getOne(Wrappers.<ChatSet>lambdaQuery().eq(ChatSet::getUserId, userId).eq(ChatSet::getAiId, aiId));
        if (chatSet != null) {
            aiChatInfoOutput.setNickname(StringUtils.isNotEmpty(chatSet.getNickname()) ? chatSet.getNickname() : baseUserInfo.getNickname());
            aiChatInfoOutput.setSex(chatSet.getSex());
            aiChatInfoOutput.setBirthday(chatSet.getBirthday() != null ? chatSet.getBirthday() : baseUserInfo.getBirthday());
            aiChatInfoOutput.setWhoAmI(StringUtils.isNotEmpty(chatSet.getWhoAmI()) ? chatSet.getWhoAmI() : "");
        }
        //查询相识天数
        AiUserHeartbeatRelation aiUserHeartbeatRelation = aiUserHeartbeatRelationService.getOne(Wrappers.<AiUserHeartbeatRelation>lambdaQuery().eq(AiUserHeartbeatRelation::getUserId, userId).eq(AiUserHeartbeatRelation::getAiId, aiId));
        //解锁了哪些心动等级
        aiChatInfoOutput.setUnlockHearbeatLevelList(Lists.newArrayList());
        if (aiUserHeartbeatRelation != null) {
            aiChatInfoOutput.setDayCount(aiUserHeartbeatRelation.getDayCount());
            aiChatInfoOutput.setHeartbeatLevel(aiUserHeartbeatRelation.getHeartbeatLevel() == null ? HeartbeatLevelEnum.LEVEL_1 : aiUserHeartbeatRelation.getHeartbeatLevel());
            aiChatInfoOutput.setRelationStage(aiUserHeartbeatRelation.getHeartbeatLevel() == null ? HeartbeatLevelEnum.LEVEL_1.getRelationStage() : aiUserHeartbeatRelation.getHeartbeatLevel().getRelationStage());
            aiChatInfoOutput.setUnlockHearbeatLevelList(heartbeatLevelDictService.getUnlockListByCode(aiChatInfoOutput.getHeartbeatLevel()));
        } else {
            aiChatInfoOutput.setDayCount(0);
            aiChatInfoOutput.setHeartbeatLevel(HeartbeatLevelEnum.LEVEL_1);
            aiChatInfoOutput.setRelationStage(HeartbeatLevelEnum.LEVEL_1.getRelationStage());
        }
        //查询左滑又滑相识
        int count = likedService.count(Wrappers.<Liked>lambdaQuery().eq(Liked::getLikedUserId, userId).eq(Liked::getAiId, aiId));
        aiChatInfoOutput.setMeet(count > 0);
        return aiChatInfoOutput;
    }

}
