var AppDispatcher = require('../dispatcher/appDispatcher');
var EventEmitter = require('events').EventEmitter;
var Cart = require('../models/cart');
var LinkBannerCollection = require('../models/linkBannerCollection');

var InitializationConstants = require('../constants/initializationConstants')
var _ = require('underscore');

var banners = {};


var state = {
    isEverythingReadyToStartTheShow: false
};

var initializeTheShop = function () { 
  // for now only guarantee that cart is created
  var cart = new Cart();
  cart.save(null, {
    success: function () {

      state.isEverythingReadyToStartTheShow = true;
      InitializationStore.emitChange();
       
    }
  })

  initializeBanners();

 
};


var putBannerOnItsPlace = function(banners, lb) { 

  
  if(lb.get('start_page')) banners['startPage'].push(lb);
  if(lb.get('thank_you_page')) banners['thankYouPage'].push(lb);
  if(lb.get('search_result_page')) banners['searchResultPage'].push(lb);
  if(lb.get('checkout_page')) banners['checkoutPage'].push(lb);

  var itemId = lb.get('item_id');
  if (itemId) { 
    banners['item'][itemId] = banners['item'][itemId] || [];
    banners['item'][itemId].push(lb);
  }

  var sectionId = lb.get('section_id');
  if (sectionId) { 
    banners['section'][sectionId] = banners['section'][sectionId] || [];
    banners['section'][sectionId].push(lb);
  }

  var categoryId = lb.get('category_id');
  if (categoryId) { 
    banners['category'][categoryId] = banners['category'][categoryId] || [];
    banners['category'][categoryId].push(lb);
  }

  var subCategoryId = lb.get('sub_category_id');
  if (subCategoryId) { 
    banners['subCategory'][subCategoryId] = banners['subCategory'][subCategoryId] || [];
    banners['subCategory'][subCategoryId].push(lb);
  } 
  
};

var initializeBanners = function() {
  banners["startPage"] = [];
  banners["searchResultPage"] = [];
  banners["thankYouPage"] = [];
  banners["checkoutPage"] = [];
  banners["header"] = [];
  banners["footer"] = [];
  banners["section"] = {};
  banners["category"] = {};
  banners["subCategory"] = {};
  banners["item"] = {};

  var lbc = new LinkBannerCollection();
  lbc.fetch({
    success: function() { 
      lbc.each(function(linkBanner) { 
        putBannerOnItsPlace( banners, linkBanner );
      } );  

      InitializationStore.emitChange();
    }
  });

};



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

    getState: function() {
        return state;
    }, 


    getBanners: function() { 
      return banners;
    },

    // 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
InitializationStore.dispatchToken = AppDispatcher.register(function(payload) {
    var action = payload.action;

    switch (action.actionType) {

        case InitializationConstants.INITIALIZE:
          initializeTheShop();
          break;
        default:
            return true;
    }

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

});

module.exports = InitializationStore;