var AppDispatcher = require('../dispatcher/appDispatcher');
var EventEmitter = require('events').EventEmitter;

var FilterCriteriaConstants = require('../constants/filterCriteriaConstants');
var _ = require('underscore');


var _appliedCategoryFilters = {};

var clearCategoryFilters = function() {
  _appliedCategoryFilters = {};
};

var addAppliedCategoryFilter = function(name, value) {
  _appliedCategoryFilters[name] = value;
};

var setAppliedCategoryFilters = function(filters) {
  _appliedCategoryFilters = filters;
};

var removeAppliedCategoryFilter = function(name) {
  delete _appliedCategoryFilters[name];
};
//var loadCategoryDetails = function(categoryId) {
  //var category = new Category({id : categoryId});
  //category.fetch({
    //success: function() {
      //_categoryDetails = category;
      //CategoryStore.emitChange();
    //}
  //});
//};

// Extend SectionStore with EventEmitter to add eventing capabilities
var FilterCriteriaStore = _.extend({}, EventEmitter.prototype, {

    //getCategoryDetails: function() {
      //return _categoryDetails;
    //},
  getAppliedCategoryFilters: function() {
    return _appliedCategoryFilters;
  },
    // Emit Change event
    emitChange: function() {
      this.emit('change');
    },

    // Add change listener
    addChangeListener: function(callback) {
      this.on('change', callback);
    },

    // Remove change listener
    removeChangeListener: function(callback) {
      this.removeListener('change', callback);
    }
});


// Register callback with AppDispatcher
AppDispatcher.register(function(payload) {
  var action = payload.action;
  var text;

  switch(action.actionType) {

    case FilterCriteriaConstants.CLEAR_CATEGORY_FILTERS:
      clearCategoryFilters();
      break;
    case FilterCriteriaConstants.ADD_APPLIED_CATEGORY_FILTER:
      addAppliedCategoryFilter(action.filter.name, action.filter.value);
      break;
    case FilterCriteriaConstants.REMOVE_APPLIED_CATEGORY_FILTER:
      removeAppliedCategoryFilter(action.name);
      break;
    case FilterCriteriaConstants.SET_APPLIED_CATEGORY_FILTERS:
      setAppliedCategoryFilters(action.filters);
      break;
    default:
      return true;
  }

  // If action was responded to, emit change event
  FilterCriteriaStore.emitChange();
  return true;

});

module.exports = FilterCriteriaStore;
