import request from '/@/api/http/request';
import { ELocalStorageKey } from "/@/types";

const workspaceId: string = localStorage.getItem(ELocalStorageKey.WorkspaceId) || '';

// Api参数类型
export type FetchFeedbackRecordListApiParams = {
    page: number,
    page_size: number,
};

export type FetchChangeRecordListApiParams = {
    page: number,
    page_size: number,
    begin_time?: number, // 开始时间
    end_time?: number, // 结束时间
    device_name?: string,//设备类型
    logsInformation?: string,//模糊查询内容
};

export type FetchMediaFileListApiParams = Partial<{
    begin_time: number,
    end_time: number,
    template_type: number,
    payload: string,
    search_info: string,
    page: number,
    page_size: number,
}>;

export type BatchDownloadMediaFileApiParams = {
    id: string
};

export type FetchFileListByFolderApiParams = Partial<{
    begin_time: number,
    end_time: number,
    media_type: number,
    search_info: string,
    page: number,
    page_size: number,
}>;

export type BatchDownloadFileApiParams = {
    id: string
};

export type BatchDeletePictureApiParams = {
    id: string
};

export type FetchTrajectoryListApiParams = Partial<{
    begin_time: number,
    end_time: number,
    template_type: number,
    payload: number,
    search_info: string,
    page: number,
    page_size: number,
}>;

// Api函数类型
export type FetchPayloadListApi = () => Promise<any>;
export type FetchFeedbackRecordListApi = (data: FetchFeedbackRecordListApiParams) => Promise<any>;
export type FetchChangeRecordListApi = (params: FetchFeedbackRecordListApiParams) => Promise<any>;
export type FetchProjectListApi = () => Promise<any>;
export type FetchMediaFileListApi = (params: FetchMediaFileListApiParams) => Promise<any>;
export type BatchDownloadMediaFileApi = (params: BatchDownloadMediaFileApiParams) => Promise<any>;
export type FetchFileListByFolderApi = (dirId: string, params: FetchFileListByFolderApiParams) => Promise<any>;
export type FetchFileDetailApi = (fileId: string) => Promise<any>;
export type BatchDownloadFileApi = (params: BatchDownloadFileApiParams) => Promise<any>;
export type CreateMapElementApi = (fileId: string) => Promise<any>;
export type DeleteMapElementApi = (fileId: string) => Promise<any>;
export type UpdateFileNameApi = (fileId: string, data: { [key: string]: any }) => Promise<any>;
export type BatchDeletePictureApi = (params: BatchDeletePictureApiParams) => Promise<any>;
export type FetchTrajectoryListApi = (params: FetchTrajectoryListApiParams) => Promise<any>;
export type FetchTrajectoryMapApi = (taskId: string) => Promise<any>;

// 获取负载列表
const fetchPayloadListApi: FetchPayloadListApi = async () => {
    const res = await request.get(`/manage/api/v1/devices/${workspaceId}/payloads`);
    return res.data;
};

// 获取反馈记录列表
const fetchFeedbackRecordListApi: FetchFeedbackRecordListApi = async (data) => {
    const res = await request.get('/manage/api/v1/devices', { params: data });
    return res.data;
};

// 获取变化记录列表
const fetchChangeRecordListApi: FetchChangeRecordListApi = async (params) => {
    const res = await request.get(`/manage/api/v1/oprlogs/${workspaceId}/devices/logs`, { params: params });
    return res.data;
};

// 获取项目列表
const fetchProjectListApi: FetchProjectListApi = async () => {
    const res = await request.get('/manage/api/v1/workspaces/list');
    return res.data;
};

// 获取文件夹列表
const fetchMediaFileListApi: FetchMediaFileListApi = async (params) => {
    const res = await request.get(`/media/api/v1/files/${workspaceId}/dirs`, { params: params });
    return res.data;
};

// 批量下载文件夹
const batchDownloadMediaFileApi: BatchDownloadMediaFileApi = async (params) => {
    const res = await request.get(`/media/api/v1/files/${workspaceId}/fileDownList`, {
        responseType: 'blob',
        params: params
    });
    return res.data;
};

// 获取文件夹下所有文件
const fetchFileListByFolderApi: FetchFileListByFolderApi = async (dirId, params) => {
    const res = await request.get(`/media/api/v1/files/${workspaceId}/files/${dirId}`, { params: params });
    return res.data;
};

// 获取文件详情
const fetchFileDetailApi: FetchFileDetailApi = async (fileId) => {
    const res = await request.get(`/media/api/v1/files/${workspaceId}/file/${fileId}`);
    return res.data;
};

// 批量下载文件
const batchDownloadFileApi: BatchDownloadFileApi = async (params) => {
    const res = await request.get(`/media/api/v1/files/${workspaceId}/downloadFiles`, {
        responseType: 'blob',
        params: params
    });
    return res.data;
};

// 添加图片元素
const createMapElementApi: CreateMapElementApi = async (fileId) => {
    const url = `/media/api/v1/files/workspaces/${workspaceId}/createMapElement/${fileId}`
    const result = await request.post(url)
    return result.data;
}

// 删除图片元素
const deleteMapElementApi: DeleteMapElementApi = async (fileId) => {
    const url = `/media/api/v1/files/workspaces/${workspaceId}/deleteMapElement/${fileId}`
    const result = await request.delete(url)
    return result.data;
}

// 更新文件名称
const updateFileNameApi: UpdateFileNameApi = async (fileId, data) => {
    const url = `/media/api/v1/files/${workspaceId}/updateFile/${fileId}`
    const result = await request.put(url, data)
    return result.data
}

// 批量删除图片
const batchDeletePictureApi: BatchDeletePictureApi = async (data) => {
    const res = await request.delete(`/media/api/v1/files/${workspaceId}/deleteFiles`, { params: data });
    return res.data;
};

// 获取轨迹列表
const fetchTrajectoryListApi: FetchTrajectoryListApi = async (params) => {
    const url = `/map/api/v1/workspaces/${workspaceId}/flight-tasks`
    const res = await request.get(url, { params: params });
    return res.data;
};

// 获取轨迹地图
const fetchTrajectoryMapApi: FetchTrajectoryMapApi = async (taskId) => {
    const url = `/map/api/v1/workspaces/${workspaceId}/flight-task/${taskId}`
    const res = await request.get(url);
    return res.data;
};

export const apis = {
    fetchPayloadList: fetchPayloadListApi,
    fetchFeedbackRecordList: fetchFeedbackRecordListApi,
    fetchChangeRecordList: fetchChangeRecordListApi,
    fetchProjectList: fetchProjectListApi,
    fetchMediaFileList: fetchMediaFileListApi,
    batchDownloadMediaFile: batchDownloadMediaFileApi,
    fetchFileListByFolder: fetchFileListByFolderApi,
    fetchFileDetail: fetchFileDetailApi,
    batchDownloadFile: batchDownloadFileApi,
    createMapElement: createMapElementApi,
    deleteMapElement: deleteMapElementApi,
    updateFileName: updateFileNameApi,
    batchDeletePicture: batchDeletePictureApi,
    fetchTrajectoryList: fetchTrajectoryListApi,
    fetchTrajectoryMap: fetchTrajectoryMapApi,
};