define([
  './templates'
], function () {

  return Marionette.ItemView.extend({
    template: Templates['provisioning-search'],

    events: {
      'submit #provisioning-search-form': 'onFormSubmit',
      'search #provisioning-search-query': 'debouncedOnKeyUp',
      'keyup #provisioning-search-query': 'debouncedOnKeyUp'
    },

    initialize: function () {
      this._bufferedValue = null;
      this.debouncedOnKeyUp = _.debounce(this.onKeyUp, 400);
    },

    onRender: function () {
      this.delegateEvents();
    },

    onFormSubmit: function (e) {
      e.preventDefault();
      this.debouncedOnKeyUp();
    },

    onKeyUp: function () {
      var q = this.getQuery();
      if (q === this._bufferedValue) {
        return;
      }
      this._bufferedValue = this.getQuery();
      if (this.searchRequest != null) {
        this.searchRequest.abort();
      }
      this.searchRequest = this.search(q);
    },

    getQuery: function () {
      return this.$('#provisioning-search-query').val();
    },

    search: function (q) {
      return this.collection.fetch({ reset: true, data: { q: q } });
    }
  });

});
