const fs = require('fs');
const https = require('https');
const WebSocket = require('ws');


const Robot = require('./robot/Robot.js')
const GCode = require('./robot/GCode.js')
                        // 238 + 1 + 25 = 264
let robot = new Robot(250,264,100); //(300,290,10);


const httpsOptions = {
  "enable": true,
  'key': fs.readFileSync('https/localhost.key'),
  'cert':  fs.readFileSync('https/localhost.pem'),
  "passphrase":   "abcd" 
}

server = https.createServer(httpsOptions, (req, res) => {;});

const wss = new WebSocket.Server({ server });

// Input Connection for WebService
wss.on('connection', function connection(ws) 
{
  console.log("WebServer for Input is connected");

  ws.on('message', function incoming(message) 
  {
    if(message == "Ping"){
      wss.clients.forEach(function (client) 
      {
        if (client.readyState == WebSocket.OPEN) 
        {
            client.send( message );
        }
      });
    }

    if(GCode.containsCommand(message)){
      GCode.receiveGCode(robot,message);
      reply = GCode.getM114(robot);
      wss.clients.forEach(function (client) 
      {
        if (client.readyState == WebSocket.OPEN) 
        {
            client.send( reply );
        }
      });
    }

    if(GCode.ContainsFilesCommand(message)){
      reply = GCode.receiveFC(robot, message);
      wss.clients.forEach(function (client) 
      {
        if (client.readyState == WebSocket.OPEN) 
        {
            client.send( reply );
        }
      });
    }
    
    // Request for Status
    if(message == "M114"){
      reply = GCode.getM114(robot);
      wss.clients.forEach(function (client) 
      {
        if (client.readyState == WebSocket.OPEN) 
        {
            client.send( reply );
        }
      });
    }
  });
});


var TenetSender = require('./robot/TelnetSenderGRBL.js')


/*
var telnetSender1 = null; //new TenetSender(urlGRBL = "fluidNcBase.local",   maxSpeedF = 2300, xAxisGrbl = "x", yAxisGrbl = "y", zAxisGrbl = "z");
var telnetSender2 = null; // new TenetSender(urlGRBL = "fluidNcEllbow.local", maxSpeedF = 5000, xAxisGrbl = "a", yAxisGrbl = null, zAxisGrbl = null);
var telnetSender3 = null; // new TenetSender(urlGRBL = "fluidNcHand.local",   maxSpeedF = 5000, xAxisGrbl = "c", yAxisGrbl = "e", zAxisGrbl = "b");
*/

var telnetSender1 = new TenetSender(urlGRBL = "fluidNcBase.local",   maxSpeedF = 2300, xAxisGrbl = "x", yAxisGrbl = "y", zAxisGrbl = "z");
var telnetSender2 = new TenetSender(urlGRBL = "fluidNcEllbow.local", maxSpeedF = 5000, xAxisGrbl = "a", yAxisGrbl = null, zAxisGrbl = null);
var telnetSender3 = new TenetSender(urlGRBL = "fluidNcHand.local",   maxSpeedF = 5000, xAxisGrbl = "c", yAxisGrbl = "e", zAxisGrbl = "b");


setTimeout(function() {
  if(telnetSender1?.tSocket != null) { robot.cmdReceivers.push(telnetSender1); console.log("Socket 1 added");}
  else{ console.log("Socket 1 is not connected"); console.log(telnetSender1?.tSocket);}
  
  if(telnetSender2?.tSocket != null) { robot.cmdReceivers.push(telnetSender2); console.log("Socket 2 added");}
  else{ console.log("Socket 2 is not connected"); console.log(telnetSender2?.tSocket);}

  if(telnetSender3?.tSocket != null) { robot.cmdReceivers.push(telnetSender3); console.log("Socket 3 added");}
  else{ console.log("Socket 3 is not connected"); console.log(telnetSender3?.tSocket);}
}, 8000);

setTimeout(function(){
  if(robot.cmdReceivers.length == 0){
    console.log("Kein GRBL Command-Receiver gefunden. Verwende reines Test-Setting.")
  }
},8002)

console.log("Works with FluidNc Base");


port = 2095
server.listen(port);
console.log("Listen on Port: https://localhost:" + port.toString()+ "/")
