import { app, BrowserWindow, Menu } from 'electron';
import * as path from 'path';
import './api';

const MAIN_HTML = 'index.html';
const PRELOAD_JS = 'preload.js';
const WIDTH = 600;
const HEIGHT = 350;

const getPath = (p: string) => path.resolve(__dirname, p);
const isDevelopment = process.env.NODE_ENV === 'development';
Menu.setApplicationMenu(null);
function createWindow(): BrowserWindow {
  const mainWindow = new BrowserWindow({
    width: WIDTH,
    height: HEIGHT,
    webPreferences: {
      devTools: isDevelopment,
      preload: getPath(PRELOAD_JS),
      defaultEncoding: 'utf-8',
    },
  });
  mainWindow.loadFile(getPath(MAIN_HTML));
  if (isDevelopment) {
    mainWindow.webContents.openDevTools();
  }
  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();
});
