package RPS::Import::Merlin::v84;

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use RPS::Import::ServiceMap;
use parent qw/RPS::Import::MapFaceBase RPS::Import::Importer/;

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        84 => {
            serviceID   => 'A',
            countryCode => 'L',
            dateBegin   => 'C',
            dateEnd     => 'D',
            productType => 'I',
            formatType  => 'M',
            artistName  => 'G',
            albumName   => 'H',
            isrc        => 'F',
            trackName   => 'I',
            units       => 'M',
            price       => 'N',
        },
    );

    return $fieldMap{ $self->_version };
}

sub _unverifiedFieldMap { {} }

sub _headerIdentifier { isrc => 'song_isrc' }

sub currencyCode { 'USD' }
sub mediaType    { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub serviceID {
    my $self = shift;

    my $service = lc( $self->_getByFieldName('serviceID') || '' );
    my $serviceID = $self->getServiceID($service) || $RPS::Import::ServiceMap::SERVICE_ALIASES{$service};
    return $serviceID if $serviceID;

    return $self->handleError( "Unknown service: '$service'", RPS::DB::Item::SaleImportError::kErrorService );
}

sub countryCode {
    my $self = shift;

    my $country = $self->_getByFieldName('countryCode') || '';
    my $oCountry = Common::Country->Get($country);
    return $oCountry->alpha2 if $oCountry;

    $self->handleError( "Unknown country: '$country'", RPS::DB::Item::SaleImportError::kErrorCountry );
    return $country;
}

sub dateBegin {
    my $self = shift;

    my $dateBegin = $self->_getByFieldName('dateBegin') || '';
    if ( $dateBegin =~ /(\d{4})(\d{2})(\d{2})/ ) {
        return sprintf("%04d-%02d-%02d", $1, $2, $3);
    }

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

sub dateEnd {
    my $self = shift;

    my $dateEnd = $self->_getByFieldName('dateEnd') || '';
    if ( $dateEnd =~ /(\d{4})(\d{2})(\d{2})/ ) {
        return sprintf("%04d-%02d-%02d", $1, $2, $3);
    }

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

sub productType {
    my $self = shift;

    my $type = $self->_getByFieldName('productType') || '';
    return RPS::File::Sale::TYPE_TRACK if $type;

    $self->fail("Unable to determine product type from: '$type'");
}

sub formatType {
    my $self = shift;

    my $type = $self->_getByFieldName('formatType') || '';
    return RPS::File::Sale::FORMAT_STREAM if $type;

    $self->fail("Unable to determine format type from: '$type'");
}

sub price     { abs( shift->_getByFieldName('price') || 0 ) }
sub unitPrice { abs shift->SUPER::unitPrice }
sub units {
    my $self = shift;

    my $units = $self->_getByFieldName('units') || 0;
    my $price = $self->_getByFieldName('price') || 0;

    if ( $price > 0 ) {
        $units = $units ? abs($units) : 1;
    } elsif ( $price < 0 ) {
       $units = $units ? -1 * abs($units) : -1;
    }

    return $units;
}

# mapFace

sub _mapFaceServiceID   { 'serviceID' }
sub _mapFaceCountryCode { 'countryCode' }
sub _mapFaceProductType { }
sub _mapFaceFormatType  { }
sub _mapFaceMediaType   { }


1;
