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

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

use lib '/app/tools/rps/lib';
use RPS::License::BaseEditForm;
use RPS::License::US::TrackLicenseWithLiquidationSchedule;
use RPS::License::US::RingtoneLicense;
use RPS::License::US::PublicDomain;
use RPS::License::US::ControlledComposition;
use RPS::DB::Item::TrackLicense;
use RPS::Publisher::US::PublisherList;
use RPS::Publisher::US::PublisherListSimple;
use RPS::RoyaltyRun::RoyaltyRunStatusList;

use base 'RPS::License::BaseEditForm';

sub _init {
    my $self = shift;

    $self->SUPER::_init(@_);

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

    return $self;
}

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

    # If we're being passed an actual TrackLicenseID, then we can fetch the db item directly.
    # And by examining the 'type' column, that will let us know what class to use.

    if ( $args{trackLicenseID} ) {
        my $dbItem = RPS::DB::Item::TrackLicense->Lookup( track_license_id => $args{trackLicenseID} );
        die "ERROR - unable to load track license with id " . $args{trackLicenseID} unless $dbItem;

        # !!! This logic used to be buried in the TrackLicense form object code.
        #
        if ( RPS::DB::Item::TrackLicense::kPublishingLicense == $dbItem->type ) {
            return RPS::License::US::TrackLicenseWithLiquidationSchedule->new( _dbItem => $dbItem );
        }

        if ( RPS::DB::Item::TrackLicense::kRingtone == $dbItem->type ) {
            return RPS::License::US::RingtoneLicense->new( _dbItem => $dbItem );
        }

        if ( RPS::DB::Item::TrackLicense::kControlledComposition == $dbItem->type ) {
            return RPS::License::US::ControlledComposition->new( _dbItem => $dbItem );
        }

        if ( RPS::DB::Item::TrackLicense::kPublicDomain == $dbItem->type ) {
            return RPS::License::US::PublicDomain->new( _dbItem => $dbItem );
        }

        die "ERROR - unknown type for track license " . $args{trackLicenseID};
    }

    my $licenseType = $args{licenseType};

    if ( $licenseType == RPS::DB::Item::TrackLicense::kPublicDomain ) {
        return RPS::License::US::PublicDomain->new( trackID => $args{trackID} );
    }

    if ( $args{controlledCompositionID} ) {
        return RPS::License::US::ControlledComposition->new( trackID => $args{trackID} );
    }

    if ( $licenseType == RPS::DB::Item::TrackLicense::kRingtone ) {
        return RPS::License::US::RingtoneLicense->new( trackID => $args{trackID} );
    }

    return RPS::License::US::TrackLicenseWithLiquidationSchedule->new( trackID => $args{trackID} );
}

sub assignCGIParams {
    my $self = shift;

    my $res = $self->SUPER::assignCGIParams(@_);

    $self->{TrackLicense}->createPublisher();

    return $res;
}


1;
