import request from '/@/api/http/request';
import { getWorkspaceId } from '/@/utils/index';

// Api参数类型
export type FetchJobListApiParams = Partial<{
    snList: string,
    begin_time: number,
    end_time: number,
    search_info: string,
    page: number,
    page_size: number,
}>;

export type FetchWaylineListApiParams = Partial<{
    order_by: string,
    template_type: number,
    key: string,
    page: number,
    page_size: number,
}>;

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

export type CreateDeviceFeedbackApiParams = {
    logs_info: string,
    happen_time: number,
    contact_number?: string,
    contact_email?: string,
    oss_ids?: string,
    list?: {
        list: any[],
        module: string,
    }[],
};

export type FetchDeviceHmsListApiParams = Partial<{
    begin_time: number,
    end_time: number,
    level: number,
    language: string,
    message: string,
    page: number,
    page_size: number,
}>;

export type SignLoginApiParams = {
    username: string,
    client_id: string,
    timestamp: number,
    workspace_id: string,
    workspace_name: string,
    signature: string,
};

export type FetchFeedbackRecordListApiParams = Partial<{
    begin_time: number,
    end_time: number,
    status: number,
    username: number,
    search_info: string,
    page: number,
    page_size: number,
}>;

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

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,
}>;

export type AddPilotApiParams = {
    username: number,
    password: number,
};

export type FetchPilotPasswordApiParams = {
    userId: string,
};

export type DeleteDirApiParams = {
    id: string
};

