var models  = require('../models');

var Sensor = function() {
  
  var initHomeManager = function(callback){
    models.Sensor.findAll({ include: [{model: models.SensorLocation, required: true},{model: models.SensorDetails, required: true}], order: 'address ASC'}).then(function(sensors){

  		var listsensors= [];
  		for (var i = 0; i<sensors.length; i++){
  		  var sens = {};
  			var id = sensors[i].identifier.split("+")[0];
  			sens['id'] = id;
  			sens['ident'] = sensors[i].identifier;
  			sens['location'] = sensors[i].SensorLocation.nombre;
  			sens['tipo'] = sensors[i].SensorDetail.nombre;
  			sens['unidad'] = sensors[i].SensorDetail.unidad;
  	    sens['active'] = sensors[i].active;
  
  			listsensors.push(sens);
  		}
  		models.SensorLocation.findAll({where:{nombre: { $ne: null }}}).then(function(locations){
  		  //console.log(locations);
  		  var listlocs = [];
  		  for (var i = 0; i<locations.length; i++){
  		    var loc = {};
  			  loc['id'] = locations[i].address;
  			  loc['nombre'] = locations[i].nombre;
  			  listlocs.push(loc);
  		  }
  
  		  models.SensorDetails.findAll({where:{evento: { $ne: 'unk' }}}).then(function(details){
    		  //console.log(locations);
    		  var listdetails = [];
    		  for (var i = 0; i<details.length; i++){
    		    var det = {};
    			  det['id'] = details[i].tipo;
    			  det['nombre'] = details[i].nombre;
    			  listdetails.push(det);
    		  }
    		  return callback({Sensors: listsensors, Locations: listlocs, Details: listdetails});
    		});
  		});
  	}).catch(function(){
  	  // on error load with empty data
  	  return callback({Sensors: [], Locations: [], Details: [] });
  	});
  }
  
  var insertSensor = function(TipoSensor, LocationSensor, callback){
    models.Sensor.build({ identifier: models.LocationSensor+'+'+TipoSensor, name: 'Sensor of type '+TipoSensor, type: TipoSensor, address: LocationSensor }).save().then(function(result) {
      return callback(result);
    });
  }
  
  var insertLocation = function(name, callback){
    models.SensorLocation.max('address').then(function(max) {
      var adrs = max+1;
      models.SensorLocation.build({address: adrs, nombre: name  }).save().then(function(result) {
        return callback(result);
      });
    });
  }
  
  var updateSensor = function(id, state, callback){
    models.Sensor.update({active: state}, { where: { identifier: id }}).then(function(result){
      return callback(result);
    });
  }
  
  var getSensorType = function(callback){
    models.SensorDetails.findAll().then(function(sensors){
      var listsensors = [];
      for(var i=0;i< sensors.length; i++){
        var sens = {};
        sens['id'] = sensors[i].tipo;
        sens['nombre'] = sensors[i].nombre;
        sens['evento'] = sensors[i].evento;
        
        listsensors.push(sens);
      }
      
      return callback(listsensors);
    }).catch(function(){
      console.log("error getting sensor details");
    });
  }
    
  return {
    initHomeManager: initHomeManager
    ,insertSensor: insertSensor
    ,insertLocation: insertLocation
    ,updateSensor: updateSensor
    ,getSensorType: getSensorType
  }
    
}();

module.exports = Sensor;