package RPS::Import::INGrooves::Nettwerk2;

use strict;

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

sub _fieldMap {
    {
        labelName        => 'N',
        serviceID        => 'B',
        countryCode      => 'L',
        dateBegin        => 'C',
        dateEnd          => 'C',
        productType      => 'Q',    # PRODUCT TYPE
        formatType       => 'P',    # ASSET TYPE
        mediaType        => 'O',    # SALES TYPE
        artistName       => 'D',
        serviceProductID => 'K',    # CATALOG
        upc              => 'F',
        albumName        => 'E',
        isrc             => 'I',
        trackName        => 'G',
        units            => 'R',
        price            => 'T',
    };
}

sub _headerIdentifier { ( artistName => 'ARTIST' ) }

my %months = (
    jan => 1,
    feb => 2,
    mar => 3,
    apr => 4,
    may => 5,
    jun => 6,
    jul => 7,
    aug => 8,
    sep => 9,
    oct => 10,
    nov => 11,
    dec => 12
);

sub _processDates {
    my $self   = shift;
    my $period = $self->_getByFieldName('dateBegin');
    my ( $begin_y, $begin_m, $begin_d, $end_y, $end_m, $end_d );

    # this period looks like this: '2013-Apr-01 - 2013-Apr-30'
    $period =~ m/^(\d{4})-(\w{3})-(\d{1,2}) - (\d{4})-(\w{3})-(\d{1,2})$/i;
    $begin_y = $1;
    $begin_m = $2;
    $begin_d = $3;
    $end_y   = $4;
    $end_m   = $5;
    $end_d   = $6;

    return $begin_y, $begin_m, $begin_d, $end_y, $end_m, $end_d;
}

sub _getDateBegin {
    my $self = shift;
    my ( $begin_y, $begin_m, $begin_d, $end_y, $end_m, $end_d ) = $self->_processDates();

    my $startDate = sprintf( "%04s-%02s-%02s", $begin_y, $months{ lc($begin_m) }, $begin_d );

    return $startDate;
}

sub _getDateEnd {
    my $self = shift;
    my ( $begin_y, $begin_m, $begin_d, $end_y, $end_m, $end_d ) = $self->_processDates();

    my $endDate = sprintf( "%04s-%02s-%02s", $end_y, $months{ lc($end_m) }, $end_d );

    return $endDate;
}

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