/**
 * Created by cgarcia on 23/10/2019.
 */

function serializeInputs(SPMonitoringsGroup, SPMonitorings) {
  var res = '';
  for (var i = 0; i < SPMonitorings.length; i++) {
    if (SPMonitorings[i].inputAvailable) {
      res +=
        "<ENTRY name='" +
        SPMonitorings[i].MONNAME +
        "' rfentry='" +
        SPMonitorings[i].RFENTRY +
        "' scanning_type='" +
        SPMonitorings[i].MONTYPE +
        "' spid='" +
        (SPMonitorings[i].RFENTRY + 1) +
        "'>\n";
      res += "<INIT_FREQ value='" + SPMonitorings[i].FREQSTART * 1000000 + "' />\n";
      res += "<FINAL_FREQ value='" + SPMonitorings[i].FREQEND * 1000000 + "' />\n";
      if (SPMonitorings[i].MONTYPE == 'FM') {
        res += "<CURSOR_FREQ value='" + (SPMonitorings[i].FREQEND * 1000000 + SPMonitorings[i].FREQSTART * 1000000) / 2 + "' />\n"; //End freq + start freq /2
        res += "<CURSOR_BANDWIDTH value='" + (SPMonitorings[i].FREQEND * 1000000 - SPMonitorings[i].FREQSTART * 1000000) / 2 + "' />\n"; //End freq-start freq /2
      } else {
        res += "<CURSOR_FREQ value='" + SPMonitorings[i].FREQANALYSIS * 1000000 + "' />\n";
        res += "<CURSOR_BANDWIDTH value='" + SPMonitorings[i].BWANALYSIS * 1000000 + "' />\n";
      }
      res += "<BAND value='" + SPMonitoringsGroup.band + "' />\n";
      if (SPMonitoringsGroup.band == 'SAT') {
        res += "<POLARIZATION value='" + SPMonitorings[i].POLARIZATION.split('-')[0] + "' />\n";
        res += "<BANDKU value='" + SPMonitorings[i].POLARIZATION.split('-')[1] + "' />\n";
        res += "<OSCL value='" + SPMonitorings[i].LOWOSC + "' />\n";
        res += "<OSCH value='" + SPMonitorings[i].HIGHOSC + "' />\n";
      }
      res +=
        "<SPCONF average='" +
        SPMonitorings[i].AVERAGECOUNT +
        "' reference_lvl='" +
        SPMonitorings[i].REFERENCELEVEL +
        "' resolution_filter='" +
        SPMonitorings[i].RESOLUTIONFILTERBW +
        "' video_filter='" +
        SPMonitorings[i].VIDEOFILTER +
        "' />\n";
      res += "<REFERENCE has_alarm='0' has_reference='0' n_references='0'>\n";
      res += '<MEASURES />\n';
      if (SPMonitorings[i].THRESHOLDMIN == '-' || SPMonitorings[i].THRESHOLDMAX == '-') {
        var wants_lowerth = SPMonitorings[i].THRESHOLDMIN == '-' ? 0 : 1;
        var wants_upperth = SPMonitorings[i].THRESHOLDMAX == '-' ? 0 : 1;
        var lower_threshold = SPMonitorings[i].THRESHOLDMIN == '-' ? '' : SPMonitorings[i].THRESHOLDMIN;
        var upper_threshold = SPMonitorings[i].THRESHOLDMAX == '-' ? '' : SPMonitorings[i].THRESHOLDMAX;

        res +=
          "<ALARM_PARAMETERS alarm_persistency='" +
          SPMonitorings[i].ALARMPERSISTENCY +
          "' wants_lowerth='" +
          wants_lowerth +
          "' lower_threshold='" +
          lower_threshold +
          "' wants_upperth='" +
          wants_upperth +
          "' upper_threshold='" +
          upper_threshold +
          "' />\n";
      } else {
        if (SPMonitorings[i].THRESHOLDMIN == '' && SPMonitorings[i].THRESHOLDMAX == '') {
          res +=
            "<ALARM_PARAMETERS alarm_persistency='" +
            SPMonitorings[i].ALARMPERSISTENCY +
            "' wants_lowerth='0' lower_threshold='" +
            SPMonitorings[i].THRESHOLDMIN +
            "' wants_upperth='0' upper_threshold='" +
            SPMonitorings[i].THRESHOLDMAX +
            "' />\n";
        } else {
          res +=
            "<ALARM_PARAMETERS alarm_persistency='" +
            SPMonitorings[i].ALARMPERSISTENCY +
            "' wants_lowerth='1' lower_threshold='" +
            SPMonitorings[i].THRESHOLDMIN +
            "' wants_upperth='1' upper_threshold='" +
            SPMonitorings[i].THRESHOLDMAX +
            "' />\n";
        }
      }
      res += '</REFERENCE>\n';
      res += '</ENTRY>\n';
    }
  }
  return res;
}

