 remote = require('electron').remote
 Menu = remote.Menu
 
+platform = require('os').platform()
+# to reduce number of == comparisons
+isDarwin = platform == 'darwin'
+isNotDarwin = platform != 'darwin'
+
 acceleratorMap = {
-    hideyakyak:           {others: 'Control+H', darwin:'Command+H'}
-    hideothers:           {others: '', darwin:'Command+Shift+H'}
-    showall:              {others: '', darwin:''}
-    openinspector:        {others: 'Control+Alt+I', darwin:'Command+Alt+I'}
-    quit:                 {others: 'Control+Q', darwin:'Command+Q'}
-    undo:                 {others: 'Control+Z', darwin:'Command+Z'}
-    redo:                 {others: 'Control+Shift+Z', darwin:'Command+Shift+Z'}
-    cut:                  {others: '', darwin:'Command+X'}
-    copy:                 {others: '', darwin:'Command+C'}
-    paste:                {others: '', darwin:'Command+V'}
-    selectall:            {others: '', darwin:'Command+A'}
-    togglefullscreen:     {others: 'Control+Alt+F', darwin:'Command+Control+F'}
-    zoomin:               {others: 'Control+Plus', darwin:'Command+Plus'}
-    zoomout:              {others: 'Control+-', darwin:'Command+-'}
-    resetzoom:            {others: 'Control+0', darwin:'Command+0'}
-    previousconversation: {others: 'Control+K', darwin:'Command+Shift+Tab'}
-    nextconversation:     {others: 'Control+J', darwin:'Command+Tab'}
-    conversation1:        {others: 'Alt+1', darwin:'Command+1'}
-    conversation2:        {others: 'Alt+2', darwin:'Command+2'}
-    conversation3:        {others: 'Alt+3', darwin:'Command+3'}
-    conversation4:        {others: 'Alt+4', darwin:'Command+4'}
-    conversation5:        {others: 'Alt+5', darwin:'Command+5'}
-    conversation6:        {others: 'Alt+6', darwin:'Command+6'}
-    conversation7:        {others: 'Alt+7', darwin:'Command+7'}
-    conversation8:        {others: 'Alt+8', darwin:'Command+8'}
-    conversation9:        {others: 'Alt+9', darwin:'Command+9'}
-    minimize:             {others: '', darwin:'Command+M'}
-    close:                {others: '', darwin:'Command+W'}
+    hideyakyak: {default: 'Control+H', darwin:'Command+H'}
+    hideothers: {default: '', darwin:'Command+Shift+H'}
+    showall: {default: '', darwin:''}
+    openinspector: {default: 'Control+Alt+I', darwin:'Command+Alt+I'}
+    quit: {default: 'Control+Q', darwin:'Command+Q'}
+    undo: {default: 'Control+Z', darwin:'Command+Z'}
+    redo: {default: 'Control+Shift+Z', darwin:'Command+Shift+Z'}
+    cut: {default: '', darwin:'Command+X'}
+    copy: {default: '', darwin:'Command+C'}
+    paste: {default: '', darwin:'Command+V'}
+    selectall: {default: '', darwin:'Command+A'}
+    togglefullscreen: {default: 'Control+Alt+F', darwin:'Command+Control+F'}
+    zoomin: {default: 'Control+Plus', darwin:'Command+Plus'}
+    zoomout: {default: 'Control+-', darwin:'Command+-'}
+    resetzoom: {default: 'Control+0', darwin:'Command+0'}
+    previousconversation: {default: 'Control+K', darwin:'Command+Shift+Tab'}
+    nextconversation:  {default: 'Control+J', darwin:'Command+Tab'}
+    conversation1: {default: 'Alt+1', darwin:'Command+1'}
+    conversation2: {default: 'Alt+2', darwin:'Command+2'}
+    conversation3: {default: 'Alt+3', darwin:'Command+3'}
+    conversation4: {default: 'Alt+4', darwin:'Command+4'}
+    conversation5: {default: 'Alt+5', darwin:'Command+5'}
+    conversation6: {default: 'Alt+6', darwin:'Command+6'}
+    conversation7: {default: 'Alt+7', darwin:'Command+7'}
+    conversation8: {default: 'Alt+8', darwin:'Command+8'}
+    conversation9: {default: 'Alt+9', darwin:'Command+9'}
+    minimize: {default: '', darwin:'Command+M'}
+    close: {default: '', darwin:'Command+W'}
 }
 
