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

# !!! Re-writing this to include both Album and Track contracts.
# Basically, this is going to be totally redone.
#

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

use lib '/app/tools/rps/lib';
use RPS::Catalog::AlbumContractWithArtistContractList;
use RPS::Track::TrackListWithContracts;
use RPS::RoyaltyRun::RoyaltyRunStatusList;

use RPS::DB::Item::AlbumContract;
use RPS::DB::Item::TrackContract;

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

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

    assert( $args{albumID} );

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

    # We want to display all the album contracts attached to this album.
    #
    $self->{AlbumContractList} = RPS::Catalog::AlbumContractWithArtistContractList->new( albumID => $args{albumID} );

    # We also want to display all the track contracts attached to tracks on this album.
    # So... the Track::TrackContractList will list all the contracts for a given track.
    # If you pass in a trackID to that guy, it will fetch and return all the contracts, but
    # won't repeat the track XML (which is good for us).
    # We'll need to provide the track XML here.
    #
    # I think I'll just wrap all that stuff up in another object.
    #
    $self->{TrackList} = RPS::Track::TrackListWithContracts->new( albumID => $args{albumID}, simplified => $args{simplified} );

    # if the 'simplified' flag is set then we must fill the TrackList manually using TrackIDs
    if ( $args{simplified} ) {
        $self->_fillSimplifiedTrackContracts();
    }

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

    return $self;
}

# This is a static method that returns the number of album and track contracts
# attached to an album
#
sub GetNumContracts {
    my ($albumID) = @_;

    # This is more efficient than it might look.
    # The DB::ItemCollection class is lazy, so it won't be
    # fetching the entire collection at once. So there won't
    # be a lot of wasted effort here.
    #
    my $albumCollection = RPS::DB::Item::AlbumContract->GetByAlbumID($albumID);
    my $trackCollection = RPS::DB::Item::TrackContract->GetByAlbumID($albumID);

    return ( $albumCollection->size() + $trackCollection->size() );
}

sub _fillSimplifiedTrackContracts {
    my $self = shift;

    # Here we need to overwrite the Track section based on the track IDs
    # and create a data structure similar to the more complex data structure.
    my $aTrackIDs = delete $self->{TrackList}{Track};
    return unless @$aTrackIDs;

    my $aData = RPS::DB::Item::TrackContract->getTrackContractsByTrackIDs($aTrackIDs);

    my $hTrack;
    foreach my $hItem (@$aData) {
        if ( !$hTrack || $hTrack->{TrackID} != $hItem->{TrackID} ) {
            $hTrack = $self->_createTrack($hItem);
            push @{ $self->{TrackList}{Track} }, $hTrack;
        }

        $self->_addTrackContract($hTrack, $hItem);
    }

    return 1;
}


sub _createTrack {
    my ($self, $hItem) = @_;

    return unless $hItem->{TrackID};

    my $hTrack = {
        TrackID           => $hItem->{TrackID},
        Title             => $hItem->{TrackTitle},
        TrackOrder        => $hItem->{TrackOrder},
        TrackContractList => {},
    };

    return $hTrack;
}

sub _addTrackContract {
    my ($self, $hTrack, $hItem) = @_;

    return unless $hItem->{ArtistContractID};

    my $hasPendingExpenses = $hItem->{pending_expenses} ? 1 : 0;
    my $canDelete = $hasPendingExpenses
        ? 0
        : ( $hItem->{processed_expenses} + $hItem->{included_on_royalty_run} ) ? 0 : 1;

    my $issueDate = $hItem->{ArtistContractIssueDate} eq '0000-00-00'
        ? undef
        : $hItem->{ArtistContractIssueDate};

    push @{ $hTrack->{TrackContractList}{TrackContract} }, {
        TrackContractID    => $hItem->{TrackContractID},
        Status             => $hItem->{TrackContractStatus},
        CanDelete          => $canDelete,
        HasPendingExpenses => $hasPendingExpenses,
        Contract           => {
            Title             => $hItem->{ArtistContractTitle},
            ArtistContractID  => $hItem->{ArtistContractID},
            ArtistDescription => $hItem->{ArtistDescription},
            ClientContractID  => $hItem->{ClientContractID},
            IssueDate         => $issueDate,
            ArtistPayee       => {
                Name => $hItem->{ArtistPayeeName},
            },
        }
    };

    return 1;
}


1;
