const webTCPServer = "165.73.80.45"; //"127.0.0.1"; //165.73.80.85

const webTCPPort = 9999;

const socketServer = "127.0.0.1";

const socketPort = 12052;



var net = new WebTCP(webTCPServer, webTCPPort);

// standard socket options

options = {

  encoding: "binary",

  timeout: 0,

  noDelay: true, // disable/enable Nagle algorithm

  keepAlive: true, //default is false

  initialDelay: 0 // for keepAlive. default is 0

};

// Create a socket. specify host and port of TCP server here

var socket = net.createSocket(socketServer, socketPort, options);



// On connection callback

socket.on("connect", function() {

  console.log("connected");

});



util = {

  getInstaller: function(callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    var installers = {};

    installers.action = "GETINSTALLER";

    var msg = JSON.stringify(installers);

    // console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);
 
    });

  },

  getTodaysInformation: function(callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    var devicedata = {};

    devicedata.action = "GETTODAYSINFORMATION";

    var msg = JSON.stringify(devicedata);

    // console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  },

  getAnalyticsdata: function(callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    var analyticsData = {};

    analyticsData.action = "GETANALYTICS";

    var msg = JSON.stringify(analyticsData);

    // console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  },

  getClientsdata: function(callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    var clientsData = {};

    clientsData.action = "GETALLCLIENTS";

    var msg = JSON.stringify(clientsData);

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  },

  getMonthlyInstalls: function(callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    var devicedata = {};

    devicedata.action = "GETMONTHLYINSTALLS";

    var msg = JSON.stringify(devicedata);

    // console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  },

  getMonthInstalls: function(label, callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    var data = {};

    data.action = "GETMONTHINSTALLS";

    data.month = label;

    var msg = JSON.stringify(data);

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      // console.log(data);

      callback(data);

    });

  },

  checkSerialNumber: function(serial, callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    var data = {};

    data.action = "CHECKSERIAL";

    data.serialnumber = serial;

    var msg = JSON.stringify(data);

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  },

  saveClientData: function(obj, callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    obj.action = "SAVECLIENT";

    var msg = JSON.stringify(obj);

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      // console.log(data);

      callback(data);

    });

  },

  login: function(username, password) {

    socket = net.createSocket(socketServer, socketPort, options);

    var msg =

      '{"action":"LOGIN","username":"' +

      username +

      '","password":"' +

      password +

      '"}';

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      console.log("data sent");

      console.log(data);



      var userInfo = JSON.parse(data);

      if (userInfo.length > 0) {

        console.log(userInfo[0]);



        sessionStorage.setItem("user", JSON.stringify(userInfo[0]));

        window.location = "home.html";

      } else {

        var div = document.getElementsByClassName("card-body");

        div[0].innerHTML +=

          '<div class="alert alert-danger" role="alert">The email and password did not match our records.</div>';

      }

    });

  },

  unlinkdevice: function(serialnumber, reason, callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    var devicedata = {};

    devicedata.action = "UNLINKDEVICE";

    devicedata.serialnumber = serialnumber;

    devicedata.reason = reason;

    var msg = JSON.stringify(devicedata);

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  },

  deactivate: function(cellphone, callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    let devicedata = {};

    devicedata.action = "DEACTIVATE";

    devicedata.cellphone = cellphone;

    var msg = JSON.stringify(devicedata);

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  },

  activate: function(cellphone, callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    let devicedata = {};

    devicedata.action = "ACTIVATE";

    devicedata.cellphone = cellphone;

    var msg = JSON.stringify(devicedata);

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  },

  replacedevice: function(oldserial, newserial, callback) {

    socket = net.createSocket(socketServer, socketPort, options);

    let devicedata = {};

    devicedata.action = "REPLACEDEVICE";

    devicedata.oldserial = oldserial;

    devicedata.newserial = newserial;

    var msg = JSON.stringify(devicedata);

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  },

  getreglocationsperdate:function(registration,date,callback)

  {

    socket = net.createSocket(socketServer, socketPort, options);

    let inData = {};

    inData.action = "OFFICIALREPORT";

    inData.date = date;

    inData.registrationnumber = registration;

    var msg = JSON.stringify(inData);

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  },

  reinstateContract:function(cellphone,callback)

  {

    socket = net.createSocket(socketServer, socketPort, options);

    let inData = {};

    inData.action = "REINSTATECONTRACT";

    inData.cellphone = cellphone;

    var msg = JSON.stringify(inData);

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  },

  cancellContract:function(cellphone,callback)

  {

    socket = net.createSocket(socketServer, socketPort, options);

    let inData = {};

    inData.action = "CANCELLCONTRACT";

    inData.cellphone = cellphone;

    var msg = JSON.stringify(inData);

    console.log(msg);

    socket.write(msg);

    socket.on("data", function(data) {

      callback(data);

    });

  }

};

