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

package RPS::Import::iTunesRadio::v1;

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

use Carp qw(croak);

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

use lib '/app/tools/rps/lib';
use RPS::Import::ServiceMap;

use base 'RPS::Import::Importer';

sub _fieldMap {
    {

        labelName        => 'G',
        artistName       => 'E',
        countryCode      => 'H',
        serviceProductID => 'D',
        isrc             => 'A',
        trackName        => 'F',
        units            => 'C',
    }
}

sub _unverifiedFieldMap {
    {
        dateLabel     => 'A', # header area
        dateField     => 'B', # header area

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

sub saleDates {
    my $self = shift;
    # from header; see _preProcess
    return ($self->{_startDate}, $self->{_endDate}) if( $self->{_startDate} );
}

sub formatType { RPS::File::Sale::FORMAT_STREAM }

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

sub currencyCode { 'USD' }

sub mediaType { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub productType { RPS::File::Sale::TYPE_TRACK }

sub price {
    my $self = shift;
    return $self->units * $self->{_unitPrice};  # unitPrice determined from footer; see _preProcess
}

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( !$foundHeader )
        {
            my $dateLabel = $self->_getByFieldName('dateLabel');
            my $dateField = $self->_getByFieldName('dateField');

            if( $dateLabel =~ /Start Date/i )
            {
                if( $dateField =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/ ) # MM/DD/YYYY
                {
                    $self->{_startDate} = sprintf( "%04d-%02d-%02d", $3, $1, 1 );
                }
            }
            elsif( $dateLabel =~ /End Date/i )
            {
                if( $dateField =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/ ) # MM/DD/YYYY
                {
                    $self->{_endDate} = sprintf( "%04d-%02d-%02d", $3, $1, Days_in_Month($3,$1) );
                }
            }
            elsif( $self->_isHeader() )
            {
                $foundHeader = 1;
            }
        }
        elsif( $foundFooter )
        {
            my $footerLabel = $self->_getByFieldName('footerLabel');
            if( $footerLabel =~ /radio total/i )
            {
                my $total = $self->_getByFieldName('radioTotal');
                $self->{_unitPrice} = $total / $totalUnits;
            }
        }
        else
        {
            my $units = $self->_getByFieldName('units');

            if( $units =~ /label amount/i )
            {
                $foundFooter = 1;
            }
            else
            {
                if( $units )
                {
                    $totalUnits += $units;
                    $numRows++;
                }
            }

        }

        $linenum++;
    }

}

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