ng.module('smart-table')
    .directive('stPagination', function () {
        return {
            restrict: 'EA',
            require: '^stTable',
            scope: {
                stItemsByPage: '=?',
                stDisplayedPages: '=?'
            },
            templateUrl: function(element, attrs) {
              if (attrs.stTemplate) {
                return attrs.stTemplate;
              }
              return 'template/smart-table/pagination.html';
            },
            link: function (scope, element, attrs, ctrl) {

                scope.stItemsByPage = scope.stItemsByPage ? +(scope.stItemsByPage) : 10;
                scope.stDisplayedPages = scope.stDisplayedPages ? +(scope.stDisplayedPages) : 5;

                scope.currentPage = 1;
                scope.pages = [];

                function redraw() {
                    var paginationState = ctrl.tableState().pagination;
                    var start = 1;
                    var end;
                    var i;
                    scope.currentPage = Math.floor(paginationState.start / paginationState.number) + 1;

                    start = Math.max(start, scope.currentPage - Math.abs(Math.floor(scope.stDisplayedPages / 2)));
                    end = start + scope.stDisplayedPages;

                    if (end > paginationState.numberOfPages) {
                        end = paginationState.numberOfPages + 1;
                        start = Math.max(1, end - scope.stDisplayedPages);
                    }

                    scope.pages = [];
                    scope.numPages = paginationState.numberOfPages;

                    for (i = start; i < end; i++) {
                        scope.pages.push(i);
                    }
                }

                //table state --> view
                scope.$watch(function () {
                    return ctrl.tableState().pagination;
                }, redraw, true);

                //scope --> table state  (--> view)
                scope.$watch('stItemsByPage', function () {
                    scope.selectPage(1);
                });

                scope.$watch('stDisplayedPages', redraw);

                //view -> table state
                scope.selectPage = function (page) {
                    if (page > 0 && page <= scope.numPages) {
                        ctrl.slice((page - 1) * scope.stItemsByPage, scope.stItemsByPage);
                    }
                };

                //select the first page
                ctrl.slice(0, scope.stItemsByPage);
            }
        };
    });
