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

package RPS::Import::iTunesRadio::v3;

use strict;

use Carp qw(croak);

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

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

sub _unverifiedFieldMap {
    {
        matchUserQuantity    => 'E',
        skips                => 'F',
        grossHeatseekerPlays => 'J',
        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 free {
    my $self          = shift;
    my $matchQuantity = $self->_getByFieldName('matchUserQuantity');
    my $skips         = $self->_getByFieldName('skips');
    my $grossPlays    = $self->_getByFieldName('grossHeatseekerPlays');
    my $price         = $self->price();

    return ( !$price && ( $matchQuantity || $skips || $grossPlays ) ) ? 1 : 0;
}

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

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

    my $foundHeader;
    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} ) {
                my $currencyCode = $self->_getByFieldName('currencyCode');
                if ( $currencyCode =~ /^\w{3}$/ ) {
                    $self->{_currencyCode} = uc($currencyCode);
                }
            }
        } else {
            my $units = $self->_getByFieldName('units');
            my $isrc  = $self->_getByFieldName('isrc');

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

        $linenum++;
    }
}

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