import { app, BrowserWindow, Menu } from 'electron';
import * as path from 'path';
import './api';
import { deleteAllTmpDirectoriesSync } from './impl';
const MAIN_HTML = 'index.html';
const PRELOAD_JS = 'preload.js';
const WIDTH = 1200;
const HEIGHT = 800;

const getPath = (p: string) => path.resolve(__dirname, p);
const isDevelopment = process.env.NODE_ENV === 'development';

if (!isDevelopment) {
  Menu.setApplicationMenu(null);
}

function createWindow(): BrowserWindow {
  const mainWindow = new BrowserWindow({
    width: WIDTH,
    height: HEIGHT,
    webPreferences: {
      sandbox: true,
      devTools: isDevelopment,
      preload: getPath(PRELOAD_JS),
      defaultEncoding: 'utf-8',
    },
  });
  mainWindow.loadFile(getPath(MAIN_HTML));
  return mainWindow;
}

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

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

process.once('exit', deleteAllTmpDirectoriesSync);
