package com.fs.ai.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baidubce.appbuilder.base.exception.AppBuilderServerException;
import com.baidubce.appbuilder.console.agentbuilder.AgentBuilder;
import com.baidubce.appbuilder.console.dataset.Dataset;
import com.baidubce.appbuilder.model.agentbuilder.AgentBuilderIterator;
import com.baidubce.appbuilder.model.agentbuilder.AgentBuilderResult;
import com.baidubce.appbuilder.model.dataset.DocumentListResponse;
import com.fs.ai.service.IBaiduAIService;
import com.fs.ai.vo.BaiduAIMsgVO;
import com.fs.ai.vo.BaiduAITokenVO;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

@Service
public class BaiduAIServiceImpl implements IBaiduAIService {

    String clientId="LUP3P6NA0q9CEAmUhjnWXt4Q";
    String clientSecret="Esxtj5QVM2klCZV1HfIRYutW5mJ2AP5G";
    String grantType="client_credentials";
    String plugins="4v4ki9a5t1hxdv92";


    String secretKey="bce-v3/ALTAK-P03N0q1ET2QKrbq3RGdgB/456be350fac6b3fe41ced46cada2cc287e04f5fc";

    public static  BaiduAITokenVO vo=null;

    public BaiduAITokenVO getToken(){
        String url="https://aip.baidubce.com/oauth/2.0/token?client_id="+clientId+"&client_secret="+clientSecret+"&grant_type="+grantType;
        String result = HttpUtil.post(url, "");
        vo=JSONUtil.toBean(result,BaiduAITokenVO.class);
        return vo;
    }

    @Override
    public BaiduAIMsgVO callAI(String msg) {
        this.getToken();
        String url="https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/plugin/"+plugins+"/?access_token="+vo.getAccess_token();
        JSONObject param = JSONUtil.createObj();
        param.set("query", msg);
        param.set("verbose",false);
        String result = HttpUtil.createPost(url)
                .contentType("application/json")
                .body(param.toString()).execute().body();
        BaiduAIMsgVO vo=JSONUtil.toBean(result,BaiduAIMsgVO.class);
        return vo;
    }

    @Override
    public String createDataset(String datasetName) {

        Dataset dataset = new Dataset(secretKey);
        String datasetId = null;
        try {
            datasetId = dataset.createDataset(datasetName);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AppBuilderServerException e) {
            e.printStackTrace();
        }
        return datasetId;

    }



    @Override
    public void delDocument(String datasetId, String docId) {
        Dataset dataset = new Dataset(secretKey,datasetId);
        try {
            dataset.deleteDocument(docId);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AppBuilderServerException e) {
            e.printStackTrace();
        }
    }


    @Override
    public String[] addDocument(String datasetId, String file) {
        Dataset dataset = new Dataset(secretKey,datasetId);
        String[] documentIds = new String[0];
        try {
            documentIds = dataset.addDocuments(new ArrayList<>(Collections.singletonList(file)), false, null, false);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AppBuilderServerException e) {
            e.printStackTrace();
        }
        return documentIds;
    }

    @Override
    public DocumentListResponse getDocument(String datasetId,int page, int limit, String keywork) {
        Dataset dataset = new Dataset(secretKey,datasetId);
        try {
            return dataset.getDocumentList(page,limit,"");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AppBuilderServerException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public AgentBuilderResult run(String appId,String msg) {
        AgentBuilder agentBuilder = new AgentBuilder(appId,secretKey);
//        String conversationId = "9c2bea5f-595d-4952-a8de-4b5eca878fde";
        try {
            String conversationId = agentBuilder.createConversation();
            //String fileId = agentBuilder.uploadLocalFile(conversationId, "c:/fs/uploadPath/upload/2024/04/20/883124fb-1c0b-402f-b19c-7fc4af31519c.pdf");
            AgentBuilderIterator itor =agentBuilder.run(msg, conversationId,null, false);
            while (itor.hasNext()) {
                AgentBuilderResult result = itor.next();
                return result;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AppBuilderServerException e) {
            e.printStackTrace();
        }
        return null;
    }
}