-templateYakYak = (viewstate, platform) ->
-    tmpl = []
-    if platform == 'darwin'
-        tmpl.push.apply tmpl,  [
-            { label: 'About YakYak', selector: 'orderFrontStandardAboutPanel:' }
-            #{ type: 'separator' }
-            # { label: 'Preferences...', accelerator: 'Command+,',
-            # click: => delegate.openConfig() }
-            { type: 'separator' }
-        ]
-    tmpl.push {
-        label: 'Hide YakYak'
-        accelerator: acceleratorMap['hideyakyak'][platform]
-        selector: 'hide:' if platform == 'darwin'
-        role: 'minimize' if platform == 'others'
-    }
-
-    if platform == 'darwin'
-        tmpl.push.apply tmpl, [{
+getAccelerator = (key) ->
+    if (retVal = acceleratorMap[key][platform])?
+        retVal
+    else
+        acceleratorMap[key]['default']
+
+templateYakYak = (viewstate) ->
+
+    [
+        { label: 'About YakYak', selector: 'orderFrontStandardAboutPanel:' } if isDarwin
+        #{ type: 'separator' }
+        # { label: 'Preferences...', accelerator: 'Command+,',
+        # click: => delegate.openConfig() }
+        { type: 'separator' } if isDarwin
+        {
+            label: 'Hide YakYak'
+            accelerator: getAccelerator('hideyakyak')
+            selector: 'hide:' if isDarwin
+            role: 'minimize' if isNotDarwin
+        }
+        if isDarwin
+            {
                 label: 'Hide Others'
-                accelerator: acceleratorMap['hideother'][platform]
-                selector: 'hideOtherApplications:' if platform == 'darwin'
-            }
+                accelerator: getAccelerator('hideother')
+                selector: 'hideOtherApplications:' if isDarwin
+            }
+
             {
                 label: 'Show All'
-                selector: 'unhideAllApplications:' if platform == 'darwin'
-            }
-        ]
-    tmpl.push.apply tmpl, [
+                selector: 'unhideAllApplications:' if isDarwin
+            }
         { type: 'separator' }
         {
             label: 'Open Inspector'
-            accelerator: acceleratorMap['openinspector'][platform]
+            accelerator: getAccelerator('openinspector')
             click: -> action 'devtools'
         }
         { type: 'separator' }

            checked:  viewstate.showtray
            click: -> action 'toggleshowtray'
        }
    ]
    #
    if platform == 'darwin'
        tmpl.push {
            label: 'Hide Dock icon'
            type: 'checkbox'
            enabled: viewstate.showtray
            checked:  viewstate.hidedockicon
            click: -> action 'togglehidedockicon'
        }
    tmpl

templateWindow = (viewstate, platform) -> [
    {
        label: 'Minimize'
        accelerator: acceleratorMap['minimize'][platform]
        selector: 'performMiniaturize:' if platform == 'darwin'
    }, {
        label: 'Close'
        accelerator: acceleratorMap['close'][platform]
        selector: 'performClose:' if platform == 'darwin'
    }, {
        type: 'separator'
    }, {
        label: 'Bring All to Front',
        selector: 'arrangeInFront:' if platform == 'darwin'
    }
]

templateMenu = (viewstate) ->
    platform = if require('os').platform() == 'darwin' then 'darwin' else
        'others'

    tmpl = [{
            label: 'YakYak'
            submenu: templateYakYak viewstate, platform
        }, {
            label: 'Edit'
            submenu: templateEdit viewstate, platform
        },{
            label: 'View'
            submenu: templateView viewstate, platform
        }
    ]
    if platform == 'darwin'
        tmpl.push {
            label: 'Window'
            submenu: templateWindow viewstate, platform
        }
    tmpl

module.exports = (viewstate) ->
    Menu.setApplicationMenu Menu.buildFromTemplate templateMenu(viewstate)

            ]
        }, {
            label: 'Toggle Full Screen',
            accelerator: getAccelerator('togglefullscreen'),
            click: -> action 'togglefullscreen'
        }, {
            # seee https://github.com/atom/electron/issues/1507
            label: 'Zoom In',
            accelerator: getAccelerator('zoomin'),
            click: -> action 'zoom', +0.25
        }, {
            label: 'Zoom Out',
            accelerator: getAccelerator('zoomout'),
            click: -> action 'zoom', -0.25
        }, {
            label: 'Reset Zoom',
            accelerator: getAccelerator('resetzoom'),
            click: -> action 'zoom'
        }, {
            type: 'separator'
        }, {
            label: 'Previous Conversation',
            accelerator: getAccelerator('previousconversation')
            enabled: viewstate.loggedin
            click: -> action 'selectNextConv', -1
        }, {
            label: 'Next Conversation',
            accelerator: getAccelerator('nextconversation')
            enabled: viewstate.loggedin
            click: -> action 'selectNextConv', +1
        }, {
