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

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

# Override this method to return the 'alpha index' structure.
# This is an hash reference containing 'indexes' and page offsets.
# It should look something like this:
#
#  {
#   'a' => 5,
#   'b' => 11,
#   'c' => 24,
#   ...
#  }
#
# Note that the stuff that would sort above 'a' (like titles that begin with a number) don't appear
# in this index:  Those are all assumed to be associated with page '1'.
# And the stuff that would sort _after_ 'z' will not get their own index.
#
# Obviously, _building_ this structure is the tricky part.  I imagine that generally this
# will be created via a static method implemented in the relevant DB::Item class.
#
# Here's an example of a SQL statement that builds a result set from the 'album' table, using
# the 'title' column, with a page size of 25:
#
#  SELECT letter, min(page) from
#  ( SELECT lower(substring(title, 1, 1)) as 'letter', @rownum := @rownum + 1, FLOOR((((@rownum - 1) / 25) + 1)) as 'page'
#    FROM album JOIN ( SELECT @rownum := 0 ) r
#    ORDER BY LOWER(title)
#  ) as subq
#  GROUP BY letter;
#
# The DB::Item accessor should parse this result set and create the hash structure.
#
sub _getAlphaIndex {
    my ( $self, $pageSize ) = @_;
    assert( 0, 'override' );
}

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

    $self->SUPER::_initPagination($collection);
    my $pageSize    = $collection->pageSize();
    my $currentPage = $collection->page();
    my $numPages    = $collection->numPages();

    return unless $numPages > 1;
    my $alphaIndex = $self->_getAlphaIndex($pageSize);
    if ($alphaIndex) {
        my @indexes;
        my $lastPage = 1;
        push @indexes, { 'Key' => '#', 'Page' => 1, 'Selected' => ( 1 == $currentPage ? 1 : 0 ) };
        my @letters = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
        foreach my $letter (@letters) {
            my $page = $alphaIndex->{$letter};

            $page = $lastPage unless $page;
            push @indexes,
              {
                'Key'      => $letter,
                'Page'     => $page,
                'Selected' => ( $page == $currentPage ? 1 : 0 ),
              };
            $lastPage = $page;
        }

        $self->{Pagination}{AlphaIndex} = \@indexes;
    }
}

1;
