import Ember from 'ember';
import app from '../app';

export default Ember.Controller.extend({
  modelChanged: function() {
    // TODO: unsubscribe
    app.pubnub.subscribe({
        channel: this.get('model.id'),
        message: function(message){
          /*
          var modelName = 'break';
          this.store.push(
            modelName,
            this.store.normalize(modelName, message[modelName])
          );*/
          this.store.find('break', message['break']['id']).then(function (break_) {
            return break_.get('file');
          }).then(function (file) {
            console.log('File loaded!', file);
          });
        }.bind(this)
    });
  }.observes('model'),

  lastBreakChanged: function() {
    var lastBreak = this.get('model.lastBreak').then(function (break_) {
      var lineno = break_.get('lineno');
      var style = document.getElementById('global-style');
      console.log('!!! lineno', lineno);
      /* Notice: this is a little bit hacky, it's hard (or slow) to modify a
      line span in Ember JS, so we insert a global style here
      */
      if (style.sheet.rules.length) {
        style.sheet.deleteRule(0);
      }
      var rule = (
      '#line-%@ { ' +
      '  background-color: green;' +
      '  width: 100%;' +
      '  display: inline-block;' +
      '}').fmt(lineno)
      style.sheet.insertRule(rule, 0);
      //style.sheet.insertRule('body { background-color: red; }'.fmt(lineno), 0);
      //Ember.$('' + lineno).addClass('line-highlight');
    });
  }.observes('model.lastBreak'),

  actions: {
    next: function(){
      this.get('model').next();
    },
    step: function(){
      this.get('model').step();
    },
    resume: function(){
      this.get('model').resume();
    }
  }
  });

@@ -258,107 +258,68 @@
     
-/*%0A          var modelName = 'break';%0A          this.store.push(%0A            modelName,%0A           
+if ('file' in message) %7B%0A            this.store.push('file',
  thi
@@ -336,25 +336,22 @@
 rmalize(
-modelName
+'file'
 , messag
@@ -356,19 +356,18 @@
 age%5B
-modelName%5D)
+'file'%5D));
 %0A   
@@ -377,12 +377,9 @@
     
-);*/
+%7D
 %0A   
@@ -389,31 +389,19 @@
     
-this.store.find
+if 
 ('break'
 , me
@@ -396,25 +396,27 @@
 ('break'
-,
+ in
  message
 %5B'break'
@@ -411,169 +411,96 @@
 sage
-%5B'break'%5D%5B'id'%5D).then(function (break_) %7B%0A            return break_.get('file');%0A          %7D).then(function (file) %7B%0A            console.log('File loaded!', file
+) %7B%0A            this.store.push('break', this.store.normalize('break', message%5B'break'%5D)
 );%0A 
@@ -509,18 +509,16 @@
        %7D
-);
 %0A       

Fix file and break pushing