/**
Copyright (c) 2018 Espruino Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Author: Alfie Kirkpatrick (jugglingcats@akirkpatrick.com)
**/

(function () {
  var PREFIX = "tcp://"; // prefix to use with -p option for hostname/ip address

  if (typeof require === 'undefined') return; // definitely not node!

  var net;
  try {
    net = require("net");
  } catch (e) {
    console.log("Require net failed - Node Socket disabled");
    return;
  }

  var socket;

  var getPorts = function (callback) {
    var uri = Espruino.Config.SERIAL_TCPIP;
    if (uri.trim() != "") {
      var ips = uri.trim().split(";");
      var portList = [];
      ips.forEach(function (s) {
        s = s.trim();
        if (s.length) portList.push({ path: s, description: "Network connection", type: "socket" });
      })
      callback(portList);
    } else
      callback();
  };

  var openSerial = function (serialPort, openCallback, receiveCallback, disconnectCallback) {
    if (serialPort.substr(0, 6) != 'tcp://') {
      console.error("Invalid connection " + JSON.stringify(serialPort));
      return;
    }
    var host = serialPort.substr(6);
    var port = 23;
    if (host.indexOf(":") >= 0) {
      var i = host.indexOf(":");
      port = parseInt(host.substr(i + 1).trim());
      host = host.substr(0, i).trim();
      if (host == "") host = "localhost";
    }

    socket = net.createConnection(port, host, function (createInfo) {
      openCallback("Ok");
    });
    socket.on("data", function (data) {
      if (receiveCallback !== undefined) {
        var a = new Uint8Array(data.length);
        for (var i = 0; i < data.length; i++)
          a[i] = data[i];
        receiveCallback(a.buffer);
      }
    });
    socket.on("error", function (info) {
      console.error("RECEIVE ERROR:", JSON.stringify(info));
      // node will close the connection
    });
    socket.on("end", function () {
      if (disconnectCallback !== undefined) {
        disconnectCallback();
      }
    });
  };

  var closeSerial = function () {
    if (socket) {
      socket.end();
    }
  };

  var writeSerial = function (data, callback) {
    socket.write(data, undefined, callback);
  };

  Espruino.Core.Serial.devices.push({
    "name": "Node Socket",
    "getPorts": getPorts,
    "open": openSerial,
    "write": writeSerial,
    "close": closeSerial,
  });
})();

@@ -1027,16 +1027,22 @@
 AL_TCPIP
+ %7C%7C %22%22
 ;%0A    if

Fix failure to check for empty uri string