package com.backendsys.service.Ai.Aiivh;

import com.backendsys.entity.Ai.Aiivh.AiivhAnchorDTO;
import com.backendsys.entity.Ai.Aiivh.AiivhAnchorPublicDTO;
import com.backendsys.mapper.Ai.Aiivh.AiivhAnchorMapper;
import com.backendsys.service.SDKService.SDKTencent.SDKTencentAiVirtualmanService;
import com.backendsys.utils.ListUtil;
import com.backendsys.utils.MapUtil;
import com.backendsys.utils.response.PageInfoResult;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AiivhAnchorServiceImpl implements AiivhAnchorService {

    @Autowired
    private AiivhAnchorMapper aiivhAnchorMapper;

    @Autowired
    private SDKTencentAiVirtualmanService sdkTencentAiVirtualmanService;

    @Override
    public Map<String, Object> queryAiivhPublicAnchorList(Integer pageNum, Integer pageSize, AiivhAnchorPublicDTO aiivhAnchorPublicDTO) {
        // 分页查询
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
            PageHelper.getLocalPage().setPageSizeZero(true);
        }
        // 分页输出 (自定义)
        List<Map<String, Object>> list = aiivhAnchorMapper.queryAiivhPublicAnchorList(aiivhAnchorPublicDTO);

        PageInfoResult pageInfoResult = new PageInfoResult(list);
        return pageInfoResult.toMap();
    }

    @Override
    public Map<String, Object> queryAiivhAnchorList(Integer pageNum, Integer pageSize, AiivhAnchorDTO aiivhAnchorDTO) {
        // 分页查询
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
            PageHelper.getLocalPage().setPageSizeZero(true);
        }
        // 分页输出 (自定义)
        List<Map<String, Object>> list = aiivhAnchorMapper.queryAiivhAnchorList(aiivhAnchorDTO);

        PageInfoResult pageInfoResult = new PageInfoResult(list);
        return pageInfoResult.toMap();
    }



    /**
     * 获得全部形象列表 (循环调用接口，至到获得全部数据)
     * List<Map<String, Object>> virtualmans = getAllVirtualman(pageIndex, pageSize);
     */
    private List<Map<String, Object>> getAllVirtualman(Integer pageIndex, Integer pageSize) {

        // 初始化变量
        List<Map<String, Object>> timbre_assets = new ArrayList<>();

        // 第一次调用 initTimbre 方法，并将当前页的数据追加到 timbre_assets 列表中
        Map<String, Object> resp = sdkTencentAiVirtualmanService.initPublicVirtualman(pageIndex, pageSize);
        Integer total = (Integer) MapUtil.get(resp, "Payload.Total");
        timbre_assets.addAll((List<Map<String, Object>>) MapUtil.get(resp, "Payload.Virtualmans"));

        // 循环获取所有页的数据
        while (total != null && total > 0 && pageIndex * pageSize <= total) {
            // 准备下一次查询，页码加1
            pageIndex++;
            // System.out.println(pageIndex);

            // 再次调用 initTimbre 方法获取下一页的数据，并将当前页的数据追加到 timbre_assets 列表中
            resp = sdkTencentAiVirtualmanService.initPublicVirtualman(pageIndex, pageSize);
            total = (Integer) MapUtil.get(resp, "Payload.Total");
            timbre_assets.addAll((List<Map<String, Object>>) MapUtil.get(resp, "Payload.Virtualmans"));
        }
        return timbre_assets;
    }
    /**
     * 文档中心 > 腾讯云智能数智人 > API 接入 > 个人资产管理 API 文档-v1.0.9 > 分页查询小样本形象列表接口
     * https://cloud.tencent.com/document/product/1240/93404
     *
     * 将查询到的公共形象存入数据库
     *
     * (有效期：2024/03/25-2024/04/08) 过期后能否继续查询？
     */
    @Override
    public long insertAiivhAnchor(Integer pageIndex, Integer pageSize) {

        List<Map<String, Object>> virtualmans = getAllVirtualman(pageIndex, pageSize);

        // 遍历 virtualmans 列表，并将 key 由大驼峰改为下划线命名
        virtualmans = ListUtil.convertToUnderscoreCase(virtualmans);

        // 清除全部数据
        aiivhAnchorMapper.cleanAiivhPublicAnchor();
        // 重新从远程获取新的形象

        return aiivhAnchorMapper.insertAiivhPublicAnchorBatch(virtualmans);
    }

}
