"use strict";

const electron      = require('electron');
const app           = electron.app;
const Menu          = electron.Menu;
const BrowserWindow = electron.BrowserWindow;
const path          = require("path");

const iolabWindow = require('./iolabWindow');
const repoWindow = require('./repoWindow');

const appName = 'IOLab';

var template = [
    {
        label: appName,
        submenu: [
            {
                label: 'About ' + appName,
                click: function () {
                    var cmd = 'IOLabEventHandler.sendEvent({ name: "openAboutModal"})';
                    iolabWindow.executeJavaScript(cmd);
                }
            },
            {
                type: 'separator'
            },
            {
                label: 'Hide ' + appName,
                accelerator: 'Command+H',
                selector: 'hide:'
            },
            {
                label: 'Hide Others',
                accelerator: 'Command+Shift+H',
                selector: 'hideOtherApplications:'
            },
            {
                label: 'Show All',
                selector: 'unhideAllApplications:'
            },
            {
                type: 'separator'
            },
            {
                label: 'Quit',
                accelerator: 'Command+Q',
                click: function () {
                    app.quit();
                }
            }
        ]
    },
    {
        label: 'Edit',
        submenu: [
            {
                label: 'Cut',
                accelerator: 'Command+X',
                selector: 'cut:'
            },
            {
                label: 'Copy',
                accelerator: 'Command+C',
                selector: 'copy:'
            },
            {
                label: 'Paste',
                accelerator: 'Command+V',
                selector: 'paste:'
            },
            {
                label: 'Select All',
                accelerator: 'Command+A',
                selector: 'selectAll:'
            }
        ]
    },
    {
        label: 'View',
        submenu: [
            {
                label: 'Print',
                accelerator: 'Command+P',
                click: function () {
                    BrowserWindow.getFocusedWindow().webContents.print();
                }
            },
            {
                type: 'separator'
            },
            {
                label: 'Clear cache',
                accelerator: 'Alt+Command+C',
                click: function () {
                    iolabWindow.clearCache();
                    repoWindow.clearCache();
                }
            },
            {
                label: 'Reload',
                accelerator: 'Command+R',
                click: function () {
                    BrowserWindow.getFocusedWindow().reload();
                }
            },
            {
                label: 'Toggle DevTools',
                accelerator: 'Alt+Command+I',
                click: function () {
                    BrowserWindow.getFocusedWindow().toggleDevTools();
                }
            }
        ]
    },
    {
        label: 'Window',
        submenu: [
            {
                label: 'Minimize',
                accelerator: 'Command+M',
                selector: 'performMiniaturize:'
            },
            {
                label: 'Close',
                accelerator: 'Command+W',
                selector: 'performClose:'
            },
            {
                type: 'separator'
            },
            {
                label: 'Bring All to Front',
                selector: 'arrangeInFront:'
            }
        ]
    }
];

module.exports = function applyTemplate() {
    Menu.setApplicationMenu(Menu.buildFromTemplate(template));
};