function SPMoniConfig_toXml(SPMonitoringsGroup, SPMonitorings) {
  var date = new Date();
  var formatDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
  var n_entries = 0;
  var active_entries = '';
  var total_entries = SPMonitorings.length;

  for (var i = 0; i < SPMonitorings.length; i++) {
    if (SPMonitorings[i].inputAvailable) {
      n_entries++;
      active_entries += i == 0 ? '' : ',' + SPMonitorings[i].RFENTRY;
    }
  }

  var res = '<?xml version="1.0" encoding="utf-8"?>\n';
  res +=
    '<SCAN_CONFIG date="' +
    formatDate +
    '" equipment="" name="' +
    SPMonitoringsGroup.name +
    '" ' +
    'description="' +
    SPMonitoringsGroup.description +
    '" trademark="PROMAX ELECTRONICA, S.L." >\n';
  res += '<TIME_PERIOD value="' + SPMonitoringsGroup.timePeriod + '" />\n';
  // res+= '<BAND value="'+SPMonitoringsGroup.band+'" />\n';
  res += '<VALUE_UNITS_SAT value="' + SPMonitoringsGroup.units + '" />\n';
  res += '<VALUE_UNITS_TER value="' + SPMonitoringsGroup.units + '" />\n';
  res += '<FREQ_UNITS value="Hz" />\n';
  res += '<ENTRIES total_entries="' + total_entries + '" n_entries="' + n_entries + '" active_entries="' + active_entries + '">\n';
  res += serializeInputs(SPMonitoringsGroup, SPMonitorings);
  res += '</ENTRIES>\n';
  res += '</SCAN_CONFIG>';

  return res;
}

