//
// MainProcess
'use strict';

const ACTIVE_MENUBAR_ICON   = __dirname + '/images/active.png'
const INACTIVE_MENUBAR_ICON = __dirname + '/images/inactive.png'
const NOTIFY_ICON           = __dirname + '/images/notify_icon.png'
import menubar from 'menubar';
import { app, ipcMain, globalShortcut, Menu } from 'electron';
import notifier from 'node-notifier';
import path from 'path'

const request = require('request');
const mb = menubar({ icon: ACTIVE_MENUBAR_ICON  });
mb.setOption('width', 500)

const switchIconUnread = ()=> {
  mb.tray.setImage(ACTIVE_MENUBAR_ICON )
}
const switchIconRead = ()=> {
  mb.tray.setImage(ACTIVE_MENUBAR_ICON )
}
const setTrayTitle = (title)=> {
  mb.tray.setTitle(title)
}
const initMenu = ()=> {
  var template = [{
      label: "Application",
      submenu: [
          { label: "About Application", selector: "orderFrontStandardAboutPanel:" },
          { type: "separator" },
          { label: "Quit",       accelerator: "Command+Q", click: function() { app.quit(); }}
      ]}, {
      label: "Edit",
      submenu: [
          { label: "Undo",       accelerator: "CmdOrCtrl+Z",       selector: "undo:" },
          { label: "Redo",       accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
          { type:  "separator" },
          { label: "Cut",        accelerator: "CmdOrCtrl+X",       selector: "cut:" },
          { label: "Copy",       accelerator: "CmdOrCtrl+C",       selector: "copy:" },
          { label: "Paste",      accelerator: "CmdOrCtrl+V",       selector: "paste:" },
          { label: "Select All", accelerator: "CmdOrCtrl+A",       selector: "selectAll:" }
      ]}
  ];

  Menu.setApplicationMenu(Menu.buildFromTemplate(template));
}

mb.on('ready', function ready () {

  var closeWindow = ()=> {
      mb.hideWindow();
  }
  var openWindow = ()=> {
    if(!trayBounds){
      mb.showWindow();
    } else {
      mb.showWindow(trayBounds);
    }
  }

  ipcMain.on('renderer_init', function(event, arg) {

    var sender = event.sender

    //
    // Toggle window show and hide
    globalShortcut.register('ctrl+shift+p', ()=> {
      if(mb.window.isVisible()) {
        closeWindow();
      } else {
        openWindow();
      }
    });

    //
    // Start pomodoro recently track
    globalShortcut.register('ctrl+shift+m', ()=> {
      sender.send('pomo_start')
    });
  });

  //
  // Setting ipc event
  ipcMain.on('notify', (event, title, message)=> {
    notifier.notify({
      title: title,
      icon: NOTIFY_ICON,
      message: message
    })
  });
  ipcMain.on('show_window', (event, arg)=> {
    openWindow();
  });
  ipcMain.on('set_title', (event, text)=> {
    setTrayTitle(text.trim())
  });
  ipcMain.on('mark_unread', (event, arg)=> {
    switchIconUnread();
  });
  ipcMain.on('quit', (event, arg)=> {
    app.quit();
  });
  ipcMain.on('reload', (event, arg)=> {
    let browserWindow = event.sender;
    browserWindow.reload()
  });
  notifier.on('click', (event, arg)=> {
    mb.showWindow();
  });
  mb.on('show', ()=> {
    setTimeout(()=> {
      switchIconRead();
    }, 1000);
  })
  mb.on('hide', ()=> {
    switchIconRead();
  })
  // FIXME: 今のところtray iconの位置を取る方法がclickイベントしかない
  //       のでここでtrayBoundsを取得している
  var trayBounds = null;
  mb.tray.on('click', (_, bounds)=> {
    trayBounds = bounds
  })

  mb.showWindow();
  mb.hideWindow();
  initMenu();
  switchIconUnread();
})

@@ -1767,18 +1767,16 @@
 %3E %7B%0A    
-  
 mb.hideW
@@ -1822,99 +1822,43 @@
     
-if(!trayBounds)%7B%0A      mb.showWindow();%0A    %7D else %7B%0A      mb.showWindow(trayBounds);%0A    %7D
+mb.showWindow(mb.tray.getBounds());
 %0A  %7D
@@ -3075,183 +3075,8 @@
   %7D)
-%0A  // FIXME: %E4%BB%8A%E3%81%AE%E3%81%A8%E3%81%93%E3%82%8Dtray icon%E3%81%AE%E4%BD%8D%E7%BD%AE%E3%82%92%E5%8F%96%E3%82%8B%E6%96%B9%E6%B3%95%E3%81%8Cclick%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E3%81%97%E3%81%8B%E3%81%AA%E3%81%84%0A  //       %E3%81%AE%E3%81%A7%E3%81%93%E3%81%93%E3%81%A7trayBounds%E3%82%92%E5%8F%96%E5%BE%97%E3%81%97%E3%81%A6%E3%81%84%E3%82%8B%0A  var trayBounds = null;%0A  mb.tray.on('click', (_, bounds)=%3E %7B%0A    trayBounds = bounds%0A  %7D)
 %0A%0A  

Use tray.getBounda fro get tray icon position