'use strict';

var Dom = require('ampersand-dom');
var View = require('ampersand-view');
var PaginatedSubcollection = require('ampersand-paginated-subcollection');

var ActivityView = require('../views/activity-history');

var perPage = 10;

module.exports = View.extend({
    template: require('../templates/pages/activity-history.jade'),
    initialize: function () {

        this.model.fetch();
        this.activities = new PaginatedSubcollection(this.collection, {
            limit: perPage
        });
        this.collection.fetch({ sort: true, reset: true });
    },
    rePaginate: function () {

        var offset = this.collection.length - perPage;
        var prev = this.queryByHook('activities-prev');
        Dom.removeClass(prev, 'disabled');
        Dom.removeAttribute(prev, 'disabled');

        if (offset < 0) {
            offset = 0;
        }
        if (offset === 0) {
            Dom.addClass(prev, 'disabled');
            Dom.addAttribute(prev, 'disabled');
        }
        this.activities.configure({ offset: offset });
    },
    bindings: {
        'model.displayName': {
            type: 'text',
            hook: 'activity-name'
        }
    },
    render: function () {

        this.renderWithTemplate();
        this.renderCollection(this.activities, ActivityView, this.queryByHook('activity-history'));
        this.listenToAndRun(this.collection, 'reset', this.rePaginate);
    },
    events: {
        'click [data-hook=activities-prev]': 'prevActivities',
        'click [data-hook=activities-next]': 'nextActivities'
    },
    prevActivities: function () {

        var prev = this.queryByHook('activities-prev');
        var next = this.queryByHook('activities-next');
        var offset = this.activities.offset - perPage;
        Dom.removeClass(next, 'disabled');
        Dom.removeAttribute(next, 'disabled');
        Dom.removeClass(prev, 'disabled');
        Dom.removeAttribute(prev, 'disabled');
        if (offset < 0) {
            offset = 0;
        }
        if (offset === 0) {
            Dom.addAttribute(prev, 'disabled');
            Dom.addClass(prev, 'disabled');
        }
        this.activities.configure({ offset: offset });
    },
    nextActivities: function () {

        var prev = this.queryByHook('activities-prev');
        var next = this.queryByHook('activities-next');
        var max = this.collection.length - perPage;
        var offset = this.activities.offset + perPage;
        Dom.removeClass(next, 'disabled');
        Dom.removeAttribute(next, 'disabled');
        Dom.removeClass(prev, 'disabled');
        Dom.removeAttribute(prev, 'disabled');
        if (offset + perPage > this.collection.length) {
            offset = max;
        }
        if (offset === max) {
            Dom.addAttribute(next, 'disabled');
            Dom.addClass(next, 'disabled');
        }
        this.activities.configure({ offset: offset });
    }
});

@@ -85,83 +85,8 @@
 w');
-%0Avar PaginatedSubcollection = require('ampersand-paginated-subcollection');
 %0A%0Ava
@@ -145,27 +145,8 @@
 );%0A%0A
-var perPage = 10;%0A%0A
 modu
@@ -302,119 +302,8 @@
 ();%0A
-        this.activities = new PaginatedSubcollection(this.collection, %7B%0A            limit: perPage%0A        %7D);%0A
     
@@ -400,37 +400,35 @@
  %7B%0A%0A        var 
-offse
+nex
 t = this.collect
@@ -424,35 +424,38 @@
 his.
-collection.length - perPage
+queryByHook('activities-next')
 ;%0A  
@@ -500,32 +500,78 @@
 ivities-prev');%0A
+%0A        if (this.collection.hasNext()) %7B%0A    
         Dom.remo
@@ -570,36 +570,36 @@
 Dom.removeClass(
-prev
+next
 , 'disabled');%0A 
@@ -597,32 +597,36 @@
 bled');%0A        
+ 
+   
 Dom.removeAttrib
@@ -621,36 +621,36 @@
 removeAttribute(
-prev
+next
 , 'disabled');%0A%0A
@@ -652,93 +652,55 @@
 ');%0A
-%0A        if (offset %3C 0) %7B%0A            offset = 0;%0A        %7D%0A        if (offset === 0
+        %7D%0A        if (this.collection.hasPrev()
 ) %7B%0A
@@ -707,35 +707,38 @@
             Dom.
-add
+remove
 Class(prev, 'dis
@@ -754,35 +754,38 @@
             Dom.
-add
+remove
 Attribute(prev, 
@@ -811,63 +811,8 @@
   %7D%0A
-        this.activities.configure(%7B offset: offset %7D);%0A
     
@@ -1035,26 +1035,26 @@
 on(this.
-activities
+collection
 , Activi
@@ -1428,219 +1428,15 @@
     
-var next = this.queryByHook('activities-next');%0A        var offset = this.activities.offset - perPage;%0A        Dom.removeClass(next, 'disabled');%0A        Dom.removeAttribute(next, 'disabled');%0A        Dom.remove
+Dom.add
 Clas
@@ -1460,38 +1460,35 @@
 ');%0A        Dom.
-remove
+add
 Attribute(prev, 
@@ -1504,532 +1504,154 @@
 ');%0A
-        if (offset %3C 0) %7B%0A            offset = 0;%0A        %7D%0A        if (offset === 0) %7B%0A            Dom.addAttribute(prev, 'disabled');%0A            Dom.addClass(prev, 'disabled');%0A        %7D%0A        this.activities.configure(%7B offset: offset %7D);%0A    %7D,%0A    nextActivities: function () %7B%0A%0A        var prev = this.queryByHook('activities-prev');%0A        var next = this.queryByHook('activities-next');%0A        var max = this.collection.length - perPage;%0A        var offset = this.activities.offset + perPage;%0A        Dom.remove
+%0A        this.collection.prev();%0A    %7D,%0A    nextActivities: function () %7B%0A%0A        var next = this.queryByHook('activities-next');%0A        Dom.add
 Clas
@@ -1675,38 +1675,35 @@
 ');%0A        Dom.
-remove
+add
 Attribute(next, 
@@ -1719,375 +1719,38 @@
 ');%0A
-        Dom.removeClass(prev, 'disabled');%0A        Dom.removeAttribute(prev, 'disabled');%0A        if (offset + perPage %3E this.collection.length) %7B%0A            offset = max;%0A        %7D%0A        if (offset === max) %7B%0A            Dom.addAttribute(next, 'disabled');%0A            Dom.addClass(next, 'disabled');%0A        %7D%0A        this.activities.configure(%7B offset: offset %7D
+%0A        this.collection.next(
 );%0A 

use server-side pagination for activity history