// Api函数类型
export type FetchBingCodeApi = () => Promise<any>;
export type FetchJobListApi = (params: FetchJobListApiParams) => Promise<any>;
export type DeleteJobApi = (params: { job_id: string }) => Promise<any>;
export type RenewalJobApi = (job_id: string) => Promise<any>;
export type FetchJobDetailApi = (job_id: string) => Promise<any>;
export type FetchAllDockDeviceListApi = () => Promise<any>;
export type FetchAllWaylineListApi = () => Promise<any>;
export type FetchWaylinePointApi = (wayline_id: string) => Promise<any>;
export type FetchWaylineListApi = (params: FetchWaylineListApiParams) => Promise<any>;
export type FetchDeviceLogListApi = (sn: string, params: { domain_list: string }) => Promise<any>;
export type FetchDeviceFeedbackRecordListApi = (sn: string, params: FetchDeviceFeedbackRecordListApiParams) => Promise<any>;
export type FetchDeviceLogDetailApi = (params: { logsId: string }) => Promise<any>;
export type CreateDeviceFeedbackApi = (sn: string, data: CreateDeviceFeedbackApiParams) => Promise<any>;
export type FetchDeviceHmsListApi = (data: FetchDeviceHmsListApiParams) => Promise<any>;
export type SignLoginApi = (data: SignLoginApiParams) => Promise<any>;
export type FetchDeviceModelApi = (params?: { flg: boolean }) => Promise<any>;
export type FetchDeviceStatusApi = (params: { snList: string }) => Promise<any>;
export type FetchPayloadListApi = () => Promise<any>;
export type FetchFeedbackRecordListApi = (data: FetchFeedbackRecordListApiParams) => Promise<any>;
export type FetchChangeRecordListApi = (params: FetchChangeRecordListApiParams) => 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 = (workspaceId: string, 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>;
export type AddPilotApi = (data: AddPilotApiParams) => Promise<any>;
export type FetchPilotPasswordApi = (params: FetchPilotPasswordApiParams) => Promise<any>;
export type DeleteDirApi = (params: DeleteDirApiParams) => Promise<any>;

export const getUploadPath = () => {
    return `api/media/api/v1/files/${getWorkspaceId()}/file/upload`;
}

// 获取设备绑定码
const fetchBingCodeApi: FetchBingCodeApi = async () => {
    const url = `/manage/api/v1/workspaces/${getWorkspaceId()}/bingCode`
    const res = await request.get(url);
    return res.data;
};

// 获取任务列表
const fetchJobListApi: FetchJobListApi = async (params) => {
    const url = `/wayline/api/v1/workspaces/${getWorkspaceId()}/jobs`
    const res = await request.get(url, { params: params });
    return res.data;
};

// 删除任务
const deleteJobApi: DeleteJobApi = async (params) => {
    const url = `/wayline/api/v1/workspaces/${getWorkspaceId()}/jobs`
    const res = await request.delete(url, { params: params });
    return res.data;
};

// 恢复任务
const renewalJobApi: RenewalJobApi = async (job_id) => {
    const url = `/wayline/api/v1/workspaces/${getWorkspaceId()}/${job_id}/renewal`
    const res = await request.post(url);
    return res.data;
};

// 获取任务详情
const fetchJobDetailApi: FetchJobDetailApi = async (id) => {
    const url = `/wayline/api/v1/workspaces/${getWorkspaceId()}/jobDetail`
    const res = await request.get(url, { params: { jobId: id } });
    return res.data;
};

// 获取全部机场设备列表
const fetchAllDockDeviceListApi: FetchAllDockDeviceListApi = async () => {
    const url = `/manage/api/v1/devices/${getWorkspaceId()}/getDockDevices`
    const res = await request.get(url);
    return res.data;
};

// 获取全部航线列表
const fetchAllWaylineListApi: FetchAllWaylineListApi = async () => {
    const url = `/wayline/api/v1/workspaces/${getWorkspaceId()}/getWaylines`
    const res = await request.get(url);
    return res.data;
};

// 获取航线点
const fetchWaylinePointApi: FetchWaylinePointApi = async (wayline_id) => {
    const url = `/wayline/api/v1/workspaces/${getWorkspaceId()}/getWaylinePoint/${wayline_id}`
    const res = await request.get(url);
    return res.data;
};

// 获取航线列表
const fetchWaylineListApi: FetchWaylineListApi = async (params) => {
    const url = `/wayline/api/v1/workspaces/${getWorkspaceId()}/waylines`
    const res = await request.get(url, { params: params });
    return res.data;
};

// 获取设备日志列表
const fetchDeviceLogListApi: FetchDeviceLogListApi = async (sn, params) => {
    const res = await request.get(`/manage/api/v1/workspaces/${getWorkspaceId()}/devices/${sn}/logs`, { params: params });
    return res.data;
};

// 获取设备异常反馈记录列表
const fetchDeviceFeedbackRecordListApi: FetchDeviceFeedbackRecordListApi = async (sn, params) => {
    const res = await request.get(`/manage/api/v1/workspaces/${getWorkspaceId()}/devices/${sn}/deviceSnLogsList`, { params: params });
    return res.data;
};

// 获取设备日志详情
const fetchDeviceLogDetailApi: FetchDeviceLogDetailApi = async (params) => {
    const res = await request.get(`/manage/api/v1/workspaces/${getWorkspaceId()}/devices/logsDetail/`, { params: params });
    return res.data;
};

// 创建设备异常反馈
const createDeviceFeedbackApi: CreateDeviceFeedbackApi = async (sn, data) => {
    const res = await request.post(`/manage/api/v1/workspaces/${getWorkspaceId()}/devices/${sn}/createLogs`, data);
    return res.data;
};

// 获取设备告警信息
const fetchDeviceHmsListApi: FetchDeviceHmsListApi = async (data) => {
    const res = await request.get(`/manage/api/v1/devices/${getWorkspaceId()}/devices/hms`, { params: data });
    return res.data;
};

// 密钥登录
const signLoginApi: SignLoginApi = async (data) => {
    const res = await request.post('/manage/api/v1/signLogin', data);
    return res.data;
}

// 获取设备型号
const fetchDeviceModelApi: FetchDeviceModelApi = async (params) => {
    const res = await request.get(`/manage/api/v1/devices/${getWorkspaceId()}/getDeviceType`, { params: params });
    return res.data;
};

// 获取设备状态
const fetchDeviceStatusApi: FetchDeviceStatusApi = async (params) => {
    const res = await request.get(`/manage/api/v1/devices/${getWorkspaceId()}/devices/status`, { params: params });
    return res.data;
};

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

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

// 获取变化记录列表
const fetchChangeRecordListApi: FetchChangeRecordListApi = async (params) => {
    const res = await request.get(`/manage/api/v1/oprlogs/${getWorkspaceId()}/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/${getWorkspaceId()}/dirs`, { params: params });
    return res.data;
};

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

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

// 获取文件详情
const fetchFileDetailApi: FetchFileDetailApi = async (workspaceId, 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/${getWorkspaceId()}/downloadFiles`, {
        responseType: 'blob',
        params: params
    });
    return res.data;
};

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

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

// 更新文件名称
const updateFileNameApi: UpdateFileNameApi = async (fileId, data) => {
    const url = `/media/api/v1/files/${getWorkspaceId()}/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/${getWorkspaceId()}/deleteFiles`, { params: data });
    return res.data;
};

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

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

// 添加飞行员
const addPilotApi: AddPilotApi = async (data) => {
    const url = `/manage/api/v1/users/${getWorkspaceId()}/savePilotUser`
    const res = await request.post(url, data);
    return res.data;
};

// 获取飞行员密码
const fetchPilotPasswordApi: FetchPilotPasswordApi = async (params) => {
    const url = `/manage/api/v1/users/getPwd`
    const res = await request.get(url, { params: params });
    return res.data;
};

// 删除文件夹
const deleteDirApi: DeleteDirApi = async (params) => {
    const res = await request.delete(`/media/api/v1/files/${getWorkspaceId()}/deleteDir`, { params: params });
    return res.data;
};

export const apis = {
    fetchBingCode: fetchBingCodeApi,
    fetchJobList: fetchJobListApi,
    deleteJob: deleteJobApi,
    renewalJob: renewalJobApi,
    fetchJobDetail: fetchJobDetailApi,
    fetchAllDockDeviceList: fetchAllDockDeviceListApi,
    fetchAllWaylineList: fetchAllWaylineListApi,
    fetchWaylinePoint: fetchWaylinePointApi,
    fetchWaylineList: fetchWaylineListApi,
    fetchDeviceLogList: fetchDeviceLogListApi,
    fetchDeviceFeedbackRecordList: fetchDeviceFeedbackRecordListApi,
    fetchDeviceLogDetail: fetchDeviceLogDetailApi,
    createDeviceFeedback: createDeviceFeedbackApi,
    fetchDeviceHmsList: fetchDeviceHmsListApi,
    signLogin: signLoginApi,
    fetchDeviceModel: fetchDeviceModelApi,
    fetchDeviceStatus: fetchDeviceStatusApi,
    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,
    AddPilot: addPilotApi,
    fetchPilotPassword: fetchPilotPasswordApi,
    deleteDir: deleteDirApi,
};