import { Component, OnInit, ViewContainerRef } from '@angular/core';
import { ControllerService } from '../services/controller.service';
import { UIService } from '../services/ui.service';
import { ColorEvent } from 'ngx-color';

@Component({
  selector: 'app-lights',
  templateUrl: './lights.page.html',
  styleUrls: ['./lights.page.scss']
})
export class LightsPage implements OnInit {

  echoDevices: Object;

  mainLight = this.controllerService.echoDeviceUUIDS['Main Light'];
  mainLightSlider: Number = 0;
  mainLightStatus: boolean;
  mainLightAutomaticStatus: boolean;

  mainBedLed = this.controllerService.echoDeviceUUIDS['Main Bed LED Lights'];
  mainBedLedStatus: boolean;
  mainBedLedAutomaticStatus: boolean;
  mainBedLedColor = {r: 255, g: 255, b: 255};


  constructor(public controllerService: ControllerService, public uiService: UIService, public vcRef: ViewContainerRef) { }

  ngOnInit() {
    this.controllerService.echoDevices.subscribe(data => { 
      this.echoDevices = data;
      this.updateDeviceStatus();
    });
  }

  ionViewWillEnter() {
    this.controllerService.updateAllData();
    // setInterval(() => this.updateDeviceStatus(), 1000);
    this.updateDeviceStatus();
  }

  updateDeviceStatus = async () => {
    if(this.echoDevices.hasOwnProperty(this.mainLight)) {
      if(this.echoDevices[this.mainLight]['status'] == "on") {
        this.mainLightStatus = true;
      } else {
        this.mainLightStatus = false;
      }

      if(this.echoDevices[this.mainLight]['automatic_movement'] == true) {
        this.mainLightAutomaticStatus = true;
      } else {
        this.mainLightAutomaticStatus = false;
      }
    }

    if(this.echoDevices.hasOwnProperty(this.mainBedLed)) {
      if(this.echoDevices[this.mainBedLed]['status'] == "on") {
        this.mainBedLedStatus = true;
      } else {
        this.mainBedLedStatus = false;
      }

      if(this.echoDevices[this.mainBedLed]['automatic_movement'] == true) {
        this.mainBedLedAutomaticStatus = true;
      } else {
        this.mainBedLedAutomaticStatus = false;
      }
      
      let colorValues = this.echoDevices[this.mainBedLed]['current_color'].split(",");
      this.mainBedLedColor = { r: parseInt(colorValues[0]), g: parseInt(colorValues[1]), b: parseInt(colorValues[2]) };
    }
  }

  toggleStatusMainLight = async () => {
    if (this.mainLightStatus) {
      let response = await this.controllerService.handleDeviceAction(this.mainLight, "on");
      this.uiService.presentToast(response['message']);
    }

    if (this.mainLightStatus == false) {
      let response = await this.controllerService.handleDeviceAction(this.mainLight, "off");
      this.uiService.presentToast(response['message']);
    }
  }

  toggleAutomaticStatusMainLight = async () => {
    if (this.mainLightAutomaticStatus) {
      let response = await this.controllerService.handleDeviceAction(this.mainLight, "automatic_movement", "on");
      this.uiService.presentToast(response['message']);
    }

    if (this.mainLightAutomaticStatus == false) {
      let response = await this.controllerService.handleDeviceAction(this.mainLight, "automatic_movement", "off");
      this.uiService.presentToast(response['message']);
    }
  }

  toggleStatusMainBedLed = async () => {
    if (this.mainBedLedStatus) {
      let response = await this.controllerService.handleDeviceAction(this.mainBedLed, "on");
      this.uiService.presentToast(response['message']);
    }

    if (this.mainBedLedStatus == false) {
      let response = await this.controllerService.handleDeviceAction(this.mainBedLed, "off");
      this.uiService.presentToast(response['message']);
    }
  }

  toggleAutomaticStatusMainBedLed = async () => {
    if (this.mainBedLedAutomaticStatus) {
      let response = await this.controllerService.handleDeviceAction(this.mainBedLed, "automatic_movement", "on");
      this.uiService.presentToast(response['message']);
    }

    if (this.mainBedLedAutomaticStatus == false) {
      let response = await this.controllerService.handleDeviceAction(this.mainBedLed, "automatic_movement", "off");
      this.uiService.presentToast(response['message']);
    }
  }

  onBedLedColorChange = async ($event: ColorEvent) =>  {
    let color = $event.color.rgb.r.toString() + "," + $event.color.rgb.g.toString() + "," + $event.color.rgb.b.toString()
    let response = await this.controllerService.handleDeviceAction(this.mainBedLed, "set_color", color);
  }

}
