package RPS::Import::Napster::v9;

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use lib '/app/tools/common/lib';
use Date::Calc qw(Days_in_Month);

use parent 'RPS::Import::Importer';

sub _fieldMap {
    {
        labelName       => 'A',
        countryCode     => 'C',
        dateBegin       => 'O',
        dateEnd         => 'P',
        clientProductID => 'L',
        artistName      => 'D',
        upc             => 'G',
        albumName       => 'E',
        isrc            => 'J',
        trackName       => 'F',
        units           => 'K',
        currencyCode    => 'T',
        price           => 'S',
    };
}

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

sub serviceID   { Client::Service::DSP_NAPSTER }
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')   || '';

    if ( my ($year, $month) = $dateBegin =~ /^(\d{4})[-.\/]?(\d{2})[-.\/]?(\d{2})?$/ ) {
        $dateBegin = sprintf "%04d-%02d-%02d", $year, $month, 1;
    }

    if ( my ($year, $month) = $dateEnd =~ /^(\d{4})[-.\/]?(\d{2})[-.\/]?(\d{2})?$/ ) {
        $dateEnd = sprintf "%04d-%02d-%02d", $year, $month, Days_in_Month( $year, $month );
    }

    return $dateBegin, $dateEnd;
}



1;