#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# 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 $sdate = $self->_getByFieldName('dateBegin');
    my $edate = $self->_getByFieldName('dateEnd');

    my $dateBegin;
    my $dateEnd;

    # Check for euro dates
    #
    if( $self->_isEuroDate($edate) )
    {
        ($dateBegin, $dateEnd) = $self->_getDates( date_begin => $sdate, type => 'eur' );
    }
    else
    {
        ($dateBegin, $dateEnd) = $self->_getDates( date_begin => $sdate );
    }

    return($dateBegin, $dateEnd);
}

sub _isEuroDate {
    my( $self, $date ) = @_;

    # Check for dates in the form DD/MM/YYYY.
    #

    # If a date has the form a/b/c, if the 'a' part is greater
    # than 13, then we assume that the date is DD/MM/YYYY.
    # Otherwise we assume the date is MM/DD/YYYY.
    #

    if ( $date =~ m/(\d{2})[\/-](\d{2})[\/-](\d{4})/ )  # MM/DD/YYYY, MM-DD-YYYY, DD/MM/YYYY or DD-MM-YYYY
    {
        my($m, $d, $y) = ($1, $2, $3);
        return 1 if ( $m > 13 );
    }
    #my($m, $d, $y) = split("/", $date) if ( $date =~ m/\d{2}\/\d{2}\/\d{4}/ );
    return 0;
}

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() || return;
    my $price = $self->SUPER::price();

    # 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.
###
