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

package RPS::Import::Merlin::v15;

use strict;

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

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

sub _fieldMap {
    {
        countryCode => 'H',
        dateBegin   => 'A',
        dateEnd     => 'B',
        labelName   => 'N',
        artistName  => 'D',
        upc         => 'G',
        albumName   => 'F',
        isrc        => 'C',
        trackName   => 'E',
        units       => 'I',
        price       => 'J',
    };
}

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

sub saleDates {
    my $self = shift;

    my $dateBegin = $self->_getByFieldName('dateBegin') || '';
    my $dateEnd   = $self->_getByFieldName('dateEnd')   || '';

    my ($ddmmyyyy, $mmddyyyy) = (0, 0);
    if ( $dateEnd =~ /^(\d{1,2})[\.\-\/](\d{1,2})[\.\-\/](\d{4})$/ ) {
        $ddmmyyyy = 1 if $1 > 12;
        $mmddyyyy = 1 if $2 > 12;
    }

    my $sdate = $self->_prepareDate( $dateBegin, $ddmmyyyy, $mmddyyyy );
    my $edate = $self->_prepareDate( $dateEnd, $ddmmyyyy, $mmddyyyy );

    return $self->_getDates( date_begin => $sdate, date_end => $edate);
}

sub _prepareDate {
    my ( $self, $date, $ddmmyyyy, $mmddyyyy ) = @_;

    # YYYY-DD-MM
    if ( $date =~ /^(\d{4})[\.\-\/](\d{1,2})[\.\-\/](\d{1,2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $3, $2;
    }
    # DD-MM-YYYY or MM-DD-YYYY
    elsif ( $date =~ m/^(\d{1,2})[\.\-\/](\d{1,2})[\.\-\/](\d{2,4})$/ ) {
        my $year = length($3) == 2 ? "20". $3 : $3;
        return sprintf "%04d-%02d-%02d", $year, $2, $1 if $ddmmyyyy;
        return sprintf "%04d-%02d-%02d", $year, $1, $2 if $mmddyyyy;
    }

    $self->fail("Unable to determine dates from: '$date'.");
}

sub serviceID { Client::Service::DSP_DEEZER }

sub currencyCode { 'EUR' }

sub _headerIdentifier { ( upc => 'UPC' ) }

sub isrc {
    Common::Util::normalize_isrc( shift->SUPER::isrc );
}

sub units {
    my $self  = shift;
    my $units = $self->SUPER::units() || 0;
    my $price = $self->SUPER::price();

    $units =~ s/\.0//g;
    $units = 1 unless $units;

    # The 'sign' of the units is driven by the price.
    # If the price is negative, we'll store negative units,
    # regardless of whether the units were positive or negative
    # in the sale file.  Accordingly, the price is always
    # positive.  This ensures that returns are recorded correctly.
    #
    if ( defined($price) ) {
        return $price < 0 ? abs($units) * -1 : $units;
    } else {
        return $units;
    }
}

sub unitPrice {
    return abs( shift->SUPER::unitPrice() );
}
###
1;    # Play nicely.
###
