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

package RPS::Import::iTunesRadio::v4;

use strict;

use Data::Dumper;

use lib '/app/tools/rps/lib';
use base 'RPS::Import::Importer';

sub _fieldMap {
    {
        labelName        => 'L',
        countryCode      => 'M',
        dateBegin        => 'B1',
        dateEnd          => 'B2',
        serviceProductID => 'I',
        artistName       => 'J',
        isrc             => 'A',
        trackName        => 'K',
        units            => 'C', # Non-Match User Quantity
    }
}

sub _unverifiedFieldMap {
    {      
        skips                        => 'D',
        grossHeatseekerCMAMatchPlays => 'H',

        footerLabel                  => 'A', # footer area
        radioTotal                   => 'B', # footer area
        footerCurrency               => 'B', # footer area
    }
}

sub _headerIdentifier { ( isrc => 'ISRC' ) }

sub productType { RPS::File::Sale::TYPE_TRACK }
sub formatType { RPS::File::Sale::FORMAT_STREAM }
sub mediaType { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub currencyCode  { shift->{_currencyCode} }

sub price {
    my $self = shift;

    return $self->units * $self->{_unitPrice};  # unitPrice determined from footer; see _preProcess
}

sub units {
    my $self = shift;
    my $units         = $self->_getByFieldName('units');
    my $skips         = $self->_getByFieldName('skips');
    my $grossPlays    = $self->_getByFieldName('grossHeatseekerCMAMatchPlays');

    my $derived_units = $units - $skips - $grossPlays;

    return ( $derived_units );
}

sub _preProcess {
    my $self = shift;
    my %args = @_;

    Log->info( "Starting _preProcess" );
    $self->SUPER::_preProcess(%args);

    my $foundFooter;

    my $totalUnits = 0;
    my $numRows    = 0;

    my $linenum = 0;
    foreach my $line (@{ $args{lines} })
    {
        $self->{line} = $line;

        if( $foundFooter )
        {
            my $footerLabel = $self->_getByFieldName('footerLabel');
            if( $footerLabel =~ /radio total/i )
            {
                my $total = $self->_getByFieldName('radioTotal');
                $self->{_unitPrice} = $total / $totalUnits;
            }
            
            if( !defined $self->{_currencyCode} )
            {
                # if we're in the footer, see if the second cell is a currency code
                my $currencyCode = @{$line}[1];
                if( $currencyCode =~ /^\w{3}$/ )
                {
                    $self->{_currencyCode} = uc($currencyCode);
                }
            }
        }
        else
        {
            my $isrc  = $self->_getByFieldName('isrc');
            my $units = $self->_getByFieldName('units');
            my $skips = $self->_getByFieldName('skips');
            my $plays = $self->_getByFieldName('grossHeatseekerCMAMatchPlays');

            if( $isrc =~ /Country Of Sale/i )
            {
                $foundFooter = 1;
            }
            else
            {
                if( $units || $skips || $plays )
                {
                    $totalUnits += $units - $skips - $plays;
                    $numRows++;
                }
            }
        }

        $linenum++;
    }
}

###
1;# Play nicely.
###
