import path from 'path';
import NylasStore from 'nylas-store';
import {remote, ipcRenderer} from 'electron';
import {UnreadBadgeStore, CanvasUtils} from 'nylas-exports';
const NativeImage = remote.require('native-image');
const Menu = remote.require('menu');
const {canvasWithSystemTrayIconAndText} = CanvasUtils;

// Must be absolute real system path
// https://github.com/atom/electron/issues/1299
const BASE_ICON_PATH = path.join(__dirname, '..', 'assets', process.platform, 'ic-systemtray-nylas.png');
const UNREAD_ICON_PATH = path.join(__dirname, '..', 'assets', process.platform, 'ic-systemtray-nylas-unread.png');

const _menuTemplate = [
  {
    label: 'New Message',
    click: ()=> ipcRenderer.send('command', 'application:new-message'),
  },
  {
    label: 'Preferences',
    click: ()=> ipcRenderer.send('command', 'application:open-preferences'),
  },
  {
    type: 'separator',
  },
  {
    label: 'Quit N1',
    click: ()=> ipcRenderer.send('command', 'application:quit'),
  },
];

if (process.platform !== 'win32') {
  _menuTemplate.unshift({
    label: 'Open Inbox',
    click: ()=> ipcRenderer.send('command', 'application:show-main-window'),
  });
}

const _buildMenu = ()=> {
  return Menu.buildFromTemplate(_menuTemplate);
};

class TrayStore extends NylasStore {

  constructor(platform) {
    super();
    this._platform = platform;

    this._unreadIcon = NativeImage.createFromPath(UNREAD_ICON_PATH);
    this._baseIcon = NativeImage.createFromPath(BASE_ICON_PATH);
    this._unreadCount = UnreadBadgeStore.count() || 0;
    this._menu = _buildMenu(platform);
    this._icon = this._getIconImg();
    this.listenTo(UnreadBadgeStore, this._onUnreadCountChanged);
  }

  unreadCount() {
    return this._unreadCount;
  }

  icon() {
    return this._icon;
  }

  tooltip() {
    return `${this._unreadCount} unread messages`;
  }

  menu() {
    return this._menu;
  }

  _getIconImg(platform = this._platform, unreadCount = this._unreadCount) {
    const imgHandlers = {
      'darwin': ()=> {
        const img = new Image();
        // toDataUrl always returns the @1x image data, so the assets/darwin/
        // contains an "@2x" image /without/ the @2x extension
        img.src = this._baseIcon.toDataURL();
        const count = this._unreadCount || '';
        const canvas = canvasWithSystemTrayIconAndText(img, count.toString());
        const pngData = NativeImage.createFromDataURL(canvas.toDataURL()).toPng();

        // creating from a buffer allows us to specify that the image is @2x
        const out2x = NativeImage.createFromBuffer(pngData, 2);
        out2x.setTemplateImage(true);
        return out2x;
      },
      'default': ()=> {
        return unreadCount > 0 ? this._unreadIcon : this._baseIcon;
      },
    };

    return imgHandlers[platform in imgHandlers ? platform : 'default']();
  }

  _onUnreadCountChanged() {
    this._unreadCount = UnreadBadgeStore.count();
    this._icon = this._getIconImg();
    this.trigger();
  }
}

export default TrayStore;

@@ -1488,63 +1488,8 @@
 H);%0A
-    this._unreadCount = UnreadBadgeStore.count() %7C%7C 0;%0A
     
@@ -1512,24 +1512,16 @@
 ildMenu(
-platform
 );%0A    t
@@ -1544,32 +1544,33 @@
 ._getIconImg();%0A
+%0A
     this.listenT
@@ -1566,105 +1566,124 @@
 his.
-listenTo(UnreadBadgeStore, this._onUnreadCountChanged);%0A  %7D%0A%0A  unreadCount() %7B%0A    return
+_unreadString = new Number(UnreadBadgeStore.count()).toLocaleString();%0A    this.listenTo(UnreadBadgeStore,
  this._
-u
+onU
 nrea
@@ -1688,16 +1688,24 @@
 eadCount
+Changed)
 ;%0A  %7D%0A%0A 
@@ -1782,21 +1782,22 @@
 ._unread
-Count
+String
 %7D unread
@@ -1870,66 +1870,8 @@
 Img(
-platform = this._platform, unreadCount = this._unreadCount
 ) %7B%0A
@@ -2143,55 +2143,8 @@
 ();%0A
-        const count = this._unreadCount %7C%7C '';%0A
     
@@ -2203,24 +2203,26 @@
 mg, 
-count.to
+this._unread
 String
-()
 );%0A 
@@ -2557,23 +2557,33 @@
 urn 
-unreadCount %3E 0
+this._unreadString != '0'
  ? t
@@ -2656,16 +2656,22 @@
 andlers%5B
+this._
 platform
@@ -2688,16 +2688,22 @@
 dlers ? 
+this._
 platform
@@ -2772,16 +2772,28 @@
 read
-Count = 
+String = new Number(
 Unre
@@ -2812,16 +2812,34 @@
 .count()
+).toLocaleString()
 ;%0A    th

Use locale string so unread count uses comma notation