var statusData = [];
var statusDataCable = [];
var statusDataKue = [];

function getConditions() {
  //console.log("%cDAT | Angezeigte Punkte : 4880", "color:blue;");
  //console.log("%cDAT | Angezeigte Linien : ---", "color:blue;");

  console.log("%cSYS | Meldungen werden geladen","color:orange;");

  var req = new XMLHttpRequest();
req.open("GET", jsonStatusURL , true);

req.onreadystatechange = function(){
    if ( req.readyState == 4) {
        if ( req.status >= 200 && req.status < 300 ||req.status == 304 ) {      
            var returnData = req.responseText;
            console.log('Statis geladen');
            statusData = JSON.parse(returnData);
            setConditions();
        } else {
            console.log("Fehler; request.status = " + req.status);
        }
        req = null;
    };
}
req.send();
}

function getConditionsCable() {
  console.log("%cSYS | Meldungen werden geladen","color:orange;");

  var req = new XMLHttpRequest();
req.open("GET", jsonServiceURL , true);

req.onreadystatechange = function(){
    if ( req.readyState == 4) {
        if ( req.status >= 200 && req.status < 300 ||req.status == 304 ) {      
            var returnData = req.responseText;
            console.log('Dienst Statis geladen');
            statusDataCable = JSON.parse(returnData);
            getConditionsKue();
        } else {
            console.log("Fehler; request.status = " + req.status);
        }
        req = null;
    };
}
req.send();
}

function getConditionsKue() {
  console.log("%cSYS | Meldungen werden geladen","color:orange;");

  var req = new XMLHttpRequest();
req.open("GET", jsonKueStatusURL , true);

req.onreadystatechange = function(){
    if ( req.readyState == 4) {
        if ( req.status >= 200 && req.status < 300 ||req.status == 304 ) {      
            var returnData = req.responseText;
            console.log('KUE Statis geladen');
            statusDataKue= JSON.parse(returnData);
            setConditionsCable();
            document.getElementById("loadingMap").innerHTML = "";
        } else {
            console.log("Fehler; request.status = " + req.status);
        }
        req = null;
    };
}
req.send();
}

function setConditions() {
  // Shape Color Change
  var anzahlStatis = statusData.Statis.length;
  var IdLalt = 0;
  var idlSet = [];

  for (var n = 0; n < anzahlStatis; n++) {
    
    if (statusData.Statis[n].IdL > 0) {
     //console.log(statusData.Statis[n].IdL);
      var statusID = statusData.Statis[n].IdL;
      var statusColor = statusData.Statis[n].Co;
      var statusFeld = statusData.Statis[n].Feld;
      var statusNa = statusData.Statis[n].Na;
      var shape = stage.find("#stationCircle"+statusID);
      
        if(idlSet.indexOf(statusID) !== -1){
          //console.log("ID vorhanden! "+statusID)
          
        } else{
          //console.log("ID noch nicht vorhanden! "+statusID)

          shape.fill(statusColor);
          var shapeQuad = stage.find("#station"+statusFeld+"f"+statusID);
          //console.log("StatusID= station"+statusFeld+"f"+statusID+" Statusfarbe= "+statusColor);
          shapeQuad.fill(statusColor);
        

        if (newScale <= 10){
          shape.height((standardMeldestationen/5)*meldungMeldestationen);
          shape.width((standardMeldestationen/5)*meldungMeldestationen);
          shape.strokeWidth((standardMeldestationen/100)*meldungMeldestationen);
        }
        else {
          shape.height(standardMeldestationen/5);
          shape.width(standardMeldestationen/5);
          shape.strokeWidth(standardMeldestationen/100);
        }
      }
        IdLalt = statusID;
        idlSet.push(statusID);
    }
  }
  layer.batchDraw();
}

function setConditionsCable() {
  // Menge an Meldungen Service
  var anzahlStatis = statusDataCable.Statis.length;
  console.log("Anzahl geladener Dienst Stati : "+anzahlStatis);
  // Menge an Meldungen KUE
  var anzahlStatisKue = statusDataKue.Statis.length;
  console.log("Anzahl geladener KUEG Stati : "+anzahlStatisKue);

  // Dienste Meldungen
  if (document.getElementById("buttonDienste").style.opacity > 0.6) {
    for (var n = 0; n < anzahlStatis; n++) {
      var sucheTemp = statusDataCable.Statis[n].IdLD;
      console.log("IdlD Services: "+sucheTemp);
      for (var x = 0; x < jso.Services.length; x++) {
        if (sucheTemp == jso.Services[x].IdLD) {
          //console.log("Test: "+sucheTemp+" IdS "+jso.Services[x].IdS);
          var statusColor = statusDataCable.Statis[n].Co;
          var shape = stage.find("#line"+jso.Services[x].IdS);
          shape.stroke(statusColor);
            shape.strokeWidth(meldungCableThickness/20);
        }
      }
    }
  } 
  else {
    console.log("Dienste Layer abgeschaltet");
  }
  
  // KUE Meldungen
  if (document.getElementById("buttonKabel").style.opacity > 0.6) {
    for (var n = 0; n < anzahlStatisKue; n++) {
      var sucheTemp = statusDataKue.Statis[n].IdLD;
      var sucheSlTemp = statusDataKue.Statis[n].Sl;
  
      for (var x = 0; x < jko.Services.length; x++) {
        if (sucheTemp == jko.Services[x].IdLD && sucheSlTemp == jko.Services[x].Sl) {
         //console.log("Test: "+sucheTemp+" IdS "+jko.Services[x].IdS);
        var statusColor = statusDataKue.Statis[n].Co;
          var shape = stage.find("#line"+jko.Services[x].IdS);
          shape.stroke(statusColor);
          shape.strokeWidth(meldungCableThickness/5); 
        }
      }
    }
  }
  else {
    console.log("KÜG Layer abgeschaltet");
  }

  layer.batchDraw();

}

function ulafCableDisable() {
  var anzahlUlafKabel = jso.Services.length;
  for (var n = 0; n < anzahlUlafKabel; n++) {
        var shape = stage.find("#line"+jso.Services[n].IdS);
        //console.log(jso.Services[n].IdS);
        shape.hide();  
        layer.batchDraw();
  }
  var anzahlKUEKabel = jko.Services.length;
  for (var n = 0; n < anzahlKUEKabel; n++) {
        var shape = stage.find("#line"+jko.Services[n].IdS);
        //console.log(jko.Services[n].IdS);
        shape.show();  
        layer.batchDraw();
  }
}

function kueCableDisable() {
  var anzahlKUEKabel = jko.Services.length;
  for (var n = 0; n < anzahlKUEKabel; n++) {
        var shape = stage.find("#line"+jko.Services[n].IdS);
        //console.log(jso.Services[n].IdS);
        shape.hide();  
        layer.batchDraw();
  }
  var anzahlUlafKabel = jso.Services.length;
  for (var n = 0; n < anzahlUlafKabel; n++) {
        var shape = stage.find("#line"+jso.Services[n].IdS);
        //console.log(jko.Services[n].IdS);
        shape.show();  
        layer.batchDraw();
  }
}
