package com.sonic.frog.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Sets;
import com.sonic.frog.dao.AiUserDao;
import com.sonic.frog.domain.entity.AiUser;
import com.sonic.frog.domain.entity.AiUserExt;
import com.sonic.frog.domain.entity.TimbreDict;
import com.sonic.frog.domain.output.AiInfoApiOutput;
import com.sonic.frog.service.AiDictService;
import com.sonic.frog.service.AiUserExtService;
import com.sonic.frog.service.AiUserService;
import com.sonic.frog.service.TimbreDictService;
import com.sonic.frog.utils.BeanConver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * AI用户表服务实现类
 */
@Service
@Slf4j
public class AiUserServiceImpl extends ServiceImpl<AiUserDao, AiUser> implements AiUserService {

    @Autowired
    private AiUserExtService aiUserExtService;
    @Autowired
    private AiDictService aiDictService;
    @Autowired
    private TimbreDictService timbreDictService;


    @Override
    public AiInfoApiOutput getAiUserInfo(Long aiId) {
        //获取ai用户信息
        AiUser aiUser = getOne(Wrappers.<AiUser>lambdaQuery().eq(AiUser::getAiId, aiId));
        //获取ai用户扩展信息
        AiUserExt aiUserExt = aiUserExtService.getOne(Wrappers.<AiUserExt>lambdaQuery().eq(AiUserExt::getAiId, aiId));
        //角色, 性格，标签名称
        AiInfoApiOutput output = BeanConver.copeBean(aiUser, AiInfoApiOutput.class);
        Map<String, String> aiDictNameMap = aiDictService.mapNameByCodeList(Sets.newHashSet(aiUser.getRoleCode(), aiUser.getCharacterCode(), aiUser.getTagCode()));
        output.setUserId(aiUser.getUserId());
        output.setRoleName(aiDictNameMap.get(aiUser.getRoleCode()));
        output.setCharacterName(aiDictNameMap.get(aiUser.getCharacterCode()));
        output.setTagName(aiDictNameMap.get(aiUser.getTagCode()));
        output.setProfile(aiUserExt.getProfile());
        output.setUserProfileExtJson(aiUserExt.getUserProfileExtJson());
        output.setDialogueStyle(aiUserExt.getDialogueStyle());
        output.setDialoguePrologue(aiUserExt.getDialoguePrologue());
        //基图
        output.setBaseImageUrl(aiUser.getBaseImageUrl());
        //语音类型，音高，语速
        TimbreDict timbreDict = timbreDictService.getTimbreDictByCode(aiUserExt.getDialogueTimbreCode());
        output.setVoiceType(timbreDict != null ? timbreDict.getVoiceType() : null);
        output.setDialoguePitch(aiUserExt.getDialoguePitch());
        output.setDialogueSpeechRate(aiUserExt.getDialogueSpeechRate());
        return output;
    }
}