const {app, BrowserWindow, ipcMain, Tray, Menu, MenuItem, nativeImage} = require('electron')

// Menu.setApplicationMenu(Menu.buildFromTemplate(template));

const path = require('path')

// const assetsDir = path.join(__dirname, 'assets')

let tray        = undefined
let appWindow   = undefined

const menu = new Menu()

var template = [{
    label: "Application",
    submenu: [
        { label: "About Application", selector: "orderFrontStandardAboutPanel:" },
        { type: "separator" },
        { label: "Quit", accelerator: "Command+Q", click: function() { app.quit(); }}
    ]}, {
    label: "Edit",
    submenu: [
        { label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
        { label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
        { type: "separator" },
        { label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
        { label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
        { label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
        { label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
    ]}
];


// This method is called once Electron is ready to run our code
// It is effectively the main method of our Electron app
app.on('ready', () => {
  
  // Make the file menu
  Menu.setApplicationMenu(Menu.buildFromTemplate(template));

  // Setup the menubar with an icon
  let icon = __dirname + '/iconTemplate.png'
  tray = new Tray(icon)

  // Make the popup window for the menubar
  appWindow = new BrowserWindow({
    icon: icon,
    width: 340,
    minWidth: 340,
    maxWidth: 600,
    height: 380,
    minHeight: 380,
    maxHeight: 600,
    show: false,
    frame: false,
    resizable: true,
  })

  // Tell the popup window to load our index.html file
  appWindow.loadURL(`file://${path.join(__dirname, 'index.html')}`)

  // Only close the window on blur if dev tools isn't opened
  appWindow.on('blur', () => {
    if(!appWindow.webContents.isDevToolsOpened()) {
      appWindow.hide()
    }
  })

  appWindow.setMenu(menu)

  // Add a click handler so that when the user clicks on the menubar icon, it shows
  // our popup window
  tray.on('click', function(event) {
    toggleWindow()

    // Show devtools when command clicked
    if (appWindow.isVisible() && process.defaultApp && event.metaKey) {
      appWindow.openDevTools({mode: 'detach'})
    }

  })

  // Hide Mac dock icon
  app.dock.hide();

  showWindow();
})

const toggleWindow = () => {
  if (appWindow.isVisible()) {
    appWindow.hide()
  } else {
    showWindow()
  }
}

const showWindow = () => {
  const trayPos = tray.getBounds()
  const windowPos = appWindow.getBounds()
  let x, y = 0
  if (process.platform == 'darwin') {
    x = Math.round(trayPos.x + (trayPos.width / 2) - (windowPos.width / 2))
    y = Math.round(trayPos.y + trayPos.height)
  } else {
    x = Math.round(trayPos.x + (trayPos.width / 2) - (windowPos.width / 2))
    y = Math.round(trayPos.y + trayPos.height * 10)
  }


  appWindow.setPosition(x, y, false)
  appWindow.show()
  appWindow.focus()
}

ipcMain.on('show-window', () => {
  showWindow()
})

app.on('window-all-closed', () => {
  // On macOS 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()
  }
})
