/* global React, ReactDOM */

import { Controller } from 'components/fxos-mvc/dist/mvc';

import HomeView from 'js/views/home';

export default class HomeController extends Controller {
  constructor(options) {
    super(options);

    this.mountNode = document.getElementById('main');
  }

  main() {
    this.hue.addEventListener('message', this.showMessage.bind(this));
    this.hue.addEventListener('dismiss-message', this.hideMessage.bind(this));

    this.view = ReactDOM.render(React.createElement(HomeView, {
      devices: [],
      hue: this.hue
    }), this.mountNode);

    this.hue.connectToBridge()
      .then(this.hue.getLights.bind(this.hue))
      .then(response => {
        console.log(response);
        this.view = ReactDOM.render(React.createElement(HomeView, {
          devices: response,
          hue: this.hue
        }), this.mountNode);
      })
      .catch(error => {
        console.error(error);
      });

    this.db.getTags()
      .then(tags => {
        console.log(tags);
      })
      .catch(error => {
        console.error(error);
      });
  }

  teardown() {
    this.hue.removeEventListener('message', this.showMessage.bind(this));
    this.hue.removeEventListener('dismiss-message', this.hideMessage.bind(this));
  }

  showMessage(message) {
    this.view.setState({
      visible: true,
      title: message.title,
      body: message.body
    });
  }

  hideMessage() {
    this.view.setState({ visible: false });
  }
}

@@ -665,24 +665,23 @@
   .then(
-response
+devices
  =%3E %7B%0A  
@@ -698,24 +698,23 @@
 ole.log(
-response
+devices
 );%0A     
@@ -799,16 +799,15 @@
 es: 
-response
+devices
 ,%0A  
@@ -856,16 +856,533 @@
 ntNode);
+%0A%0A        devices.forEach((device, id) =%3E %7B%0A          let lightId = (id + 1);%0A%0A          this.db.getDevice(device.uniqueid)%0A            .then(deviceData =%3E %7B%0A              if (!deviceData) %7B%0A                this.db.setDevice(%7B%0A                  id: device.uniqueid,%0A                  lightId: lightId%0A                %7D);%0A              %7D else %7B%0A                deviceData.data.lightId = lightId; // In case the id changed.%0A                this.db.setDevice(deviceData.data);%0A              %7D%0A            %7D);%0A        %7D);
 %0A      %7D

Store the devices data in the indexedDB