const { app, BrowserWindow } = require('electron');
const path = require('path');
const fs = require('fs');

let mainWindow;

function createWindow() {
    // 简化图标路径：直接从 app 目录下的 build 文件夹获取
    const iconPath = path.join(__dirname, 'build', 'icon.ico');
    
    mainWindow = new BrowserWindow({
        width: 1200,
        height: 800,
        icon: fs.existsSync(iconPath) ? iconPath : undefined,
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
            enableRemoteModule: true
        }
    });

    mainWindow.loadFile('index.html');
    
    // 开发模式下打开开发者工具
    if (!app.isPackaged || process.env.NODE_ENV === 'development') {
        mainWindow.webContents.openDevTools();
    }

    mainWindow.on('closed', () => {
        mainWindow = null;
    });

    // 打印路径信息用于调试
    console.log('========================================');
    console.log('应用路径信息:');
    console.log('是否打包:', app.isPackaged);
    console.log('图标路径:', iconPath);
    console.log('图标存在:', fs.existsSync(iconPath));
    console.log('当前目录:', __dirname);
    console.log('========================================');
}

app.whenReady().then(() => {
    createWindow();

    app.on('activate', () => {
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow();
        }
    });
});

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});
