#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package Common::FormObject::List::NumericPaged;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Assert;
use base 'Common::FormObject::List';

use constant kOtherPage => '0';

use constant kDefaultNavSpread => 9;
sub _navSpread { return kDefaultNavSpread; }

sub _getCollection {
    my ( $self, %args ) = @_;

    # !!! JPK - It's important to remember that this method
    # needs to be pagination aware...
    #
    assert( 0, 'override' );
}

sub _initPagination {
    my ( $self, $collection ) = @_;
    assert($collection);

    $self->SUPER::_initPagination($collection);

    CreateNumericPagination(
        $self->{Pagination},
        currentPage => $self->{Pagination}{Page},
        numPages    => $self->{Pagination}{NumPages},
        spread      => $self->_navSpread(),
    );
}

# JPK - Going to put the code that creates the usual numeric pagination hash in a static method.
# I want to be able to re-use this pattern in places where we might not want to have a 'List' object.
#
sub CreateNumericPagination {
    my ( $outHash, %args ) = @_;
    my $page     = $args{currentPage};
    my $numPages = $args{numPages};
    my $spread   = $args{spread};

    $spread = kDefaultNavSpread unless $spread;

    $outHash->{Page}     = $page;
    $outHash->{NumPages} = $numPages;

    if ( $numPages > 1 ) {

        # If we are on the first page, there is no previous page.
        #
        if ( $page != 1 ) {
            $outHash->{PreviousPage} = $page - 1;
            $outHash->{PreviousPage} = 1 unless $outHash->{PreviousPage} > 1;
        }

        # If there is only 1 page, there is no next page.
        #
        if ( $numPages > 1 && $numPages != $page ) {
            $outHash->{NextPage} = $page + 1;
            $outHash->{NextPage} = $numPages unless $outHash->{NextPage} < $numPages;
        }

        # Let's just build some XML to reflect this nav bar.
        #
        my @nav;
        push @nav, { Index => 1 };

        my $firstLink = $page - $spread;
        if ( $firstLink <= 2 ) {
            $firstLink = 2;
        } else {

            # Index == 0  mean put an elipse in there.
            #
            push @nav, { Index => 0 };
        }

        my $lastLink = $page + $spread;
        if ( $lastLink > ( $numPages - 1 ) ) {
            $lastLink = $numPages - 1;
        }

        for ( my $i = $firstLink ; $i <= $lastLink ; $i++ ) {
            my $navElement = { Index => $i };
            if ( $i == $page ) {
                $navElement->{Selected} = 1;
            }
            push @nav, $navElement;
        }

        if ( $lastLink < ( $numPages - 1 ) ) {

            # Index == 0  mean put an elipse in there.
            #
            push @nav, { Index => 0 };
        }

        push @nav, { Index => $numPages };

        $outHash->{Nav} = \@nav;
    }

    # What I want is to provide everything necessary to build this sort of nav bar:
    #       <<Prev<<  1 ... 45 46 47 _48_ 49 50 51 ... 1045  >>Next>>
    #       <<Prev<<  1 _2_ 3 4 5 ... 1045  >>Next>>
}

1;
