#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Statement::Label::BaseStatementList;

use strict;
use warnings;
use Carp;

use Data::Dumper;
use lib '/app/tools/common/lib';
use Common::FormObject;
use Common::Assert;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::LabelRoyaltyStatement;
use RPS::Pagination;

use base 'Common::FormObject';

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

    $self->SUPER::_init(%args);

    $self->{LabelStatement} = [];

    my $runID   = $args{labelRoyaltyRunID};
    my $payorID = $args{payorID};
    assert($runID);
    assert($payorID);

    my $filterBy                    = $args{filterBy};
    my $pageSize                    = $args{pageSize};
    my $page                        = $args{page};
    my $skipTransactionsList        = $args{skipTransactionsList} || 0;

    my $collection = RPS::DB::Item::LabelRoyaltyStatement->Get(
        runID           => $runID,
        payorID         => $payorID,
        sortByPayeeName => 1,
        filterBy        => $filterBy,
    );

    if ( $page && $pageSize ) {
        $collection->setPagination( pageIndex => $page, pageSize => $pageSize );

        $self->{Pagination} = RPS::Pagination->new(
            total     => $collection->totalSize(),
            recordNum => $collection->{_record},
            pageSize  => $pageSize
        );
    }

    if ($collection) {
        while ( $collection->hasNext() ) {
            my $obj = $collection->next();

            # Calling an abstract member function to actually create the correct
            # subclass of BaseLabelRoyaltyStatement.
            # We may skip Transactions List generation to speed up rendering time
            #
            push @{ $self->{LabelStatement} }, $self->_makeStatement($obj, (skipTransactionsList => $skipTransactionsList) );
        }
    }

    return $self;
}

sub _listProperties {
    return { LabelStatement => 1 };
}

sub LabelStatement {
    my ( $self, $index, $methodAddressList, $newValue ) = @_;

    if ( 0 == scalar @$methodAddressList ) {
        if ( defined $newValue ) {
            $self->{LabelStatement}[$index] = $newValue;
        }
        return 1;
    }

    # May need to dynamically add objects to the list...
    #
    if ( !defined $self->{LabelStatement}[$index] ) {
        die "ERROR - no statement with index $index";
    }

    return $self->{LabelStatement}[$index]->_accessProperty( $methodAddressList, $newValue );
}

# Have to override validate to allow it to validate all the members in my list.
#
sub validate {
    my ( $self, $skipRequiredFields ) = @_;

    # A recursive validation mechanism.
    # Subclasses should override this to perform specific
    # validation, if they care too, then call the inherited
    # method (this).  This base method implements the
    # recursive descent.
    #
    my $valid = 1;

    foreach my $item ( @{ $self->{LabelStatement} } ) {
        if ( !$item->validate($skipRequiredFields) ) {
            $valid = 0;
        }
    }

    return $valid;
}

# !!! This is abstract, so I won't _really_ implement this here.
# You _must_ instiantiate this method in subclasses.
#
#sub _makeStatement
#{
#    my ($self, $dbItem) = @_;
#}

1;
