/* jshint esnext: true */

"use strict";

var menubar = require('menubar');
var Docker  = require('dockerode');
var fs      = require('fs');
var docker  = new Docker();

var mb = menubar({
  dir: __dirname + "/src",
  preloadWindow: true,
  icon: __dirname + "/IconTemplate@2x.png"
});

var lastConnectionStatus = null;

function setIcon() {
  docker.ping((err, data) => {
    if (data == 'OK') {
      mb.window.webContents.send('ping', 'connected');
    }

    if (lastConnectionStatus === null && data == 'OK') {
      let NativeImage = require('native-image');
      let image = NativeImage.createFromPath(__dirname + "/IconTemplate@2x.png");
      mb.tray.setImage(image);
      lastConnectionStatus = 'OK';

      // console.log('connected');
    }

    if (lastConnectionStatus === 'OK' && data === null) {
      let NativeImage = require('native-image');
      let image = NativeImage.createFromPath(__dirname + "/IconOffTemplate@2x.png");
      mb.tray.setImage(image);
      lastConnectionStatus = null;
    }
  });
}


mb.once('show', function () {
  mb.window.openDevTools();
});

mb.on('ready', function ready () {
  // receive events from the page
  // var ipc = require('ipc');
  // ipc.on('asynchronous-message', function(event, arg) {
  //   console.log(arg);  // prints "ping"
  //   event.sender.send('asynchronous-reply', 'pong');
  // });

  // ipc.on('synchronous-message', function(event, arg) {
  //   console.log(arg);  // prints "ping"
  //   event.returnValue = 'pong';
  // });

  setInterval(setIcon, 5000);

});


mb.on('show', function () {
  //send events to the page
  // mb.window.webContents.send('ping', 'whoooooooh!');

});

//sdg

@@ -365,24 +365,27 @@
 ta) =%3E %7B%0A   
+ //
  if (data ==
@@ -392,24 +392,27 @@
  'OK') %7B%0A   
+ //
    mb.window
@@ -446,32 +446,35 @@
 connected');%0A   
+ //
  %7D%0A%0A    if (last
@@ -718,44 +718,8 @@
 OK';
-%0A%0A      // console.log('connected');
 %0A   
@@ -1621,16 +1621,111 @@
 oooh!');
+%0A  if (lastConnectionStatus == 'OK') %7B%0A    mb.window.webContents.send('ping', 'connected');%0A  %7D
 %0A%0A%7D);%0A%0A/

Stop update on poll