export const version = () => "1.0.0";

/* TODO : Créer le modèle objet ici */

export const sensorType = { 'TEMPERATURE':'TEMPERATURE',
                            'HUMIDITY':'HUMIDITY',
                            'LIGHT':'LIGHT',
                            'SWITCH':'SWITCH',
                            'DOOR':'DOOR',
                            'FAN_SPEED':'FAN_SPEED',
                            'ON_OFF':'ON_OFF',
                            'PERCENT':'PERCENT'
                          };

// SENSOR /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

export class Sensor{
  constructor(param){
    this._id = param.id;
    this._name = param.name;
  }
  get id(){
    return this._id;
  }
  get name(){
    return this._name;
  }
  toString() {
    return ( 'id: ' + this._id + ' name: ' + this._name);
  }
}

export class TemperatureSensor extends Sensor{
  constructor(param){
    super(param);
    this._type ='TEMPERATURE';
    this._dataSensor = new TimeSeries(param.dataSensor);
  }
  get type(){
    return this._type;
  }
  get dataSensor(){
    return this._dataSensor;
  }
  lastTemperature(){
    return this._dataSensor.lastMesure();
  }
  temperature(){
    return 'the last mesurement was ' + this._dataSensor.lastMesure() + '°C at ' + this._dataSensor.lastMesureDate();
  }
  toString() {
    return ( 'id: ' + this._id + ' type: ' + this._type + ' name: ' + this._name);
  }
  addTemperature(value){
    this._dataSensor.addValue(value);
  }
}

export class PercentSensor extends Sensor{
  constructor(param){
    super(param);
    this._type ='Percent';
    this._dataSensor = new TimeSeries(param.dataSensor);
  }
  get type(){
    return this._type;
  }
  get dataSensor(){
    return this._dataSensor;
  }
  lastPercent(){
    return this._dataSensor.lastMesure();
  }
  /*temperature(){
    return 'the last mesurement was ' + this._dataSensor.lastMesure() + '°C at ' + this._dataSensor.lastMesureDate();
  }*/
  toString() {
    return ( 'id: ' + this._id + ' type: ' + this._type + ' name: ' + this._name);
  }
  addPercent(value){
    this._dataSensor.addValue(value);
  }
}


export class OnOffSensor extends Sensor{
  constructor(param){
    super(param);
    this._type ='ON_OFF';
    this._dataSensor = new Datum(param.dataSensor);
  }
  get type(){
    return this._type;
  }
  get dataSensor(){
    return this._dataSensor;
  }
  toString() {
    return ( 'id: ' + this._id + ' type: ' + this._type + ' name: ' + this._name);
  }
  lastLight(){
    return this._dataSensor.lastMesure();
  }
  setOnOff(value){
    this._dataSensor.setValue(value);
  }
  /*light(){
    return 'the last mesurement was ' + this._dataSensor.lastMesure() + ' Lumens at ' + this._dataSensor.lastMesureDate();
  }*/
}

export class HumiditySensor extends Sensor{
  constructor(param){
    super(param);
    this._type ='HUMIDITY';
    this._dataSensor = new TimeSeries(param.dataSensor);
  }
  get type(){
    return this._type;
  }
  get dataSensor(){
    return this._dataSensor;
  }
  toString() {
    return ( 'id: ' + this._id + ' type: ' + this._type + ' name: ' + this._name);
  }
  lastHumidity(){
    return this._dataSensor.lastMesure();
  }
  humidity(){
    return 'the last mesurement was ' + this._dataSensor.lastMesure() + '% at ' + this._dataSensor.lastMesureDate();
  }
}

export class LightSensor extends Sensor{
  constructor(param){
    super(param);
    this._type ='LIGHT';
    this._dataSensor = new TimeSeries(param.dataSensor);
  }
  get type(){
    return this._type;
  }
  get dataSensor(){
    return this._dataSensor;
  }
  toString() {
    return ( 'id: ' + this._id + ' type: ' + this._type + ' name: ' + this._name);
  }
  lastLight(){
    return this._dataSensor.lastMesure();
  }
  light(){
    return 'the last mesurement was ' + this._dataSensor.lastMesure() + ' Lumens at ' + this._dataSensor.lastMesureDate();
  }
}

export class SwitchSensor extends Sensor{
  constructor(param){
    super(param);
    this._type ='SWITCH';
    this._dataSensor = new Datum(param.dataSensor);
  }
  get type(){
    return this._type;
  }
  get dataSensor(){
    return this._dataSensor;
  }
  toString() {
    return ( 'id: ' + this._id + ' type: ' + this._type + ' name: ' + this._name);
  }
  switchState(){
    return this._dataSensor.value;
  }
  switch(){
    let result = 'the switch is ';
    if( this._dataSensor.value === 0)
      result += 'off';
    else
      result += 'on';

    return result;
  }
}

export class DoorSensor extends Sensor{
  constructor(param){
    super(param);
    this._type ='DOOR';
    this._dataSensor = new Datum(param.dataSensor);
  }
  get type(){
    return this._type;
  }
  get dataSensor(){
    return this._dataSensor;
  }
  toString() {
    return ( 'id: ' + this._id + ' type: ' + this._type + ' name: ' + this._name);
  }
  doorState(){
    return this._dataSensor.value;
  }
  door(){
    let result = 'the door is ';
    if( this._dataSensor.value === 0)
      result += 'close';
    else
      result += 'open';

    return result;
  }
}

export class FanSpeedSensor extends Sensor{
  constructor(param){
    super(param);
    this._type ='FAN_SPEED';
    this._dataSensor = new TimeSeries(param.dataSensor);
  }
  get type(){
    return this._type;
  }
  get dataSensor(){
    return this._dataSensor;
  }
  toString() {
    return ( 'id: ' + this._id + ' type: ' + this._type + ' name: ' + this._name);
  }
  lastFanSpeed(){
    return this._dataSensor.lastMesure();
  }
  fanSpeed(){
    return 'the last mesurement was ' + this._dataSensor.lastMesure() + ' t/m at ' + this._dataSensor.lastMesureDate();
  }
}

// DATA /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

export class Data{
	constructor(data){
    //if(Array.isArray(data)){
      //console.log("t")
      this._data = data;
  /* }
    else{
      this._data = new Array();
      this._data.push(data);
      //console.log("D:" + this._data);
    }*/
	}

  get data(){
    return this._data;
  }
}

export class TimeSeries extends Data {
	constructor(data){
		super(data);
    this._values = new Array();
    this._values.push( parseFloat(data) );
		//this._labels = data.labels;
	}

	get values(){
    return this._values;
	}

  addValue(value){
    //console.log("A : "+value);
    this._values.push(parseFloat(value));
  }

	/*set values(val){
		this._values = val;
	}*/

  get labels(){
		return this._labels;
	}

	/*set labels(val){
		this._labels = val;
	}*/

  numberOfValues(){
    return this._values.length;
  }

  moyValues(){
    let result = 0;
    for(let i=0; i<this._values.length;i++){
      result += this._values[i];
    }
    result = result/this._values.length;
    return result;
  }

  lastMesureDate(){
    return this._labels[this._labels.length-1];
  }

  lastMesure(){
    return this._values[this._values.length-1];
  }
}

export class Datum extends Data {
	constructor(data){
		super(data);
		this._value = data;
	}

	get value(){
		return this._value;
	}

  setValue(val){
    this._value = val;
  }
}