function SPMoniConfig_toJson(xmlDoc) {
  // var parser = new DOMParser();
  // xmlDoc = parser.parseFromString(xmlFileConfig,"text/xml");

  var jsonObj = {
    xml_name: xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getAttribute('name'),
    xml_description: xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getAttribute('description'),
    xml_timePeriod: xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('TIME_PERIOD')[0].getAttribute('value'),
    xml_units: xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('VALUE_UNITS_TER')[0].getAttribute('value'),
    xml_band: xmlDoc
      .getElementsByTagName('SCAN_CONFIG')[0]
      .getElementsByTagName('ENTRIES')[0]
      .getElementsByTagName('ENTRY')[0]
      .getElementsByTagName('BAND')[0]
      .getAttribute('value'),
    xml_entries: [],
    xml_total_entries: Number(xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].getAttribute('total_entries')),
    xml_n_entries: Number(xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].getAttribute('n_entries')),
    // xml_description: xmlDoc.getElementsByTagName("SCAN_CONFIG")[0].getAttribute("description"),
  };

  for (var i = 0; i < xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].getAttribute('total_entries'); i++) {
    jsonObj.xml_entries.push({});
  }

  //Remove #text element from the DOM
  for (var i = 0; i < xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].childNodes.length; i++) {
    if (
      xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].childNodes[i].nodeName == '#text' ||
      xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].childNodes[i].nodeName == '#comment'
    ) {
      xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].childNodes[i].remove();
      i--;
    }
  }

  for (var i = 0; i < xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].childNodes.length; i++) {
    jsonObj.xml_entries[
      xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].getElementsByTagName('ENTRY')[i].getAttribute('rfentry')
    ] = {
      MONNAME: xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].getElementsByTagName('ENTRY')[i].getAttribute('name'),
      RFENTRY: Number(
        xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].getElementsByTagName('ENTRY')[i].getAttribute('rfentry'),
      ),
      MONTYPE: xmlDoc
        .getElementsByTagName('SCAN_CONFIG')[0]
        .getElementsByTagName('ENTRIES')[0]
        .getElementsByTagName('ENTRY')
        [i].getAttribute('scanning_type'),
      xml_spid: Number(
        xmlDoc.getElementsByTagName('SCAN_CONFIG')[0].getElementsByTagName('ENTRIES')[0].getElementsByTagName('ENTRY')[i].getAttribute('spid'),
      ),
      FREQSTART:
        xmlDoc
          .getElementsByTagName('SCAN_CONFIG')[0]
          .getElementsByTagName('ENTRIES')[0]
          .getElementsByTagName('ENTRY')
          [i].getElementsByTagName('INIT_FREQ')[0]
          .getAttribute('value') / 1000000,
      FREQEND:
        xmlDoc
          .getElementsByTagName('SCAN_CONFIG')[0]
          .getElementsByTagName('ENTRIES')[0]
          .getElementsByTagName('ENTRY')
          [i].getElementsByTagName('FINAL_FREQ')[0]
          .getAttribute('value') / 1000000,
      FREQANALYSIS:
        xmlDoc
          .getElementsByTagName('SCAN_CONFIG')[0]
          .getElementsByTagName('ENTRIES')[0]
          .getElementsByTagName('ENTRY')
          [i].getElementsByTagName('CURSOR_FREQ')[0]
          .getAttribute('value') / 1000000,
      BWANALYSIS:
        xmlDoc
          .getElementsByTagName('SCAN_CONFIG')[0]
          .getElementsByTagName('ENTRIES')[0]
          .getElementsByTagName('ENTRY')
          [i].getElementsByTagName('CURSOR_BANDWIDTH')[0]
          .getAttribute('value') / 1000000,
      AVERAGECOUNT: xmlDoc
        .getElementsByTagName('SCAN_CONFIG')[0]
        .getElementsByTagName('ENTRIES')[0]
        .getElementsByTagName('ENTRY')
        [i].getElementsByTagName('SPCONF')[0]
        .getAttribute('average'),
      REFERENCELEVEL: Number(
        xmlDoc
          .getElementsByTagName('SCAN_CONFIG')[0]
          .getElementsByTagName('ENTRIES')[0]
          .getElementsByTagName('ENTRY')
          [i].getElementsByTagName('SPCONF')[0]
          .getAttribute('reference_lvl'),
      ),
      RESOLUTIONFILTERBW: xmlDoc
        .getElementsByTagName('SCAN_CONFIG')[0]
        .getElementsByTagName('ENTRIES')[0]
        .getElementsByTagName('ENTRY')
        [i].getElementsByTagName('SPCONF')[0]
        .getAttribute('resolution_filter'),
      VIDEOFILTER: xmlDoc
        .getElementsByTagName('SCAN_CONFIG')[0]
        .getElementsByTagName('ENTRIES')[0]
        .getElementsByTagName('ENTRY')
        [i].getElementsByTagName('SPCONF')[0]
        .getAttribute('video_filter'),
      ALARMPERSISTENCY: xmlDoc
        .getElementsByTagName('SCAN_CONFIG')[0]
        .getElementsByTagName('ENTRIES')[0]
        .getElementsByTagName('ENTRY')
        [i].getElementsByTagName('REFERENCE')[0]
        .getElementsByTagName('ALARM_PARAMETERS')[0]
        .getAttribute('alarm_persistency'),
      xml_wants_lowerth_threshold: xmlDoc
        .getElementsByTagName('SCAN_CONFIG')[0]
        .getElementsByTagName('ENTRIES')[0]
        .getElementsByTagName('ENTRY')
        [i].getElementsByTagName('REFERENCE')[0]
        .getElementsByTagName('ALARM_PARAMETERS')[0]
        .getAttribute('wants_lowerth'),
      THRESHOLDMIN:
        xmlDoc
          .getElementsByTagName('SCAN_CONFIG')[0]
          .getElementsByTagName('ENTRIES')[0]
          .getElementsByTagName('ENTRY')
          [i].getElementsByTagName('REFERENCE')[0]
          .getElementsByTagName('ALARM_PARAMETERS')[0]
          .getAttribute('wants_lowerth') == '1'
          ? Number(
              xmlDoc
                .getElementsByTagName('SCAN_CONFIG')[0]
                .getElementsByTagName('ENTRIES')[0]
                .getElementsByTagName('ENTRY')
                [i].getElementsByTagName('REFERENCE')[0]
                .getElementsByTagName('ALARM_PARAMETERS')[0]
                .getAttribute('lower_threshold'),
            )
          : '-',
      xml_upper_threshold: xmlDoc
        .getElementsByTagName('SCAN_CONFIG')[0]
        .getElementsByTagName('ENTRIES')[0]
        .getElementsByTagName('ENTRY')
        [i].getElementsByTagName('REFERENCE')[0]
        .getElementsByTagName('ALARM_PARAMETERS')[0]
        .getAttribute('wants_upperth'),
      THRESHOLDMAX:
        xmlDoc
          .getElementsByTagName('SCAN_CONFIG')[0]
          .getElementsByTagName('ENTRIES')[0]
          .getElementsByTagName('ENTRY')
          [i].getElementsByTagName('REFERENCE')[0]
          .getElementsByTagName('ALARM_PARAMETERS')[0]
          .getAttribute('wants_upperth') == '1'
          ? Number(
              xmlDoc
                .getElementsByTagName('SCAN_CONFIG')[0]
                .getElementsByTagName('ENTRIES')[0]
                .getElementsByTagName('ENTRY')
                [i].getElementsByTagName('REFERENCE')[0]
                .getElementsByTagName('ALARM_PARAMETERS')[0]
                .getAttribute('upper_threshold'),
            )
          : '-',
      POLARIZATION: '',
      SATBAND: '',
      LOWOSC: '',
      HIGHOSC: '',
      DEVIATION: undefined,
      legend: '',
      channels: '',
      spectrumPoints: '',
      className: i == 0 ? 'active' : '',
      inputAvailable: true,
    };
  }

  return jsonObj;
}
