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

use strict;
use warnings;
use Data::Dumper;
use Carp;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Log;

use lib '/app/tools/rps/lib';
use RPS::License::BaseControlledComposition;
use RPS::DB::Item::TrackLicense;
use RPS::DB::Item::LicenseReserve;
use RPS::DB::Item::MechanicalStatementLicense;

use RPS::License::US;
use base 'RPS::License::BaseControlledComposition';


sub _getDBItem
{
    my ($self, $id) = @_;
    return RPS::License::US::GetDBItem($id);
}


sub _homeCountryCode
{
    my ($self) = @_;
    return 'US';
}


sub _type { return RPS::DB::Item::TrackLicense::kControlledComposition; }


sub _getPublisherObject
{
    my ($self, $publisherID) = @_;
    assert($publisherID);

    my $pubObj = RPS::Publisher::US::Publisher->new(publisherID => $publisherID);
    return $pubObj;
}


sub _propertiesFromDBItem
{
	my ($self, $hrProperties, $dbItem) = @_;

    $self->SUPER::_propertiesFromDBItem($hrProperties, $dbItem);
	$hrProperties->{track_license_id} = $dbItem->track_license_id,
	$hrProperties->{publisher_id}	  = $dbItem->publisher_id,
}


sub _initProperties
{
    my ($self, $props) = @_;

	$self->SUPER::_initProperties($props);

    $self->{TrackLicenseID}     = Common::FormObject::Scalar->new(value => $props->{track_license_id}, readOnly => 1);
    $self->{PublisherID}	    = Common::FormObject::Scalar->new(value => $props->{publisher_id});
    
    $self->createPublisher();
    
    if ($props->{track_license_id})
    {
    
        # We need to mark the license so that we can tell if it's ever been used in a run (that wasn't deleted).
        #
        my $inUse = 0;
        
        my $licenseCount = RPS::DB::Item::MechanicalStatementLicense->LicenseCount($props->{track_license_id});
        if ($licenseCount > 0)
        {
            $inUse = 1;    
        }   
        
        # Let's also check for (historical) reserves, just to be safe
        
        if ($inUse == 0)
        {
            my $reserveCount = RPS::DB::Item::LicenseReserve->CountByTrackLicenseID($props->{track_license_id});
            if ($reserveCount > 0)
            {
                $inUse = 1;  
            }     
        }        
             
        $self->{InUse} = $inUse;
   
    }    
    
}


sub _setDBValues
{
    my ($self, $dbItem) = @_;
    assert($dbItem);
    $self->SUPER::_setDBValues($dbItem);
    
    $dbItem->publisher_id($self->PublisherID());  
}


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

    my %countryCodeMap;
    my $similarLicenses = RPS::DB::Item::TrackLicense->GetByTrackPublisherProductType($self->TrackID(),$self->PublisherID, $self->ProductTypeID());
    while (my $similarLicense = $similarLicenses->next())
    {
        # Skip over _this_ track license...
        #
        next if ($self->TrackLicenseID() && $similarLicense->track_license_id == $self->TrackLicenseID());
        # We only care about non-ringtone licenses.
        #
        next if ($similarLicense->type == RPS::DB::Item::TrackLicense::kRingtone);        
        my $ccMapItems = RPS::DB::Item::RegionCountryMap->GetByRegionID($similarLicense->region_id());
        while (my $mapItem = $ccMapItems->next())
        {
            $countryCodeMap{$mapItem->country_code} = 1;
        }
    }


    return \%countryCodeMap;
}


sub _getPublisherObject
{
    my ($self, $publisherID) = @_;
    assert($publisherID);

    my $pubObj = RPS::Publisher::US::Publisher->new(publisherID => $publisherID);
    return $pubObj;
}


sub createPublisher {
	my $self = shift;

	$self->{Publisher} = $self->_getPublisherObject($self->PublisherID)
	    if( $self->PublisherID );
}


###
1;#
###


