console.log('Preload script loaded');
const {contextBridge, ipcRenderer} = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {

    openFileManager: async () => {
        try {
            return await ipcRenderer.invoke('dialog:open');
        } catch (error) {
            console.error('打开文件管理器时出错:', error);
            throw error;
        }
    },
    downloadFile: async (url, fileName, savePath, overwriteFlag, dialogFlag, requestType, data) => {
        try {
            return await ipcRenderer.invoke('download-file', { url, fileName, savePath, overwriteFlag, dialogFlag, requestType, data });
        } catch (error) {
            console.error('下载文件时出错:', error);
            return { success: false, error: error.message };
        }
    },
    deleteFile: (fileName, savePath) => ipcRenderer.send('delete-file', {fileName, savePath}),
    onDeleteFileResponse: (callback) => ipcRenderer.once('delete-file-response', callback),
    uploadFile: async (filePath, uploadUrl) => ipcRenderer.send('upload-file', {filePath, uploadUrl}),
    onUploadFileResponse: (callback) => ipcRenderer.once('upload-file-response', callback),
    // dockerImport 函数现在接受两个参数：filePath 和 tag
    dockerImport: (filePath, tag) => {
        console.log('docker导入算法镜像文件：', filePath, '本地镜像版本：', tag);
        // 发送事件到主进程，并带上文件路径和文件名作为参数
        ipcRenderer.send('docker-import', filePath, tag);
    },
    onDockerImportResponse: (callback) => ipcRenderer.once('docker-import-response', callback),
    updateMap: async (container_name, file_path) => {
        // 异步
        ipcRenderer.send('update-map', {container_name, file_path});
    },
    onUpdateMapResponse: (callback) => ipcRenderer.once('update-map-response', callback),
    runSimulation: async (random_flag, count, obstacle_flag, default_start_flag, default_end_flag, start_point, end_point) => {
        // 异步
        ipcRenderer.send('run-simulation', {random_flag, count, obstacle_flag, default_start_flag, default_end_flag, start_point, end_point});
    },
    onRunSimulationResponse: (callback) => ipcRenderer.once('run-simulation-response', callback),
    generateWorld: (rosbag_path) => {
        // 发送事件到主进程
        ipcRenderer.send('generate-world', {rosbag_path});
    },
    onGenerateWorldResponse: (callback) =>  ipcRenderer.once('generate-world-response', callback),
    startContainer: (zip_file_path, image_name, container_name) => {
        // 发送事件到主进程
        ipcRenderer.send('start-container', {zip_file_path, image_name, container_name});
    },
    onStartContainerResponse: (callback) =>  ipcRenderer.once('start-container-response', callback),
    processEvaluationFiles: async (N, equipmentNo, sceneNo) => {
        return await ipcRenderer.invoke('process-evaluation-files', {N, equipmentNo, sceneNo});
    },
    processMapUpdateFiles: async (equipmentNo, timeStamp) => {
        return await ipcRenderer.invoke('process-map-update-files', {equipmentNo, timeStamp});
    },
});


