package com.fs.fastgptApi.service.Impl;


import com.alibaba.fastjson.JSON;
import com.fs.fastgptApi.config.FastGptApiConfig;
import com.fs.fastgptApi.param.*;
import com.fs.fastgptApi.result.*;
import com.fs.fastgptApi.service.DataSetCollectionService;
import com.fs.fastgptApi.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

/**
 * 集合
 */
@Service
public class DataSetCollectionServiceImpl implements DataSetCollectionService {
    private static final Logger log = LoggerFactory.getLogger(DataSetCollectionServiceImpl.class);

    /**
     * 创建一个空的集合
     */
    @Override
    public KnowledgeBaseResult createEmptyCollection(CreateCollectionParam param,String url,String key) {
//        String json = HttpUtil.sendPost(param, url + FastGptApiConfig.createEmptyCollection, FastGptApiConfig.Key);
        String json = HttpUtil.sendPost(param, url + FastGptApiConfig.createEmptyCollection, key);
        KnowledgeBaseResult result = JSON.parseObject(json, KnowledgeBaseResult.class);
        return result;
    }

    /**
     * 创建一个纯文本集合
     */
    @Override
    public CreateTextCollectionResult createTextCollection(CreateTextCollectionParam param,String url,String key) {
//        String json = HttpUtil.sendPost(param, url + FastGptApiConfig.createTextCollection, FastGptApiConfig.Key);
        String json = HttpUtil.sendPost(param, url + FastGptApiConfig.createTextCollection, key);
        CreateTextCollectionResult result = JSON.parseObject(json, CreateTextCollectionResult.class);
        return result;
    }

    /**
     * 创建一个链接结合
     */
    @Override
    public CreateLinkCollectionResult createLinkCollection(CreateLinkCollectionParam param,String url,String key) {
//        String json = HttpUtil.sendPost(param, url + FastGptApiConfig.createLinkCollection, FastGptApiConfig.Key);
        String json = HttpUtil.sendPost(param, url + FastGptApiConfig.createLinkCollection, key);
        CreateLinkCollectionResult result = JSON.parseObject(json, CreateLinkCollectionResult.class);
        return result;
    }


    /**
     * 创建一个文件集合
     */
    @Override
    public CreateFileCollectionResult createFileCollection(File file,String filaName,UploadFileParam param,String url,String key) throws IOException {
//        String json = HttpUtil.uploadFile(file,param,url + FastGptApiConfig.createFileCollection, FastGptApiConfig.Key);
        String json = HttpUtil.uploadFile(file,filaName,param,url + FastGptApiConfig.createFileCollection,key);
        CreateFileCollectionResult result = JSON.parseObject(json, CreateFileCollectionResult.class);
        return result;
    }

    /**
     *  创建一个外部文件库集合（商业版）
     */
    @Override
    public CreateExternalFileCollectionResult createExternalFileCollection(CreateExternalFileCollectionParam param,String url,String key) {
//        String json = HttpUtil.sendPost(param, url + FastGptApiConfig.createExternalFileCollection, FastGptApiConfig.Key);
        String json = HttpUtil.sendPost(param, url + FastGptApiConfig.createExternalFileCollection, key);
        CreateExternalFileCollectionResult result = JSON.parseObject(json, CreateExternalFileCollectionResult.class);
        return result;
    }

    /**
     * 获取集合列表
     */
    @Override
    public GetCollectionListResult getCollectionList(GetCollectionListParam param,String url,String key) {
//        String json = HttpUtil.sendPost(param, url + FastGptApiConfig.getCollectionList, FastGptApiConfig.Key);
        String json = HttpUtil.sendPost(param, url + FastGptApiConfig.getCollectionList, key);
        GetCollectionListResult result = JSON.parseObject(json, GetCollectionListResult.class);
        return result;
    }

    /**
     * 获取集合详情
     */
    @Override
    public GetCollectionDetailResult getCollectionDetail(String ID,String url,String key) {
//        String json = HttpUtil.sendGet(ID, url + FastGptApiConfig.getCollectionDetail, FastGptApiConfig.Key);
        String json = HttpUtil.sendGet(ID, url + FastGptApiConfig.getCollectionDetail, key);
        GetCollectionDetailResult result = JSON.parseObject(json, GetCollectionDetailResult.class);
        return result;
    }


    /**
     * 修改集合信息(通过集合 ID 修改集合信息)
     */
    @Override
    public KnowledgeBaseResult updateCollectionInfoById(UpdateCollectionByIdParam param,String url,String key) {
//        String json = HttpUtil.sendPut(param, url + FastGptApiConfig.updateCollection, FastGptApiConfig.Key);
        String json = HttpUtil.sendPut(param, url + FastGptApiConfig.updateCollection, key);
        KnowledgeBaseResult result = JSON.parseObject(json, KnowledgeBaseResult.class);
        return result;
    }

    /**
     * 修改集合信息(通过外部文件 ID 修改集合信息， 只需要把 id 换成 datasetId 和 externalFileId。)
     */
    @Override
    public KnowledgeBaseResult updateCollectionInfoByIdByDatasetId(UpdateCollectionByDatasetIdParam param,String url,String key) {
//        String json = HttpUtil.sendPut(param, url + FastGptApiConfig.updateCollection, FastGptApiConfig.Key);
        String json = HttpUtil.sendPut(param, url + FastGptApiConfig.updateCollection, key);
        KnowledgeBaseResult result = JSON.parseObject(json, KnowledgeBaseResult.class);
        return result;
    }

    /**
     *  删除一个集合
     */
    @Override
    public KnowledgeBaseResult deleteCollection(String ID,String url,String key) {
//        String json = HttpUtil.sendDelete(ID, url + FastGptApiConfig.deleteCollection, FastGptApiConfig.Key);
        String json = HttpUtil.sendDelete(ID, url + FastGptApiConfig.deleteCollection, key);
        KnowledgeBaseResult result = JSON.parseObject(json, KnowledgeBaseResult.class);
        return result;
    }

}
