var _ = require('underscore');
var CoreView = require('backbone/core-view');
var template = require('./table-paginator.tpl');
var templateLoader = require('./table-paginator-loader.tpl');
var Notifier = require('builder/components/notifier/notifier');
var errorParser = require('builder/helpers/error-parser');
var QueryUtilsModel = require('builder/helpers/query-utils-model');

module.exports = CoreView.extend({

  tagName: 'div',
  className: 'Table-paginator js-tablePaginator',

  events: {
    'click .js-prev': '_onPrevPage',
    'click .js-next': '_onNextPage'
  },

  initialize: function (opts) {
    if (!opts.rowsCollection) throw new Error('rowsCollection is required');
    if (!opts.tableViewModel) throw new Error('tableViewModel is required');

    this._tableViewModel = opts.tableViewModel;
    this._rowsCollection = opts.rowsCollection;

    this._queryUtilsModel = new QueryUtilsModel({
      tableName: this._rowsCollection._tableName,
      configModel: this._rowsCollection._configModel
    });

    this._rowsCollection.bind('loading', function (item) {
      // Don't take into account row model loading triggers
      if (item.models) {
        this._isLoading = true;
      }
    }, this);
    this._rowsCollection.bind('add', this._onRowAdded, this);
    this._rowsCollection.bind('remove', this._onRowRemoved, this);
    this._tableViewModel.bind('change:page', this._onTablePageChange, this);
    this.add_related_model(this._tableViewModel);
    this.add_related_model(this._rowsCollection);
  },

  render: function () {
    var rowsSize = this._rowsCollection.size();
    var page = this._tableViewModel.get('page');
    var maxRowsPerPage = this._rowsCollection.DEFAULT_FETCH_OPTIONS.rows_per_page;

    this.$el.html(
      template({
        page: this._tableViewModel.get('page'),
        size: rowsSize,
        isNextAvailable: rowsSize >= maxRowsPerPage,
        isPrevAvailable: page > 0
      })
    );

    this.$el.toggleClass('Table-paginator--relative', !!this._tableViewModel.get('relativePositionated'));
    return this;
  },

  _onRowAdded: function () {
    this._queryUtilsModel.fetchCount(function (numRows) {
      if (numRows > -1) {
        // Go to the last page
        var maxRowsPerPage = this._rowsCollection.DEFAULT_FETCH_OPTIONS.rows_per_page;
        var lastPage = Math.ceil(numRows / maxRowsPerPage) - 1;
        this._tableViewModel.set('page', lastPage);
      }
    }.bind(this));
  },

  _onRowRemoved: function () {
    this._rowsCollection.fetch({
      data: this._getData()
    });
  },

  _onTablePageChange: function () {
    this._rowsCollection.fetch({
      data: this._getData(),
      error: function (mdl, response) {
        this._isLoading = false;
        this.render();

        Notifier.addNotification({
          status: 'error',
          info: _t('components.table.rows.paginator.error', { error: errorParser(response) }),
          closable: true
        });
      }.bind(this)
    });
  },

  _getData: function () {
    return _.extend(
      this._tableViewModel.pick('page', 'order_by', 'sort_order'),
      {
        exclude: !this._tableViewModel.isCustomQueryApplied() ? ['the_geom_webmercator'] : []
      }
    );
  },

  _onPrevPage: function (ev) {
    if (!this._isLoading) {
      this.$('.js-prev').html(templateLoader());
      this._tableViewModel.set('page', this._tableViewModel.get('page') - 1);
    }
  },

  _onNextPage: function (ev) {
    if (!this._isLoading) {
      this.$('.js-next').html(templateLoader());
      this._tableViewModel.set('page', this._tableViewModel.get('page') + 1);
    }
  }

});

@@ -851,16 +851,40 @@
 lection;
+%0A    this._numRows = -1;
 %0A%0A    th
@@ -1035,24 +1035,245 @@
 el%0A    %7D);%0A%0A
+    this._queryUtilsModel.fetchCount(function (numRows) %7B%0A      // Check total number of rows to evaluate%0A      // if there is a next page available%0A      this._numRows = numRows;%0A      this.render();%0A    %7D.bind(this));%0A%0A
     this._ro
@@ -2112,19 +2112,36 @@
 le: 
-rowsSize %3E=
+this._numRows %3E (page + 1) *
  max
@@ -2641,51 +2641,166 @@
     
-this._tableViewModel.set('page', lastPage);
+if (lastPage !== this._tableViewModel.get('page')) %7B%0A          this._tableViewModel.set('page', lastPage);%0A        %7D else %7B%0A          this.render();%0A        %7D
 %0A   

Fix next page tick for 40*n rows