package com.sonic.cow.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sonic.cow.client.response.ResponseChatResponse;
import com.sonic.cow.service.FunctionCallService;
import com.sonic.cow.tools.GetAiImageArgs;
import com.sonic.frog.lib.client.AiUserAlbumClient;
import com.sonic.frog.lib.output.AIUserAlbumApiOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class FunctionCallServiceImpl implements FunctionCallService {

    @Autowired
    private AiUserAlbumClient aiUserAlbumClient;

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public AIUserAlbumApiOutput callFunction(ResponseChatResponse response) {
        //处理获取AI未解锁图片的方法调用
        if ("get_ai_image".equals(response.getFunctionCallName())) {
            //解析入参
            String argumentsJson = response.getFunctionCallArguments();
            log.info("===> callFunction argumentsJson : {}", argumentsJson);
            GetAiImageArgs tool_args = null;
            try {
                tool_args = objectMapper.readValue(argumentsJson, GetAiImageArgs.class);
            } catch (JsonProcessingException e) {
                log.error("===> callFunction 解析 get_ai_image 参数时出错: " + argumentsJson + " - " + e.getMessage());
            }
            //调用方法获取待解锁图片
            AIUserAlbumApiOutput toolResult = aiUserAlbumClient.getRandomLockImage(tool_args.getUserId(), tool_args.getAiId());
            log.info("===> callFunction 工具执行结果 id : {}, result : {}", response.getFunctionCallId(), toolResult);
            //返回 图片
            return toolResult;
        }
        return null;
    }

}
