package RPS::Import::Merlin::v49;

use strict;
use warnings;

use lib '/app/tools/rps/lib';
use RPS::Import::ServiceMap;

use parent qw/RPS::Import::MapFaceBase RPS::Import::ImporterMixed/;

sub physical { 2 }

sub _fieldMapExtended {
    {
        digital => {
            option1 => {
                labelName    => 'E',
                serviceID    => 'R',
                countryCode  => 'L',
                dateBegin    => 'A',
                dateEnd      => 'B',
                productType  => 'K',
                formatType   => 'P',
                artistName   => 'I',
                upc          => 'F',
                albumName    => 'H',
                isrc         => 'K',
                trackName    => 'J',
                units        => 'M',
                currencyCode => 'O',
                price        => 'N',
            },
         },
    };
}

sub _unverifiedFieldMapExtended { {} };

sub _headerIdentifierExtended {
    {
        digital => {
            option1 => {
                productType => 'ISRC',
            },
        },
    };
}

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};

    $self->fail("Unable to determine service id from '$service'") unless $serviceID;

    return $serviceID;
}

sub countryCode {
    my $self = shift;

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

    return $oCountry->alpha2 if $oCountry;
}

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') || '';
    if ( $type =~ /^tier-2-background-music-tier-with-caching$/i ) {
        return RPS::File::Sale::FORMAT_BACKGROUNDMUSIC;
    } elsif ( $type =~ /^tier-3a-fully-interactive-tier-with-caching$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    }

    $self->handleError( "Unknown format_type: '$type'.", RPS::DB::Item::SaleImportError::kErrorFormatType );
}

sub units {
    my $self = shift;

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

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

    return $units;
}

sub _isValidSaleRecord {
    my $self = shift;

    return defined $self->units && defined $self->_getByFieldName('price')
        && ( $self->_getByFieldName('albumName') || $self->_getByFieldName('trackName') );
}

# mapFace
sub _mapFaceServiceID   { }
sub _mapFaceCountryCode { }
sub _mapFaceProductType { }
sub _mapFaceFormatType  { 'formatType' }
sub _mapFaceMediaType   { }


1;
