
function sensor_data_collection() {
  var controllerId = Session.get('controller_id');
  return SensorData.find({
    controllerId: controllerId
  }, {
    sort: {
      created_at: -1
    },
    limit: 1
  });
}

function last_sensor_reading() {
  var controller = Session.get('controller_id');
  var result = null;
  if (controller) {
    result = sensor_data_collection();
  }
  if (result && result.count() > 0) {
    return result.fetch()[0];
  } else {
    return {}
  }
}


Template.state.helpers({
  controller_state: controller_state,
  pretty_valve: function(state) {
    if (state === 'open') return "Otvoren";
    if (state === 'opening') return "Otvara se";
    if (state === 'closing') return "Zatvara se";
    if (state === 'closed') return "Zatvoren";
  },

  bucket_image: function() {
    var sensor = last_sensor_reading();
    var stateObject = Meteor.zoblak.client.controller_state();
    if (sensor) {
      if (parseInt(sensor.tankFull) === 0 && stateObject.state.in_valve === 'open' && stateObject.state.out_valve === 'closed') return "/images/barrellFillingUp.png";
      else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'closed')) return "/images/barrellFull.png";
      else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'opening')) return "/images/barrellStartWateringFull.png";
      else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'open')) return "/images/barrellWateringFull.png";
      else if (parseInt(sensor.tankFull) === 1 && (stateObject.state.out_valve === 'closing')) return "/images/barrellStopWateringFull.png";
      else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'closed')) return "/images/barrellNotFull.png";
      else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'opening')) return "/images/barrellStartWateringNotFull.png";
      else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'open')) return "/images/barrellWateringNotFull.png"
      else if (parseInt(sensor.tankFull) === 0 && (stateObject.state.out_valve === 'closing')) return "/images/barrellStopWateringNotFull.png"
      else return "/images/statusAmber.png";
    } else return "/images/statusRed.png";
  },

  last_sensor_reading: last_sensor_reading,

  water_level: function() {
    var sensor = last_sensor_reading();
    if (sensor) {
      if (parseInt(sensor.tankFull) === 1) return '100 %';
      else if (parseInt(sensor.tankLevel4) === 1) return '80 %';
      else if (parseInt(sensor.tankLevel3) === 1) return '60 %';
      else if (parseInt(sensor.tankLevel2) === 1) return '40 %';
      else if (parseInt(sensor.tankLevel1) === 1) return '20 %';
      else '0 %'
      }
    },

  water_now_button_class: function() {
    var stateObject = Meteor.zoblak.client.controller_state();
    if (stateObject.state && (stateObject.state.out_valve === 'open' || stateObject.state.out_valve === 'opening')) {
      return 'hidden btn btn-success';
    } else {
      return 'btn btn-success';
    }
  },
  stop_button_class: function() {
    var stateObject = Meteor.zoblak.client.controller_state();
    if (stateObject.state && (stateObject.state.out_valve === 'closed' || stateObject.state.out_valve === 'closing')) {
      return 'hidden btn btn-success';
    } else {
      return 'btn btn-success';
    }
  },

  start_inflow_button_class: function() {
    var stateObject = Meteor.zoblak.client.controller_state();
    if(stateObject.config && stateObject.config.manualInflow && stateObject.state.out_valve === 'closed' && ( stateObject.state.in_valve === 'closed' ||  stateObject.state.in_valve === 'closing')) {
      return 'btn btn-danger'
    } else {
      return 'hidden btn btn-danger'
    }
  },

  stop_inflow_button_class: function() {
    var stateObject = Meteor.zoblak.client.controller_state();
    if(stateObject.config && stateObject.config.manualInflow && stateObject.state.out_valve === 'closed' && ( stateObject.state.in_valve === 'open' ||  stateObject.state.in_valve === 'opening')) {
      return 'btn btn-danger'
    } else {
      return 'hidden btn btn-danger'
    }
  },


  pretty_days: function(daysInNumbers) {
    var days = ["Nedjelja", "Ponedjeljak", "Utorak", "Srijeda", "Četvrtak", "Petak", "Subota"];
    if (!daysInNumbers || daysInNumbers.length == 0) {
      return "Nikad"
    }
    else if (daysInNumbers.length == 7) {
      return "Svaki dan"
    } else {
      return daysInNumbers.map(function(number) {
        return days[number -1 ];
      }).join(", ");
    }
  },
  pretty_time: function(daysInNumbers, time) {
    if (!daysInNumbers || daysInNumbers.length == 0) {
      return ""
    } else {
      return " u " + time;
    }
  }
});



Template.state.events({
  'click #water_now': function() {
    var controller_id = Session.get('controller_id');
    Meteor.call('openOutValve', controller_id)
  },

  'click #stop_water_now': function() {
    var controller_id = Session.get('controller_id');
    Meteor.call('closeOutValve', controller_id)
  },

  'click #start_inflow_now': function() {
    var controller_id = Session.get('controller_id');
    Meteor.call('openInValve', controller_id)
  },

  'click #stop_inflow_now': function() {
    var controller_id = Session.get('controller_id');
    Meteor.call('closeInValve', controller_id)
  },

  'click #run_settings': function() {
    Modal.show('settings');
  },

  'click #bucket_image': function() {
    Modal.show('state_details', Meteor.zoblak.client.controller_state());
  }

});
