#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Distribution::Catalog::Volume;
use strict;
use warnings;

use Data::Dumper;

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

use lib '/app/tools/distribution/lib';
use Distribution::Catalog::Track;

use base 'Common::XMLObject';

# This hash defines what tags/keys we expect.
#
use constant kAttribute    => 'a';
use constant kTag          => 't';
use constant kTrackListObj => 'l';

my %gConfig = (
    'id'           => kAttribute,
    'sequence'     => kTag,
    'total-length' => kTag,
    'track'        => kTrackListObj,
);

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

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

    $self->{_serviceID} = $args{serviceID};

    # So, we'll keep this _very_ simple for now.
    # I don't think we'll even need an XML based constructor here.
    # We'll just take in a hash table reference, and that'll be it.
    #
    my $hashRef = $args{hash};
    assert( $hashRef, "ERROR - Missing 'hash' argument" );

    # It would be quicker, I suppose, to just hang onto the hash reference
    # and output that directly...  But, I'd like to give us the chance to
    # do some sort of validation.  And some of these keys need to become 'attributes'.
    # Plus, hanging onto references that get passed around is a good way to leak memory.
    #
    foreach my $expectedKey ( keys %gConfig ) {
        assert( exists $hashRef->{$expectedKey}, "ERROR - hash missing key $expectedKey" );
        if ( kAttribute eq $gConfig{$expectedKey} ) {
            $self->setXMLParam( $expectedKey, Common::UTF8::Encode( $hashRef->{$expectedKey} ) );
        } elsif ( kTag eq $gConfig{$expectedKey} ) {
            $self->{$expectedKey} = Common::UTF8::Encode( $hashRef->{$expectedKey} );
        } elsif ( kTrackListObj eq $gConfig{$expectedKey} ) {

            # I don't think we'll need a whole seperate class for the list of tracks -
            # Just a class for the track.
            my $trackList = $hashRef->{$expectedKey};
            foreach my $trackHash (@$trackList) {

                # Instantiate a Track, save it here.
                #
                my $track = Distribution::Catalog::Track->new( hash => $trackHash, serviceID => $self->{_serviceID} );
                push @{ $self->{track} }, $track;
            }
        }
    }

    return $self;
}

# Returns an array ref containing all the 'Distribution::Track' objects.
#
sub getTracks {
    my ($self) = @_;

    return $self->{track};
}

sub asHashRef {
    my $self = shift;
    my $result;
    my %data;
    my $dest_key;

    foreach my $key ( keys( %{$self} ) ) {
        next if ( $key =~ /^_/ );
        next if ( $key =~ /^track/ );

        $dest_key = $key;
        $dest_key =~ s/-/_/g;

        if ( ref( $self->{$key} ) && ref( $self->{$key} ) eq 'HASH' ) {
            %data = %{ $self->{$key} };
            $result->{$dest_key} = \%data;
        } elsif ( ref( $self->{$key} ) && ref( $self->{$key} ) ne 'ARRAY' ) {
            $result->{$dest_key} = $self->{$key}->asHashRef();
        } else {
            $result->{$dest_key} = $self->{$key};
        }
    }

    $result->{tracks} = [];
    foreach my $track ( @{ $self->{track} } ) {
        push( @{ $result->{tracks} }, $track->asHashRef() );
    }

    return $result;
}

# Set sequence numbers for vendors that don't have volume numbers
sub setSequence {
    my $self     = shift;
    my $maxTrack = shift;
    my $track;

    my @tracks = @{ $self->{track} } if ( $self->{track} );

    foreach $track (@tracks) {
        $track->setSequence( $maxTrack, $self->{sequence} );
    }
}

1;
