var AppDispatcher = require('../dispatcher/appDispatcher');
var NavigationConstants = require('../constants/navigationConstants');

// Define action methods
var NavigationActions = {
  // select item
  goToItemDetails: function(item) {
    
    AppDispatcher.handleAction({
      actionType: NavigationConstants.CHANGE_URL,
      url: '/artikal/' + item.get('id') +'/' + item.get('name')
    });
  },

  goToSection: function(section) {
    AppDispatcher.handleAction({
      actionType: NavigationConstants.CHANGE_URL,
      url: '/sekcija/'+ section.get('id') + '/' + section.get('name')
    });
  },
  _getQueryStringPart: function(query, offset, limit) {
    var q = '';
    var qp = [];

    if(query) {
      for(var key in query) {

        if (key !== 'offset' && key !== 'limit' && query.hasOwnProperty(key)) {
          qp.push(key + '=' + query[key]);
        }
      }
    }
    if (offset !== undefined) {
      qp.push('offset='+offset);
    }

    if (limit !== undefined) {
      qp.push('limit='+limit);
    }

    if (qp.length > 0) {
      q = '?' + qp.join('&');
    }

    return q;
  },

  goToCategory: function(category,section, query, offset, limit) {
    var url ='/sekcija/' + section.get('name') +'/kategorija/'+ category.get('id') + '/' + category.get('name');
    var q = this._getQueryStringPart(query, offset, limit);
    AppDispatcher.handleAction({
      actionType: NavigationConstants.CHANGE_URL,
      url: (url + q)
    });
  },

  goToSubCategory: function(subCategory, offset, limit, query) {
      var q = this._getQueryStringPart(query, offset, limit);
      var url = '/podkategorija/' + subCategory.get('id') + '/' + subCategory.get('name');
    AppDispatcher.handleAction({
      actionType: NavigationConstants.CHANGE_URL,
      url: (url  + q)
    });
  },

  goToCart: function() {
     AppDispatcher.handleAction({
      actionType: NavigationConstants.CHANGE_URL,
      url: '/korpa'
    });
  },

  goToCheckout: function() {
     AppDispatcher.handleAction({
      actionType: NavigationConstants.CHANGE_URL,
      url: '/dostava'
    });
  },
  goToHome: function() {
     AppDispatcher.handleAction({
      actionType: NavigationConstants.CHANGE_URL,
      url: '/'
    });
  },

  goToThankYou: function() {
    AppDispatcher.handleAction({
      actionType: NavigationConstants.CHANGE_URL,
      url: '/hvala'
    });
  },
  goToSearchResults : function(q) {
    AppDispatcher.handleAction({
      actionType: NavigationConstants.CHANGE_URL,
      url: '/pretraga?q=' + q
    });
  },
  goToMenuItem: function(menuItem) {
    var url = '';
    if (menuItem.get) {
        url = menuItem.get('url');
    } else {
        url = menuItem.url;
    }

    AppDispatcher.handleAction({
      actionType: NavigationConstants.CHANGE_URL,
      url: url
    });
  }
};

module.exports = NavigationActions;
