const URL_WEBSITE = 'https://dogancelik.com/atomic-tweetdeck/';
const URL_ISSUES = 'https://github.com/dogancelik/atomic-tweetdeck/issues';

const appName = global.electron.app.getName();
const appVersion = global.electron.app.getVersion();

const shell = global.electron.shell;
const dialog = global.electron.dialog;

const { getAboutMessage, updateState } = require('./utils');

exports.getMenuTemplate = function () {
  return [{
    label: '&Window',
    role: 'window',
    submenu: [
      {
        visible: false,
        label: 'Toggle &DevTools',
        accelerator: 'F12',
        click(item, wnd) { wnd.webContents.toggleDevTools(); }
      },
      {
        label: '&Reload',
        accelerator: 'CmdOrCtrl+R',
        click(item, wnd) { wnd.reload(); }
      },
      {
        label: '&Hide',
        accelerator: 'CmdOrCtrl+W',
        click(item, wnd) {
          if (wnd.getParentWindow() == null) {
            wnd.minimize();
          } else {
            wnd.close();
          }
        }
      },
      {
        label: '&Quit',
        accelerator: 'CmdOrCtrl+Q',
        click() {
          // app.exit() crashes Electron on child window
          // can't do app.quit() if Close to Tray
          // this will work because windowAllClosed
          updateState();
          Array.prototype.slice.call(global.electron.BrowserWindow.getAllWindows())
            .forEach((wnd) => wnd.destroy());
        }
      }
    ]
  }, {
    label: 'Hi&story',
    submenu: [
      {
        label: 'Go back',
        accelerator: 'Alt+Left',
        click(item, wnd) { wnd.webContents.canGoBack() && wnd.webContents.goBack(); }
      },
      {
        label: 'Go forward',
        accelerator: 'Alt+Right', // Insert smug Pepe here
        click(item, wnd) { wnd.webContents.canGoForward() && wnd.webContents.goForward(); }
      }
    ]
  }, {
    label: 'Settings',
    submenu: [
      {
        label: 'Open new windows in default browser',
        type: 'checkbox',
        click() { config.store.set(config.names.openBrowser, !config.store.get(config.names.openBrowser)); }
      },
      {
        label: 'Minimize to Tray',
        type: 'checkbox',
        click() { config.store.set(config.names.minToTray, !config.store.get(config.names.minToTray)); }
      },
      {
        label: 'Hide menu',
        type: 'checkbox',
        click(item, wnd) {
          let newValue = !config.store.get(config.names.hideMenu);
          wnd.setAutoHideMenuBar(newValue);
          wnd.setMenuBarVisibility(!newValue);
          config.store.set(config.names.hideMenu, newValue);
        }
      }
    ]
  }, {
    label: '&Help',
    submenu: [
      {
        label: '&Visit website',
        click() { shell.openExternal(URL_WEBSITE); }
      },
      {
        label: '&Report an issue',
        click() { shell.openExternal(URL_ISSUES); }
      },
      {
        label: '&About ' + appName,
        click() {
          dialog.showMessageBox(global.mainWindow, {
            buttons: ['OK'],
            title: 'About ' + appName,
            message: getAboutMessage(appName, appVersion)
          })
        }
      }
    ]
  }];
};

exports.createMenu = function (template) {
  const Menu = global.electron.Menu;
  var menu = Menu.buildFromTemplate(template);
  Menu.setApplicationMenu(menu);
  return menu;
};

exports.setMenuChecks = function (appMenu) {
  appMenu.items[2].submenu.items[0].checked = config.store.get(config.names.openBrowser);
  appMenu.items[2].submenu.items[1].checked = config.store.get(config.names.minToTray);
  appMenu.items[2].submenu.items[2].checked = config.store.get(config.names.hideMenu);
};

@@ -1464,11 +1464,586 @@
 l: '
+&View',%0A    role: 'view',%0A    submenu: %5B%0A      %7B%0A        label: 'Zoom &In',%0A        accelerator: 'CmdOrCtrl+NumAdd',%0A        click(item, wnd) %7B wnd.webContents.setZoomLevel(wnd.webContents.getZoomLevel() + 1); %7D%0A      %7D,%0A      %7B%0A        label: 'Zoom &Out',%0A        accelerator: 'CmdOrCtrl+NumSub',%0A        click(item, wnd) %7B wnd.webContents.setZoomLevel(wnd.webContents.getZoomLevel() - 1); %7D%0A      %7D,%0A      %7B%0A        label: '&Reset Zoom',%0A        accelerator: 'CmdOrCtrl+Num0',%0A        click(item, wnd) %7B wnd.webContents.setZoomLevel(0); %7D%0A      %7D,%0A    %5D%0A  %7D, %7B%0A    label: '&
 Hi
-&
 stor
@@ -2088,17 +2088,18 @@
 el: 'Go 
-b
+&B
 ack',%0A  
@@ -2251,17 +2251,18 @@
 el: 'Go 
-f
+&F
 orward',
@@ -2446,16 +2446,17 @@
 label: '
+&
 Settings
@@ -2473,32 +2473,70 @@
 menu: %5B%0A      %7B%0A
+        id: config.names.openBrowser,%0A
         label: '
@@ -2717,32 +2717,68 @@
      %7D,%0A      %7B%0A
+        id: config.names.minToTray,%0A
         label: '
@@ -2936,32 +2936,67 @@
      %7D,%0A      %7B%0A
+        id: config.names.hideMenu,%0A
         label: '
@@ -3326,12 +3326,12 @@
 l: '
-&
 Hel
+&
 p',%0A
@@ -3376,17 +3376,17 @@
 '&Visit 
-w
+W
 ebsite',
@@ -3484,12 +3484,9 @@
 ort 
-an i
+I
 ssue
@@ -4075,33 +4075,49 @@
 enu.
-items%5B2%5D.submenu.items%5B0%5D
+getMenuItemById(config.names.openBrowser)
 .che
@@ -4181,33 +4181,47 @@
 enu.
-items%5B2%5D.submenu.items%5B1%5D
+getMenuItemById(config.names.minToTray)
 .che
@@ -4283,33 +4283,46 @@
 enu.
-items%5B2%5D.submenu.items%5B2%5D
+getMenuItemById(config.names.hideMenu)
 .che

Add zoom