/**
 Copyright 2014 Gordon Williams (gw@pur3.co.uk)

 This Source Code is subject to the terms of the Mozilla Public
 License, v2.0. If a copy of the MPL was not distributed with this
 file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 ------------------------------------------------------------------
   Check for the latest version of the board's software
 ------------------------------------------------------------------
**/
"use strict";
(function(){
  
  function init() {
    // Configuration
    Espruino.Core.Config.add("SERIAL_THROTTLE_SEND", {
      section : "Communications",
      name : "Throttle Send",
      description : "Throttle code when sending to Espruino? If you are experiencing lost characters when sending code from the Code Editor pane, this may help.",
      type : "boolean",
      defaultValue : false,
      onChange : function() { 
        checkEnv(Espruino.Core.Env.getData());
      }
    });
    
    // must be AFTER boardJSON
    Espruino.addProcessor("environmentVar", function(env, callback) {
      checkEnv(env);
      callback(env);
    }); 

    Espruino.addProcessor("flashComplete", function(env, callback) {
      if (Espruino.Core.App) {
        var icon = Espruino.Core.App.findIcon("update");
        if(icon) icon.remove();
      }

      callback(env);
    }); 

    Espruino.addProcessor("disconnected", function(env, callback) {
      if (Espruino.Core.App) {
        var icon = Espruino.Core.App.findIcon("update");
        if(icon) icon.remove();
      }
      
      callback(env);
    });
  }
  
  function checkEnv(env) {
    if (env!==undefined && 
        env.VERSION!==undefined) {        
      var tCurrent = env.VERSION;
      var vCurrent = Espruino.Core.Utils.versionToFloat(tCurrent);

      if (vCurrent > 1.43 && env.CONSOLE=="USB") {
        console.log("Firmware >1.43 supports faster writes over USB");
        Espruino.Core.Serial.setSlowWrite(false);
      } else {
        Espruino.Core.Serial.setSlowWrite(true);
      }  

      if (env.info!==undefined &&
          env.info.binary_version!==undefined) {
        var tAvailable = env.info.binary_version;
        var vAvailable = Espruino.Core.Utils.versionToFloat(tAvailable);

        console.log("FIRMWARE: Current "+tCurrent+", Available "+tAvailable);
      
        if (vAvailable > vCurrent && (env.BOARD=="ESPRUINOBOARD" || env.BOARD.substr(0,4)=="PICO")) {
          console.log("New Firmware "+tAvailable+" available");

          if (Espruino.Core.App) Espruino.Core.App.addIcon({
            id:'update',
            icon: 'alert',
            title: 'New Firmware '+ tAvailable +' available. Click to update.',
            order: 999,
            cssClass: 'title-bar__button--alert',
            area: {
              name: "titlebar",
              position: "right"
            },
            click: function(){
              Espruino.Core.MenuSettings.show("Flasher");
            }
          });
        }
      }
    } 
  }
  
  Espruino.Plugins.VersionChecker = {
    init : init,
  };
}());

@@ -235,18 +235,17 @@
 L/2.0/.%0A
- 
 %0A
+
  -------
@@ -459,19 +459,17 @@
 tion()%7B%0A
-  
 %0A
+
   functi
@@ -864,17 +864,16 @@
 tion() %7B
- 
 %0A       
@@ -928,21 +928,17 @@
     %7D);%0A
-    
 %0A
+
     // m
@@ -1071,33 +1071,32 @@
 ck(env);%0A    %7D);
- 
 %0A%0A    Espruino.a
@@ -1307,17 +1307,16 @@
 %0A    %7D);
- 
 %0A%0A    Es
@@ -1505,23 +1505,17 @@
       %7D%0A
-      
 %0A
+
       ca
@@ -1539,19 +1539,17 @@
 %7D);%0A  %7D%0A
-  
 %0A
+
   functi
@@ -1593,17 +1593,16 @@
 fined &&
- 
 %0A       
@@ -1628,24 +1628,16 @@
 fined) %7B
-        
 %0A      v
@@ -1759,17 +1759,28 @@
  1.43 &&
- 
+%0A          (
 env.CONS
@@ -1789,16 +1789,67 @@
 E==%22USB%22
+%7C%7Cenv.CONSOLE==%22Bluetooth%22%7C%7Cenv.CONSOLE==%22Telnet%22))
 ) %7B%0A    
@@ -2036,18 +2036,16 @@
 %0A      %7D
-  
 %0A%0A      
@@ -2327,15 +2327,9 @@
 e);%0A
-      
 %0A
+
     
@@ -3001,16 +3001,13 @@
    %7D
- 
 %0A  %7D%0A
-  
 %0A  E

disable throttling for Bluetooth and Telnet