var AppDispatcher = require('../dispatcher');
var EventEmitter = require('events').EventEmitter;
var assign = require('object-assign');
var assetLoader = require('../modules/asset_loader.js')
var factory = require('../modules/factory')

var CHANGE_EVENT = 'Quiz:Change';

var _state = {
  loaded: false,
  slides: [],
  current: 0, // zero-based index of the current slide
  max: 0,
  loadProgress: 0
}

var goToSlide = function(index){
  var slides = _state.slides;
  var current = _state.current;
  if(slides[current]){
    slides[current].blur();
  }
  if(slides[index]){
    slides[index].focus();
  }
  _state.current = index;
}

var answerQuestion = function(question,answer){
  question.answerWith(answer);
}

var init = function(data){
  var slides = factory.createSlides(data);
  assetLoader.load().then(function(){
    _state.slides = slides;
    _state.max = Math.max(slides.length - 1, 0);
    _state.loaded = true;
    QuizStore.emitChange();
  }, console.error);
  // TODO handle error in loading
  assetLoader.listenToLoad(function(loaded, total){
    var progress = (loaded / total) * 100;
    _state.loadProgress = Math.floor(progress);
    QuizStore.emit(CHANGE_EVENT);
  });
}

var QuizStore = assign({}, EventEmitter.prototype, {
  emitChange: function() {
    this.emit(CHANGE_EVENT);
  },

  /**
   * @param {function} callback
   */
  addChangeListener: function(callback) {
    this.on(CHANGE_EVENT, callback);
  },

  /**
   * @param {function} callback
   */
  removeChangeListener: function(callback) {
    this.removeListener(CHANGE_EVENT, callback);
  },

  getState: function(){
    return _state;
  }
});

// Register callback to handle all updates
AppDispatcher.register(function(action) {

  switch(action.actionType) {
    case 'Quiz:QuestionAnswered':
      answerQuestion(action.question, action.answer);
      QuizStore.emitChange();
      break;

    case 'Quiz:goToSlide':
      goToSlide(action.slideIndex);
      QuizStore.emitChange();
      break;

    case 'Quiz:Init':
      init(action.quizData);
      QuizStore.emitChange();
      break;

    default:
      // no op
  }
});

module.exports = QuizStore;
@@ -430,16 +430,59 @@
 index)%7B%0A
+  if(index %3C= _state.max && index %3E= 0)%7B%0A  
   var sl
@@ -505,16 +505,18 @@
 ides;%0A  
+ 
+ 
 var curr
@@ -537,16 +537,18 @@
 urrent;%0A
+  
   if(sli
@@ -562,24 +562,26 @@
 rent%5D)%7B%0A    
+ 
+ 
 slides%5Bcurre
@@ -592,22 +592,26 @@
 blur();%0A
+  
   %7D%0A  
+ 
+ 
 if(slide
@@ -617,24 +617,26 @@
 es%5Bindex%5D)%7B%0A
+  
     slides%5Bi
@@ -652,18 +652,22 @@
 us();%0A  
+ 
+ 
 %7D%0A
+  
   _state
@@ -684,16 +684,20 @@
  index;%0A
+  %7D%0A
 %7D%0A%0Avar a

check for boundaries before navigating to a slide