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

use strict;
use warnings;
use Carp;
use Data::Dumper;

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

use lib '/app/tools/rps/lib';
use RPS::Statement::Label::StatementLabel;
use RPS::DB::Item::LabelRoyaltyLabelItem;

use Common::FormObject;
use base 'Common::FormObject';

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

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

    my $statementID = $args{labelRoyaltyStatementID};

    $self->{LabelStatementLabel} = [];
    if ($statementID) {
        my $collection = RPS::DB::Item::LabelRoyaltyLabelItem->GetByLabelRoyaltyStatementID($statementID);

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

            my $label = RPS::Statement::Label::StatementLabel->new( _dbItem => $obj );

            push @{ $self->{LabelStatementLabel} }, $label;
        }

        $self->_sortByLabelName();
    }

    return $self;
}

sub _sortByLabelName {
    my ($self) = @_;

    @{ $self->{LabelStatementLabel} } = sort {
             uc( $a->{Label}->LabelName() ) cmp uc( $b->{Label}->LabelName() )
          || compareServiceNames( $a->ServiceName(), $b->ServiceName() )
          || compareCountryNames( $a->CountryName(), $b->CountryName() )
    } @{ $self->{LabelStatementLabel} };
}

sub compareServiceNames {
    my $a = shift;
    my $b = shift;

    if ( uc($a) eq 'ALL' && uc($b) eq 'ALL' ) {
        return 0;
    } elsif ( uc($a) eq 'ALL' ) {
        return 1;
    } elsif ( uc($b) eq 'ALL' ) {
        return -1;
    } else {
        return ( uc($a) cmp uc($b) );
    }
}

sub compareCountryNames {
    my $a = shift;
    my $b = shift;

    if ( uc($a) eq 'WORLD' ) {
        return 1;
    } elsif ( uc($b) eq 'WORLD' ) {
        return -1;
    } else {
        return ( uc($a) cmp uc($b) );
    }
}

sub getList {
    my ($self) = @_;

    return $self->{LabelStatementLabel};
}

1;
