import { remote, shell } from 'electron';
import { autorun, computed, observable, toJS } from 'mobx';

import { isDevMode, isMac } from '../environment';

const { app, Menu } = remote;

const template = [
  {
    label: 'Edit',
    submenu: [
      {
        role: 'undo',
      },
      {
        role: 'redo',
      },
      {
        type: 'separator',
      },
      {
        role: 'cut',
      },
      {
        role: 'copy',
      },
      {
        role: 'paste',
      },
      {
        role: 'pasteandmatchstyle',
      },
      {
        role: 'delete',
      },
      {
        role: 'selectall',
      },
    ],
  },
  {
    label: 'View',
    submenu: [
      {
        type: 'separator',
      },
      {
        role: 'resetzoom',
      },
      {
        role: 'zoomin',
        accelerator: 'CommandOrControl+=',
      },
      {
        role: 'zoomout',
      },
      {
        type: 'separator',
      },
      {
        role: 'togglefullscreen',
      },
    ],
  },
  {
    label: 'Services',
    submenu: [],
  },
  {
    role: 'window',
    submenu: [
      {
        role: 'minimize',
      },
      {
        role: 'close',
      },
    ],
  },
  {
    role: 'help',
    submenu: [
      {
        label: 'Learn More',
        click() { shell.openExternal('http://meetfranz.com'); },
      },
    ],
  },
];

export default class FranzMenu {
  @observable tpl = template;

  constructor(stores, actions) {
    this.stores = stores;
    this.actions = actions;

    autorun(this._build.bind(this));
  }

  _build() {
    const tpl = toJS(this.tpl);

    if (isDevMode) {
      tpl[1].submenu.push({
        role: 'toggledevtools',
      }, {
        label: 'Toggle Service Developer Tools',
        accelerator: 'CmdOrCtrl+Shift+Alt+i',
        click: () => {
          this.actions.service.openDevToolsForActiveService();
        },
      });
    }

    tpl[1].submenu.unshift({
      label: 'Reload Service',
      id: 'reloadService',
      accelerator: 'CmdOrCtrl+R',
      click: () => {
        if (this.stores.user.isLoggedIn
          && this.stores.services.enabled.length > 0) {
          this.actions.service.reloadActive();
        } else {
          window.location.reload();
        }
      },
    }, {
      label: 'Reload Franz',
      accelerator: 'CmdOrCtrl+Shift+R',
      click: () => {
        window.location.reload();
      },
    });

    if (isMac) {
      tpl.unshift({
        label: app.getName(),
        submenu: [
          {
            role: 'about',
          },
          {
            type: 'separator',
          },
          {
            label: 'Settings',
            accelerator: 'CmdOrCtrl+,',
            click: () => {
              this.actions.ui.openSettings({ path: '' });
            },
          },
          {
            type: 'separator',
          },
          {
            role: 'services',
            submenu: [],
          },
          {
            type: 'separator',
          },
          {
            role: 'hide',
          },
          {
            role: 'hideothers',
          },
          {
            role: 'unhide',
          },
          {
            type: 'separator',
          },
          {
            role: 'quit',
          },
        ],
      });
      // Edit menu.
      tpl[1].submenu.push(
        {
          type: 'separator',
        },
        {
          label: 'Speech',
          submenu: [
            {
              role: 'startspeaking',
            },
            {
              role: 'stopspeaking',
            },
          ],
        },
      );
      // Window menu.
      tpl[3].submenu = [
        {
          // label: 'Close',
          accelerator: 'CmdOrCtrl+W',
          role: 'close',
        },
        {
          // label: 'Minimize',
          accelerator: 'CmdOrCtrl+M',
          role: 'minimize',
        },
        {
          // label: 'Zoom',
          role: 'zoom',
        },
        {
          type: 'separator',
        },
        {
          // label: 'Bring All to Front',
          role: 'front',
        },
      ];
    }

    const serviceTpl = this.serviceTpl;

    serviceTpl.unshift({
      label: 'Add new Service',
      accelerator: 'CmdOrCtrl+N',
      click: () => {
        this.actions.ui.openSettings({ path: 'recipes' });
      },
    }, {
      type: 'separator',
    });

    if (serviceTpl.length > 0) {
      tpl[isMac ? 3 : 2].submenu = toJS(this.serviceTpl);
    }

    const menu = Menu.buildFromTemplate(tpl);
    Menu.setApplicationMenu(menu);
  }

  @computed get serviceTpl() {
    const services = this.stores.services.enabled;

    if (this.stores.user.isLoggedIn) {
      return services.map((service, i) => ({
        label: service.name,
        accelerator: i <= 9 ? `CmdOrCtrl+${i + 1}` : null,
        type: 'radio',
        checked: service.isActive,
        click: () => {
          this.actions.service.setActive({ serviceId: service.id });
        },
      }));
    }

    return [];
  }
}

@@ -108,19 +108,8 @@
 rt %7B
- isDevMode,
  isM
@@ -1564,31 +1564,8 @@
 );%0A%0A
-    if (isDevMode) %7B%0A  
     
@@ -1584,34 +1584,32 @@
 nu.push(%7B%0A      
-  
 role: 'toggledev
@@ -1620,25 +1620,21 @@
 s',%0A    
-  
 %7D, %7B%0A
-  
       la
@@ -1670,34 +1670,32 @@
 r Tools',%0A      
-  
 accelerator: 'Cm
@@ -1714,34 +1714,32 @@
 t+Alt+i',%0A      
-  
 click: () =%3E %7B%0A 
@@ -1737,34 +1737,32 @@
 () =%3E %7B%0A        
- 
- 
 this.actions.ser
@@ -1808,28 +1808,18 @@
     
-  
 %7D,%0A    
- 
- 
 %7D);
-%0A    %7D
 %0A%0A  

Add menu item to toggle (service) dev tools