const { app, BrowserWindow, ipcMain } = require('electron');
const { fork } = require('child_process');
const path = require('path');

let win;
let backend;

function startBackend() {
    backend = fork(path.join(__dirname, 'server.js'));
}

function createWindow() {
    win = new BrowserWindow({
        width: 1200,
        height: 800,
        frame: false,
        titleBarStyle: "hidden",
        webPreferences: {
            preload: path.join(__dirname, "preload.js"),
            nodeIntegration: false,
            contextIsolation: true
        }
    });

    win.setMenu(null);
    win.loadURL('http://localhost:54322');
}

ipcMain.on("win:minimize", () => win.minimize());
ipcMain.on("win:maximize", () => win.maximize());
ipcMain.on("win:close", () => win.close());

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

app.on('window-all-closed', () => {
    if (backend) backend.kill();
    app.quit();
});
