const { spawn } = require('node:child_process')

let command;
let browserOpen = false;

async function openBrowser() {
    if(!browserOpen) {
        // const command = spawn("chromium-browser --display=:0 --noerrors --disable-session-crashed-bubble --disable-infobars --start-fullscreen http://localhost:3000 &")
        command = spawn('sudo', ['-upi', 'chromium-browser', '--display=:0', '--incognito', '--noerrors', '--hide-crash-restore-bubble', '--disable-infobars', '--kiosk', 'http://localhost']);
    
        command.stdout.on('data', data => {
            console.log("stdout: ", data.toString());
        });
        
        command.stderr.on('data', data => {
            console.log("sterr: ", data.toString());
        });
        browserOpen = true;
    }
}

async function killBrowser() {
    if(browserOpen) {
        command.kill();
        browserOpen = false;
    }
}

module.exports = {
    openBrowser, killBrowser
}
