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

package RPS::Import::BelieveDigital::v8;

use strict;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_isrc);

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

sub _fieldMap {
    {
        labelName       => 'F',
        serviceID       => 'C',
        countryCode     => 'D',
        dateBegin       => 'B',
        dateEnd         => 'B',
        formatType      => 'N',
        clientProductID => 'L',
        artistName      => 'G',
        upc             => 'J',
        albumName       => 'H',
        isrc            => 'K',
        trackName       => 'I',
        units           => 'P',
        currencyCode    => 'Q',
        price           => 'W',
    };
}

sub _unverifiedFieldMap {
    { offerType => 'O', };
}

sub countryCode {
    my $self = shift;
    my $code = $self->_getByFieldName('countryCode');

    if ( lc($code) eq 'world' ) {
        return 'US';
    }
    return $self->SUPER::countryCode();
}

sub saleDates {
    my $self = shift;

    my $dateBegin;
    my $dateEnd;

    my $startDate = $self->_getDateBegin();
    my $endDate   = $self->_getDateEnd();

    $startDate = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $startDate ) if ( $startDate && $startDate =~ m/^(\d+)$/ );
    $endDate   = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $endDate )   if ( $endDate   && $endDate =~ m/^(\d+)$/ );

    if ( $startDate && $endDate ) {
        ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $startDate, date_end => $endDate );
    }

    return ( $dateBegin, $dateEnd );
}

sub isrc {
    my $self = shift;
    my $isrc = $self->_getByFieldName('isrc');

    if ($isrc) {
        $isrc = Common::Util::normalize_isrc($isrc);
    }

    return $isrc;
}

sub price {
    my $self  = shift;
    my $price = $self->_getByFieldName('price');
    $price =~ s/,/./;    # replace euro-style decimal point with a "real" decimal point
    if ( $price < 0 ) {
        $price *= -1;
    }
    return $price;
}

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