#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Support::Collection;

use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tools/appuser/lib';

use lib '/app/tools/rps/lib';
use RPS::Pagination;
use RPS::AlphabeticalPagination;

use RPS::Command;

use constant kDefaultPageSize => 30;

use base 'Common::FormObject';

sub new {
    my ( $class, %args ) = @_;
    my $self = bless {}, $class;

    return $self->_init(%args);
}

sub _init {
    my $self = shift;
    my %args = @_;

    $self->{_dbClass}      = $args{dbClass};
    $self->{_xmlClass}     = $args{xmlClass};
    $self->{_pageSize}     = $args{pageSize} || kDefaultPageSize;
    $self->{_pageIndex}    = $args{page} || 1;
    $self->{_getAllMethod} = $args{getAllMethod} || 'GetAll';
    $self->{_searchMethod} = $args{searchMethod} || 'Search';

    $self->{_query}      = $args{query};
    $self->{_collection} = $args{collection};

    assert( $args{dbClass} );
    assert( $args{xmlClass} );

    $self->_query();

    $self->_initProperties();
    return $self;
}

sub _initProperties {
    my $self = shift;

    $self->{PageSize}    = Common::FormObject::Scalar::Integer->new( value => $self->{_pageSize} );
    $self->{PageIndex}   = Common::FormObject::Scalar::Integer->new( value => $self->{_pageIndex} );
    $self->{PageCount}   = Common::FormObject::Scalar::Integer->new( value => $self->pageCount() );
    $self->{IsPaginated} = Common::FormObject::Scalar::Integer->new( value => $self->isPaginated() );
    $self->{TotalSize}   = Common::FormObject::Scalar::Integer->new( value => $self->{_collection}->totalSize() );
}

sub List {
    my $self = shift;
    return @{ $self->{_list} };
}

sub _query {
    my $self = shift;

    $self->{_collection} = $self->_get_collection()
      unless ( $self->{_collection} );

    $self->{_collection}->setPagination( pageIndex => $self->{_pageIndex}, pageSize => $self->{_pageSize} );

    $self->{List}->{Item} = [];

    return unless ( $self->{_collection} );

    eval "require $self->{_xmlClass}";
    die $@ if ($@);

    while ( $self->{_collection}->hasNext() ) {
        my $data = $self->{_collection}->next();
        push( @{ $self->{List}->{Item} }, $self->{_xmlClass}->new( _dbItem => $data ) );
    }
}

sub _get_collection {
    my $self = shift;

    if ( $self->{_query} ) {
        return $self->_get_search_collection();
    }

    return $self->_get_all_collection();
}

sub _get_search_collection {
    my $self         = shift;
    my $searchMethod = $self->{_searchMethod};
    my $dbClass      = $self->{_dbClass};

    eval "require $self->{_dbClass}";
    die $@ if ($@);

    return $dbClass->$searchMethod( $self->{_query} );
}

sub _get_all_collection {
    my $self         = shift;
    my $getAllMethod = $self->{_getAllMethod};
    my $dbClass      = $self->{_dbClass};

    eval "require $self->{_dbClass}";
    die $@ if ($@);

    return $dbClass->$getAllMethod();
}

sub pageCount {
    my $self = shift;
    $self->{_collection}->numPages();
}

sub currentPage {
    my $self = shift;
    $self->{_pageIndex};
}

sub isPaginated {
    my $self = shift;
    return $self->pageCount > 1;
}

###
1;    # Play nicely.
###
