/**
 * Created by cgarcia on 17/10/2017.
 */

class AlarmsParser {
  constructor(json_alarms, json_static) {
    this.json_alarms = json_alarms.ch_alarm;
    this.json_static = json_static.labels;
    //once "buildAlarmList" method is called alarms are saved in this variable.
    this.channel_alarm = [];
    this.scientificNotation = ['CBER', 'VBER', 'LBER', 'SER'];
    this.floatNotation = ['POWER', 'LEVEL', 'CN', 'VA', 'MER', 'LM'];
  }

  getJSONAlarms() {
    return this.json_alarms;
  }

  //parse the json alarm and json static information and build the channel_alarm
  //variable.
  buildAlarmList() {
    //clear previous data.
    this.channel_alarm = [];
    //iterate over all alarms and build the data.
    for (var ii = 0; ii < this.json_alarms.length; ii++) {
      this.addAlarms(this.json_alarms[ii]);
    }
    return this.channel_alarm;
  }

  addAlarms(channel) {
    var id = [];
    //get the "channel_id" property and add to channel description.
    id.push(channel.channel_id);
    insertAlarms(this.scientificNotation, this.floatNotation, id, channel.alarm, this.channel_alarm, this.json_static);

    function getIdTxt(id) {
      if (id.length == 0) {
        return '';
      }
      var txt = id[0];
      for (var ii = 1; ii < id.length; ii++) {
        txt += ' | ' + id[ii];
      }
      return txt;
    }

    function format(scientific_ids, floats_ids, id, value) {
      if (typeof value == 'number') {
        if (value == 0) {
          return '0';
        }
        if (floats_ids.indexOf(id) != -1) {
          var tmp = Math.round(value * 10) / 10;
          return tmp.toFixed(1);
        }
        if (scientific_ids.indexOf(id) != -1) {
          return value.toExponential(1);
        }
        return value.toFixed(0);
      }
      return value;
    }

    function insertAlarms(scientific_ids, floats_ids, channel_id, alarm, alarm_list, json_static) {
      //search for a object with "alarm" property.
      for (var ii = 0; ii < alarm.length; ii++) {
        if (alarm[ii].hasOwnProperty('alarm')) {
          var clone_id = JSON.parse(JSON.stringify(channel_id));
          clone_id.push(alarm[ii].value);
          insertAlarms(scientific_ids, floats_ids, clone_id, alarm[ii].alarm, alarm_list, json_static);
        } else {
          var item = JSON.parse(JSON.stringify(alarm[ii]));
          item['channel_id'] = getIdTxt(channel_id);
          //round to 1 decimal or pass to scientific notation if id is a
          //BER king measure.
          if (item.hasOwnProperty('id')) {
            item.id = returnStaticSignalId(json_static, item.id);
          }

          if (item.hasOwnProperty('threshold')) {
            item.threshold = format(scientific_ids, floats_ids, item.id, item.threshold);
          }
          if (item.hasOwnProperty('value')) {
            item.value = format(scientific_ids, floats_ids, item.id, item.value);
          }
          alarm_list.push(item);
        }
      }
    }

    //build an object that includes the label of signal_id provided.
    function returnStaticSignalId(json_static, signal_id) {
      for (var jj = 0; jj < json_static.length; jj++) {
        var node = json_static[jj];
        if (node.id == signal_id) {
          return node.label;
        }
      }
    }
  }
}
