const { app, BrowserWindow, Tray, ipcMain } = require('electron');

let python;
let mainWindow = null;

app.on('ready', () => {
  mainWindow = new BrowserWindow({
    fullscreen: true,
    webPreferences: {
      nodeIntegration : true , 
      contextIsolation : false, 
      devTools: false,
    },
    autoHideMenuBar: true,
    icon: __dirname+'/resources/assets/icons/linux.png'
  });
  mainWindow.loadFile(__dirname + '/views/cargando.html');
});

ipcMain.on('aforo:enviar', (e, datosAforoIniciales) => {
  sendToPython();
});

ipcMain.on('cerrar:hilo', (e) => {
  cerrarHilo();
});

function sendToPython() {
  python = require('child_process').spawn('python', ['./resources/python/index.py']);
  python.stdout.on('data', function (data) {
    mainWindow.webContents.send('aforo:actualizado', data.toString('utf8'));
  });

  python.stderr.on('data', (data) => {
    console.error(`stderr: ${data}`);
  }); 
}

function cerrarHilo() {
  console.log('Hilo cerrado');
  python.kill();
  python.on('close', (code) => {
    console.log(`child process exited with code ${code}`);
  });
}