var { app, BrowserWindow } = require('electron');
var path = require('path');
var url = require('url');

var BROWSER_CONFIG = {
  webPreferences: {
    devTools: true,
    webSecurity: false,
    allowRunningInsecureContent: true,
  },
};

if (app.isPackaged) {
  BROWSER_CONFIG.webPreferences.devTools = false;
  BROWSER_CONFIG.webPreferences.allowRunningInsecureContent = false;
}

var win;

var createWindow = function () {
  win = new BrowserWindow(BROWSER_CONFIG);
  if (app.isPackaged) {
    win.setMenu(null);
  }

  var ses = win.webContents.session;
  ses.clearCache(() => {
    console.log('Cache cleared!');
  });

  win.loadURL(
    url.format({
      pathname: path.join(__dirname, 'app', 'index.html'),
      protocol: 'file:',
      slashes: true,
    }),
    { extraHeaders: 'pragma: no-cache\n' },
  );

  win.maximize();

  win.on('closed', function () {
    win = null;
  });
}

app.on('ready', createWindow);

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

app.on('activate', function () {
  if (win === null) {
    createWindow();
  }
});
