const { app, BrowserWindow, screen, session } = require('electron')
const path = require('path')

const createWindow = () => {
    // 获取主显示器的尺寸
    const primaryDisplay = screen.getPrimaryDisplay()
    const { width, height } = primaryDisplay.workAreaSize

    const win = new BrowserWindow({
        width: width,
        height: height,
        webPreferences: {
            contextIsolation: true,
            nodeIntegration: false
        }
    })

    // 将窗口位置设置为(0,0)，确保它位于屏幕左上角
    win.setPosition(0, 0)

    win.loadFile(path.join(__dirname, '../dist/index.html'))
}

app.on('ready', () => {
    const filter = {
        urls: ['file:///C:/api/*']
    };
    session.defaultSession.webRequest.onBeforeRequest(filter, (details, callback) => {
        const newUrl = details.url.replace('file:///C:/api', 'http://127.0.0.1:8014');
        callback({ redirectURL: newUrl });
    });
});

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

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