/**
 *
 * User: Martin Donnelly
 * Date: 2016-09-09
 * Time: 11:38
 *
 */
/* global Backbone, _,  WEBSOCKET */
/* jshint browser: true , devel: true*/

'use strict';

var SOCKETMANAGER = (function() {

  var SocketManager = Backbone.Model.extend({

    initialize: function() {
          _.bindAll(this, 'turnOn', 'turnOff');
          this.listeningID = null;
          this.listening = false;
          this.webSocket = new WEBSOCKET(this);

          this.on('message', function(o) {
            console.log('On message', this.listening);
            if (this.listening) {
              this.checkItem(o);
            }

          });
        },
    turnOn: function() {
          console.log('Socket now listening');
          this.listening = true;
        },
    turnOff: function() {
          this.listening = false;
        },
    listenFor: function(id) {
          this.listeningID = this.deviceId.indexOf(id);
        },
    checkItem: function(item) {

        if (item.hasOwnProperty('id')) {
          console.log('id:',item.id);
          if (item.id === 'weather') {
            console.log('Setting weather');
            this.weather.set(item.data);
          }

          if (item.id === 'trains') {
                     console.log(item);
                      this.train.set('data',item.data);

                   }

        }


      }
      , setWeather: function(obj) {
        this.weather = obj;
    },
    setTrain: function(obj) {
            this.train = obj;
        },
    getUpdate: function() {
          this.webSocket.send('update');
    }
  });

  return SocketManager;
}());


