const electron = require('electron')
// Module to control application life.

// Module to create native browser window.
const { app, BrowserWindow, Tray, Menu, globalShortcut, ipcMain } = electron;

const path = require('path')
const url = require('url')
const fs = require('fs');

const logFile = path.join((electron.app || electron.remote.app).getPath('userData'), 'logs.jsonl');

fs.openSync(logFile, 'a');


const isDev = process.env.NODE_ENV === 'dev';

const shortcuts = require('./shortcuts');

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

ipcMain.on('new-entry', (event, arg) => {
  fs.appendFile(logFile, JSON.stringify(arg) + '\n', (err) => {
    if (err) throw err;
    console.log('Saved!');
  });
});

ipcMain.on('get-entries', (event, arg) => {
  console.log('getting entries')
  fs.readFile(logFile, 'utf-8', (err, data) => {
    if (err) throw err;
    const jsonArray = data.split('\n');
    console.log({ jsonArray })
    const a = jsonArray.filter(jsonString => !!jsonString).map(jsonString => JSON.parse(jsonString));
    event.sender.send('get-entries-resp', a);
  });
})

function createWindow() {
  // Create the browser window.

  const tray = new Tray(path.join(__dirname, '../img/if_stop-watch-time-count_2203547.png'))
  const contextMenu = Menu.buildFromTemplate([
    { label: 'Quit', type: 'normal', click: () => app.quit() },

  ])

  tray.setToolTip('This is my application.')


  tray.on('right-click', () => {
    console.log('right-click')
    tray.popUpContextMenu(contextMenu)
  })

  const createBrowserWindow = () => {
    const trayBounds = tray.getBounds();
    console.log({ trayBounds })

    mainWindow = new BrowserWindow({
      width: isDev ? 1330 : 330,
      height: isDev ? 800 : 400,
      title: 'Tijd',
      resizable: !isDev,
      show: true,
      fullscreenable: false,

      maximizable: false,
      minimizable: false,
      transparent: true,
      frame: false,
      movable: isDev,
      y: 25,
      x: trayBounds.x - 160,
      webPreferences: {
        backgroundThrottling: false,
        devTools: isDev
      }
    })


    const prod = url.format({
      pathname: path.join(__dirname, '..', 'renderer', 'build', 'index.html'),
      protocol: 'file:',
      slashes: true
    });
    const dev = 'http://localhost:3000';
    console.log(process.env.NODE_ENV === 'dev' ? dev : prod)
    mainWindow.loadURL(process.env.NODE_ENV === 'dev' ? dev : prod);
    mainWindow.on('blur', () => mainWindow.hide())

  };


  const openWindow = () => {
    console.log('click')
    if (!mainWindow) {
      createBrowserWindow();
    }

    mainWindow.show();
  };
  createBrowserWindow();


  tray.on('click', openWindow)
  const showWindow = () => {
    console.log('CmdOrCtrl+Shift+U is pressed')
    if (!mainWindow || !mainWindow.isFocused()) {
      openWindow();
    } else {
      mainWindow.hide()
    }
  }

  if (!shortcuts.onOpen(showWindow)) {
    console.error('registering shortcut failed')
  }


}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

app.on('will-quit', () => {

  // Unregister all shortcuts.
  globalShortcut.unregisterAll()
})

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

@@ -1214,16 +1214,82 @@
 %7D);%0A%7D)%0A%0A
+ipcMain.on('hide-app', (event, arg) =%3E %7B%0A  mainWindow.hide()%0A%7D);%0A%0A
 function

add hide-app listener