var app = require('app');
var delegate = require('atom_delegate');
var ipc = require('ipc');
var Menu = require('menu');
var MenuItem = require('menu_item');
var BrowserWindow = require('browser_window');

var mainWindow = null;
var menu = null;

// Quit when all windows are closed.
app.on('window-all-closed', function() {
  app.terminate();
});

delegate.browserMainParts.preMainMessageLoopRun = function() {
  app.commandLine.appendSwitch('js-flags', '--harmony_collections');

  mainWindow = new BrowserWindow({ width: 800, height: 600 });
  mainWindow.loadUrl('file://' + __dirname + '/index.html');

  mainWindow.on('page-title-updated', function(event, title) {
    event.preventDefault();

    this.setTitle('Atom Shell - ' + title);
  });

  mainWindow.on('closed', function() {
    console.log('closed');
    mainWindow = null;
  });

  var template = [
    {
      label: 'Atom Shell',
      submenu: [
        {
          label: 'About Atom Shell',
          selector: 'orderFrontStandardAboutPanel:'
        },
        {
          type: 'separator'
        },
        {
          label: 'Hide Atom Shell',
          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: 'View',
      submenu: [
        {
          label: 'Reload',
          accelerator: 'Command+R',
          click: function() { BrowserWindow.getFocusedWindow().reloadIgnoringCache(); }
        },
        {
          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:'
        },
      ]
    },
  ];

  menu = Menu.buildFromTemplate(template);
  Menu.setApplicationMenu(menu);

  ipc.on('message', function(processId, routingId, type) {
    if (type == 'menu')
      menu.popup(mainWindow);
  });
}

@@ -1611,32 +1611,817 @@
  %5D%0A    %7D,%0A    %7B%0A
+      label: 'Edit',%0A      submenu: %5B%0A        %7B%0A          label: 'Undo',%0A          accelerator: 'Command+Z',%0A          selector: 'undo:'%0A        %7D,%0A        %7B%0A          label: 'Redo',%0A          accelerator: 'Shift+Command+Z',%0A          selector: 'redo:'%0A        %7D,%0A        %7B%0A          type: 'separator'%0A        %7D,%0A        %7B%0A          label: 'Cut',%0A          accelerator: 'Command+X',%0A          selector: 'cut:'%0A        %7D,%0A        %7B%0A          label: 'Copy',%0A          accelerator: 'Command+C',%0A          selector: 'copy:'%0A        %7D,%0A        %7B%0A          label: 'Paste',%0A          accelerator: 'Command+V',%0A          selector: 'paste:'%0A        %7D,%0A        %7B%0A          label: 'Select All',%0A          accelerator: 'Command+A',%0A          selector: 'selectAll:'%0A        %7D,%0A      %5D%0A    %7D,%0A    %7B%0A
       label: 'Vi

Add Edit menu.