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

use lib '/app/tools/common/lib';
use Common::Assert;
use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::PriceTypeVariance;
use BookPub::DB::Item::PriceType;
use BookPub::Price::PriceTypeWithVariance;

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

sub _itemName { return 'PriceType' }

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

    my $dbitem = BookPub::DB::Item::PriceTypeVariance->GetAll();

    while ( my $id = $dbitem->next ) {
        push( @ids, $id->price_type_id );
    }

    return BookPub::DB::Item::PriceType->GetAllByID(@ids);
}

sub _getObj {
    my ( $self, $dbItem ) = @_;
    assert($dbItem);

    return BookPub::Price::PriceTypeWithVariance->new( dbItem => $dbItem, readOnly => 1 );
}

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

    my $valid = 1;
    foreach my $priceType ( @{ $self->{PriceType} } ) {
        if ( !$priceType->validate() ) {
            $valid = 0;
        }
    }

    return $valid;
}

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

    foreach my $priceType ( @{ $self->{PriceType} } ) {
        $priceType->save();
    }
}

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

sub _propertyIsEditable {
    my ( $self, $propertyName ) = @_;

    if ( $propertyName eq 'PriceType' ) {
        return 1;
    }
    return 0;
}

# This is the special sort of list accessor.
#
sub PriceType {
    my ( $self, $index, $methodAddressList, $newValue ) = @_;

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

    # May need to dynamically add objects to the list...
    #
    if ( !defined $self->{PriceType}[$index] ) {

        # something bad is happening here
        assert(0);
    }

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

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

    return $self->{PriceType};
}

1;
