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

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

use lib '/app/tools/rps/lib';
use RPS::Catalog::AlbumSimple;

#use RPS::LabelList;
#use RPS::GenreList;

use RPS::Catalog::DATrackHash;

#use RPS::Catalog::ArtistList;
use RPS::Catalog::DAArtistList;
use RPS::Catalog::TrackHash;

#use RPS::Product::DAProduct;
use RPS::Product::PriceList;
use RPS::Product::StatusList;
use RPS::Product::TypeList;
use RPS::RoyaltyRun::RoyaltyRunStatusList;

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

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

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

    if ( $args{albumID} ) {
        $self->{Album} = $self->_instantiateAlbumObj( $args{albumID} );
    }

    $self->{TrackList} = $self->_instantiateTrackHash( $args{albumID} );

    $self->{RoyaltyRunStatus} = RPS::RoyaltyRun::RoyaltyRunStatusList->new( usMechanical => 1, caMechanical => 1 );

    $self->{TypeList} = RPS::Product::TypeList->new();

    return $self;
}

sub _instantiateAlbumObj {
    my ( $self, $albumID ) = @_;

    return RPS::Catalog::AlbumSimple->new( albumID => $albumID, loadSubs => 0 );
}

sub _instantiateTrackHash {
    my ( $self, $albumID ) = @_;

    return RPS::Catalog::TrackHash->new( albumID => $albumID );
}

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

    # The Product (and contained objects) can be edited.
    #
    return 1 if $propertyName eq 'Album';
    return 1 if $propertyName eq 'TrackList';

    #    return 1 if $propertyName eq 'Product';

    return 0;
}

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

    my $valid = 1;

    my $validator = new Metadata::Validate::Harness( failState => Validate::kStatusWarn );

    if ( !$self->{TrackList}->validate( metadataValidator => $validator ) ) {

        #print STDERR "\n\nTRACKLIST INVALID: " . Dumper($self->{TrackList}) . "\n";
        $valid = 0;
    }

    return $valid;
}

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

    # This is where the interesting things occur.
    #
    # First, we want to save the album data.
    #
    # Next, we will save the track list.
    #
    # Last, the product.
    #
    $self->{TrackList}->save( $self->{Album}->AlbumID() );

}

sub albumID {
    my ($self) = @_;
    return $self->{Album}->AlbumID();
}

###
1;    #
###
