import './index.css';
import { Sensor, TemperatureSensor, PercentSensor, OnOffSensor, HumiditySensor, LightSensor, SwitchSensor, DoorSensor, FanSpeedSensor } from "./model-objet/sensors.js";

var array_sensors = new Array();
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://127.0.0.1:8080');

client.on('connect', function () {
  client.subscribe('#');
});

client.on('message', function (topic, message) {

  var array_m = JSON.parse(message);
  array_m.topic = topic.split("/")[1];
  let present = false;

  switch(array_m.type){

    case 'TEMPERATURE':
      present = false;
      for(let i=0; i<array_sensors.length;i++){
        if(array_m.topic === array_sensors[i].name){
          //console.log("present temperature");
          present = true;

          array_sensors[i].addTemperature(array_m.value);

          console.log();
          console.log(array_sensors[i].name + ":")
          console.log("Data: " + array_sensors[i].dataSensor.values);
          console.log("Derniere valeur: " + array_sensors[i].lastTemperature());
          console.log("Moyenne: " + array_sensors[i].dataSensor.moyValues());
          set_Box(document.querySelectorAll("[data-name='"+array_sensors[i]._name+"']")[0], array_sensors[i]);
        }
      }
      if(present == false){
        let sensor = new TemperatureSensor({'id':12, 'name':array_m.topic, 'dataSensor':array_m.value})
        array_sensors.push(sensor);
        console.log("non present temperature");
        addBox("red", "temperature", sensor);
      }
    break;

    case 'ON_OFF':
      present = false;
      for(let i=0; i<array_sensors.length;i++){
        if(array_m.topic === array_sensors[i].name){
          console.log("present on_off");
          present = true;

          array_sensors[i].setOnOff(array_m.value);
          console.log(array_sensors[i].dataSensor.value);
          set_Box(document.querySelectorAll("[data-name='"+array_sensors[i]._name+"']")[0], array_sensors[i]);
        }
      }
      if(present == false){
        let sensor = new OnOffSensor({'id':12, 'name':array_m.topic, 'dataSensor':array_m.value});
        array_sensors.push(sensor);
        console.log("non present on_off");
        addBox("orange", "light", sensor);
      }
    break;

    case 'PERCENT':
      present = false;
      for(let i=0; i<array_sensors.length;i++){
        if(array_m.topic === array_sensors[i].name){
          //console.log("present percent");
          present = true;

          array_sensors[i].addPercent(array_m.value);

          console.log();
          console.log(array_sensors[i].name + ":")
          console.log("Data: " + array_sensors[i].dataSensor.values);
          console.log("Derniere valeur: " + array_sensors[i].lastPercent());
          console.log("Moyenne: " + array_sensors[i].dataSensor.moyValues());
          set_Box(document.querySelectorAll("[data-name='"+array_sensors[i]._name+"']")[0], array_sensors[i]);
        }
      }
      if(present == false){
        let sensor = new PercentSensor({'id':12, 'name':array_m.topic, 'dataSensor':array_m.value});
        array_sensors.push(sensor);
        //console.log("non present percent");
        addBox("green", "eye", sensor);
      }
    break;

    default:

  }

});

function set_Box(dom, data){
  console.log(dom);
  if(data.dataSensor.constructor.name == "TimeSeries"){
    dom.getElementsByClassName("value")[0].textContent = Math.round((data.dataSensor.values[data.dataSensor.values.length-1])*100)/100;
    dom.getElementsByClassName("moy")[0].textContent = Math.round((data.dataSensor.moyValues())*100)/100;
  }
  else{
    dom.getElementsByClassName("value")[0].textContent = data.dataSensor.value;
  }
}

function addBox(color, type, data){
  var container = document.getElementById("container");

  var row = container.getElementsByClassName("row")[0];

  var col = document.createElement("div");
  col.className = "col s12 m12 l12";
  row.appendChild(col);

  var sensor = document.createElement("div");
  sensor.setAttribute("data-name", data._name);
  sensor.className = color + " sensor " + type;
  col.appendChild(sensor);

  var name = document.createElement("div");
  name.className = "name";
  name.textContent = data._name;
  sensor.appendChild(name);

  var value = document.createElement("div");
  value.className = "value";
  sensor.appendChild(value);

  if(data.dataSensor.constructor.name == "TimeSeries"){
    var moyenne = document.createElement("div");
    moyenne.className = "moyenne";
    moyenne.textContent = "Average = ";
    sensor.appendChild(moyenne);

    var span = document.createElement("span");
    span.className = "moy";
    moyenne.appendChild(span);
  }

  set_Box(sensor, data);
}
