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

use strict;
use warnings;
use Data::Dumper;
use Carp;
use Date::Calc qw( Date_to_Days Decode_Date_US );

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Util qw(decodeDateMysql);
use Common::Log;

use lib '/app/tools/rps/lib';
use RPS::License::BaseTrackLicense;
use RPS::License::LicenseType;
use RPS::Publisher::US::Publisher;
use RPS::DB::Item::TrackLicense;
use RPS::DB::Item::LicenseReserve;
use RPS::DB::Item::ReserveLiquidation;
use RPS::DB::Item::MechanicalStatementItem;
use RPS::DB::Item::MechanicalStatementLicense;
use RPS::DB::Item::PublisherTrackCrossedLicenseAccount;
use RPS::License::ReserveLiquidationList;
use RPS::DB::Item::ReserveLiquidation;

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

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

sub _type { return RPS::License::LicenseType::kRingtone; }

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;
            }
        }

        # And lastly let's check for an account (which would mean transactions or a balance).

        if ( $inUse == 0 ) {
            my $trackLicense = RPS::DB::Item::TrackLicense->Lookup( track_license_id => $props->{track_license_id} );
            if ( $trackLicense->finance_account_id ) {
                $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 other ringtone licenses.
        #

        next if ( $similarLicense->type != RPS::DB::Item::TrackLicense::kRingtone );

        # Uncomment the following if we ever want to prevent duplicate Ringtone licenses again (FB10165).

        #my $ccMapItems = RPS::DB::Item::RegionCountryMap->GetByRegionID($similarLicense->region_id());
        #while (my $mapItem = $ccMapItems->next())
        #{
        #    $countryCodeMap{$mapItem->country_code} = 1;
        #}
    }

    return \%countryCodeMap;
}

sub createPublisher {
    my $self = shift;

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

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

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

###
1;    #
###
