#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::Report::Dynamic::Licenses::CA;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/report/lib';
use lib '/app/tools/rps/lib';
use lib '/app/tools/appuser/lib';

use Common::Log;
use Common::Assert;
use Common::Client;
use File::Path;
use AppUser::DB::Item::AccessLevel;
use Report::Dynamic::Parameters;
use RPS::DB::Item::DynamicReport::Licenses::CA;
use RPS::DB::Item::CAPublisher;
use RPS::DB::Item::ReserveLiquidation;

use base 'RPS::Report::Dynamic::Licenses::USCABase';


sub Type { 'CA Mechanicals'; }
sub FilenameType { 'CA Licenses' }

sub ClientTypeMask { return Common::Client::kCAMechanicals; }

sub ReserveType { return RPS::DB::Item::ReserveLiquidation::kTypeCAPublishingLicense; }

sub _issuerColumns
{
    my ($self, $columns) = @_;

    push @$columns, 
    (
        { 
            header => { 
                name => 'issuer-license-id', 
            },         
            column => { 
                field => 'issuer_license_id', 
            }, 
        },
        {
            header => {
                name => 'issuer-song-id',
            },
            column => {
                field => 'issuer_song_id',
            },
        },
    );
}

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

    my $dateType;
    my $dateTypeParam = $self->parameters()->{Criteria}->DateType();
    if ($dateTypeParam)
    {
        if ('Date Created' eq $dateTypeParam)
        {
            $dateType = 'date_created';
        }
        elsif ('Date Modified' eq $dateTypeParam)
        {
            $dateType = 'date_modified';
        }
    }

    return RPS::DB::Item::DynamicReport::Licenses::CA->ReportQuery
    (
        dateType => $dateType,
        startDate => $self->parameters()->{Criteria}->StartDate(),
        endDate => $self->parameters()->{Criteria}->EndDate(),
    );
}


my $gPublisherCache;
sub _idToPublisherName
{
    my ($self, %args) = @_;
    my $rowItem = $args{row};
    my $fieldName = $args{field};

    my $outString;
    my $id = $rowItem->$fieldName();

    if ($id)
    {
        if (! $gPublisherCache)
        {
            $gPublisherCache = {};
            my $allPubs = RPS::DB::Item::CAPublisher->GetAll();
            while (my $pub = $allPubs->next())
            {
                $gPublisherCache->{ $pub->ca_publisher_id() } = $pub->publisher_name;
            }
        }
        $outString = $gPublisherCache->{$id};
    }

    return $outString;
}


sub _licenseTypeToString
{
    my ($self, %args) = @_;
    my $rowItem = $args{row};
    my $fieldName = $args{field};

    my $outString;
    my $typeID = $rowItem->$fieldName();

    if ($typeID)
    {
        $outString = 'CA Mechanical' if 1 == $typeID;
        $outString = 'Controlled Composition' if 2 == $typeID;
        $outString = 'Public Domain' if 4 == $typeID;
        $outString = 'Ringtone' if 5 == $typeID;
    }

    return $outString;
}


1;

