
import { defHttp } from '/@/utils/http/axios';
import { session } from "/@/utils/Memory.js";
import type { ErrorMessageMode } from '/#/axios';

enum Api {
    CallProvider = '/callProvider',
    Update = '/datastore/file/uploadFile/1fcf0fc166206ceb7f2780302ec8f854',
    Download = '/datastore/file/download'
}

const client = {
    grant_type: "password",
    scope: "all",
    client_id: "space",
    client_secret: 'e10adc3949ba59abbe56e057f20f883e',
}

const interfaceType = { apiUrl2: true };

/**
 * @description:获取组件资源列表
 */
export function list() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "getResourceList");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", "{ 'type': 'SR', 'servicetype': 'WidgetService' }");
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.CallProvider, params: formData, ...interfaceType })
            .then((r) => {
                var e = r.result ? JSON.parse(r.result) : [];
                resolve(e);
            })
    })
}


/**
 * @description:获取资源类型字典
 */
export function getServiceTypes() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.SServicebaseProvider");
    formData.append("methodName", "getSServiceTypes");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", '{"abbreviation":"MR","pid":"MapResource"}');
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.CallProvider, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}


/**
 * @description:获取资源标签
 */
export function queryServiceTags() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.tags.TagsProvider");
    formData.append("methodName", "getAllByType");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", '业务类型,服务专题,数据类型,年度');
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.CallProvider, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}


/**
 * @description:根据标签获取字典
 */
export function queryDicsByName(name) {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.tags.TagsProvider");
    formData.append("methodName", "getAllByType");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", name);
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.CallProvider, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}


/**
 * @description:获取坐标系列表
 */
export function queryCoors() {
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.geomname.GeomNameCDProvider");
    formData.append("methodName", "getGeomNameList");
    formData.append("args[]", session.getItem('tokenV2'));
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.CallProvider, params: formData, ...interfaceType })
            .then((r) => {
                let result = r.result === '' ? [] : JSON.parse(r.result)
                resolve(result);
            })
    })
}

/**
 * @description:新增组件资源
 */
export function insertService(params) {
    if (!params) return
    params.servicebase.appid = "CBEC5C04E50E4088AB9D343314912674";
    params.servicebase.servicetype = 'WidgetService';
    params.metadata.servicetype = 'WidgetService';
    const str = JSON.stringify([params])
    const formData = new URLSearchParams();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "insertList");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", str)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.CallProvider, params: formData, ...interfaceType })
            .then((r) => {
                resolve(r);
            })
    })
}
/**
 * @description:修改组件资源
 */
export function updateService(params) {
    if (!params) return
    params.servicebase.appid = "CBEC5C04E50E4088AB9D343314912674";
    params.servicebase.servicetype = 'WidgetService';
    params.metadata.servicetype = 'WidgetService';
    const str = JSON.stringify(params)
    const formData = new URLSearchParams();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "update");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", str)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.CallProvider, params: formData, ...interfaceType })
            .then((r) => {
                resolve(r);
            })
    })
}



/**
 * @description:删除组件资源
 */
export function deleteService(param) {
    if (!param) return
    const formData = new FormData();
    formData.append("interfaceName", "antu.space.provider.sservicebase.ServiceBaseMetaDataViewProvider");
    formData.append("methodName", "delete");
    formData.append("args[]", session.getItem('tokenV2'));
    formData.append("args[]", param)
    return new Promise<void>((resolve, reject) => {
        defHttp.post({ url: Api.CallProvider, params: formData, ...interfaceType })
            .then((r) => {
                resolve(r);
            })
    })
}


/**
 * @description:文件资源上传
 */
export function update(param, id) {
    return new Promise<void>((resolve, reject) => {
        defHttp.post({
            url: Api.Update,
            params: param,
            headers: {
                bucketId: id,
            }
        })
            .then((r) => {
                resolve(r);
            }).catch(e => {
                resolve()
            })
    })
}

/**
 * @description:文件资源上传
 */
export function getImgUrl(id, pid) {
    return new Promise<void>((resolve, reject) => {
        try {
            defHttp.post({
                url: Api.Download,
                params: {
                    objs: [{ id: id, objType: "FILE", userId: "" }],
                    parentId: pid
                },
            })
                .then((r) => {
                    if (r && r.resp_code == 0 && r.resp_msg == "OK") {
                        resolve(r.datas[0].url);
                    } else {
                        resolve()
                    }
                }).catch(e => {
                    resolve()
                })
        } catch (error) {
            resolve()
        }
    })
}