 /**
 * Created by Gifer on 31.08.2016.
 */

 "use strict";

 var TRAY       = {};
 var appTray    = null;
 var trayMenu   = null;
 var electron   = null;
 var mainWindow = null;
 var images     = {
     mychat: null,
     empty : null,
     post  : null
 };
 // var blinkInterval = null;
 var breakBlink = false;
 var nativeImage= null;
 var blinking   = false;

 const BLINKTIMEOUT = 500;
 const os           = require('os');

 TRAY.init = function (_electron, _mainWindow) {
     electron   = _electron;
     mainWindow = _mainWindow;

     nativeImage = electron.nativeImage;

     images.mychat = nativeImage.createFromPath(`${MCPathes.Images}small_logo.png`);
     
     if (os.platform() === "linux") {
         images.empty = nativeImage.createFromPath(`${MCPathes.Images}empty32.png`);
     } else {
         images.empty  = nativeImage.createEmpty();
     }

     images.post   = nativeImage.createFromPath(`${MCPathes.Images}post32.png`);

     appTray  = new electron.Tray(images.mychat);

     appTray.setContextMenu(null);
     appTray.setIgnoreDoubleClickEvents(true);

     trayMenu = electron.Menu;

     appTray.setPressedImage(images.mychat);

     appTray.setToolTip('MyChat Client');
 };

 TRAY.trayMenu = function (menuItems) {
     let contextMenu = trayMenu.buildFromTemplate(menuItems);

     appTray.setContextMenu(contextMenu);
 };

 TRAY.setEvent = function (event, action) {
     appTray.on(event, action);
 };

 TRAY.removeEvent = function (event) {
     //todo: make it
 };

 TRAY.clearBlink = function () {
     // if (os.platform() === "linux") {
     //     blinking   = false;
     //     breakBlink = false;
     //
     //     appTray && appTray.setImage(images.mychat);
     // } else {
         breakBlink = true;
     // }
 };
                                      
 TRAY.isBlinking = function () {
     return blinking;
 };

 TRAY.blinkPost = function () {
     // console.log("breakBlink: " + breakBlink);

     if (!breakBlink) {
         blinking = true;

         // if (os.platform() === "linux") {
         //     appTray && appTray.setImage(images.post);
         // } else {
             appTray && appTray.setImage(images.empty);

             setTimeout(function () {
                 appTray && appTray.setImage(images.post);

                 if (breakBlink) TRAY.blinkPost(); else
                     setTimeout(function () {
                         appTray && appTray.setImage(images.empty);

                         if (breakBlink) TRAY.blinkPost(); else
                             setTimeout(function () {
                                 appTray && appTray.setImage(images.post);

                                 if (breakBlink) TRAY.blinkPost(); else
                                     setTimeout(function () {
                                         appTray && appTray.setImage(images.empty);

                                         if (breakBlink) TRAY.blinkPost(); else
                                             setTimeout(function () {
                                                 appTray && appTray.setImage(images.mychat);

                                                 setTimeout(TRAY.blinkPost, BLINKTIMEOUT);
                                             }, BLINKTIMEOUT);
                                     }, BLINKTIMEOUT);
                             }, BLINKTIMEOUT);
                     }, BLINKTIMEOUT);
             }, BLINKTIMEOUT);
         // }
     } else {
         breakBlink = false;
         blinking   = false;

         appTray.setImage(images.mychat);
     }
 };

 TRAY.showIcon = function (icon) {
     if (images.hasOwnProperty(icon)){
         appTray.setImage(images[icon]);
     } else {
         appTray.setImage(images.mychat);
     }
 };

 TRAY.hide = function () {
     if (appTray){
         appTray.destroy();
         appTray = null;
     }
 };

 TRAY.hasTray = function () {
     return !!appTray;
 };


 module.exports = TRAY;