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

package RPS::Import::AmazonMusicUnlimited;

use strict;

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

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

sub _fieldMap {
    {

        labelName        => 'L',
        countryCode      => 'M',
        dateBegin        => 'P',
        serviceProductID => 'A',
        upc              => 'C',
        artistName       => 'I',
        isrc             => 'E',
        albumName        => 'J',
        trackName        => 'K',
        price            => 'N',
    }
}

sub _unverifiedFieldMap {
    {
        downloadPlays  => 'G',
        streamingPlays => 'H',
    }
}

sub _headerIdentifier { ( isrc => 'isrc' ) }
sub mediaType { RPS::DB::Item::MediaType::kMediaTypeAudio }
sub productType { RPS::File::Sale::TYPE_TRACK }

sub upc {
    my $self = shift;
    return Common::Util::normalize_upc( $self->_getByFieldName('upc') );
}

sub isrc {
    my $self = shift;
    return Common::Util::normalize_isrc( $self->_getByFieldName('isrc') );
}

sub saleDates {
    my $self  = shift;
    my $dt = $self->_getByFieldName('dateBegin');
    if( $dt =~ /^(\d{4})(\d{2})$/ )
    {
        $dt = sprintf("%04d-%02d-01", $1, $2);
    }
    return $self->_getDates( date_begin => $dt );
}

# The following accessors are used in our parent's (SUPER) _saveLine
#
sub formatType {
    return shift->{_formatType}; # set in _saveLine (below)
}

sub unitPrice {
    return shift->{_unitPrice}; # set in _saveLine (below)
}

sub units {
    return shift->{_units}; # set in _saveLine (below)
}

# We can have units from two different columns so we need to override
# _isValidSaleRecord to process lines with units in either location.
#
sub _isValidSaleRecord {
    my $self = shift;
    my $downloadPlays = $self->_getByFieldName('downloadPlays');
    my $streamingPlays = $self->_getByFieldName('streamingPlays');
    return ( ($downloadPlays || $streamingPlays) && ($self->trackName || $self->albumName) );
}

# By default, _saveLine will create a single sale record.  Amazon may
# have multiple sales per line (streams and/or tethered).  To accomodate
# these we'll override _saveLine and create multiple sales records as
# needed.
#
sub _saveLine {
    my $self = shift;

    my $downloadPlays = $self->_getByFieldName('downloadPlays');
    my $streamingPlays = $self->_getByFieldName('streamingPlays');
    my $price = $self->price;

    # revenue is applied evenly to all units
    my $unitPrice = $price / ($downloadPlays + $streamingPlays);

    if( $downloadPlays )
    {
        $self->{_formatType} = RPS::File::Sale::FORMAT_TETHERED;
        $self->{_units} = $downloadPlays;
        $self->{_unitPrice} = $unitPrice;
        $self->SUPER::_saveLine();
    }

    if( $streamingPlays )
    {
        $self->{_formatType} = RPS::File::Sale::FORMAT_STREAM;
        $self->{_units} = $streamingPlays;
        $self->{_unitPrice} = $unitPrice;
        $self->SUPER::_saveLine();
    }
}

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

    my $ccode = $self->_getByFieldName("countryCode");

    # Lookup the currency code
    #
    my $currencyFormat = Common::CurrencyFormat->new( countryCode => $ccode );
    return $currencyFormat->currencyCode() if( $currencyFormat );
    $self->fail("Unable to determine country code from '$ccode'")
}


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