package RPS::Import::AudibleMagic::v1;

use strict;
use warnings;

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

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


sub _headerIdentifier { upc => 'Album Upc' }

sub _fieldMap {
    {
        labelName   => 'I',
        serviceID   => 'M',
        countryCode => 'P',
        productType => 'B',
        formatType  => 'H',
        artistName  => 'E',
        upc         => 'C',
        albumName   => 'D',
        isrc        => 'F',
        trackNum    => 'J',
        trackName   => 'B',
        units       => 'G',
        free        => 'N',
    };
}

sub currencyCode { 'USD' }

sub serviceID {
    my $self = shift;

    my $service = lc( $self->_getByFieldName('serviceID') || '' );

    if ( $service =~ /^Securus/i ) {
        return Client::Service::DSP_SECURUS;
    }

    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 $name = lc( $self->_getByFieldName('countryCode') || '' );

    my $oCountry = Common::Country->Get($name);
    return $oCountry->alpha2() if $oCountry;

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

sub saleDates {
    my $self = shift;

    # YYYY-MM-DD_YYYY-MM-DD
    if ( $self->filename =~ /(\d{4}-\d{2}-\d{2})[_ ](\d{4}-\d{2}-\d{2})/ ) {
        return $self->_getDates( date_begin => $1, date_end => $2 );
    }

    $self->fail("Unable to extract sale dates from the filenme.");
}

sub productType {
    my $self = shift;

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

    $self->handleError( "Unknown product type: $type", RPS::DB::Item::SaleImportError::kErrorProductType );
}

sub formatType {
    my $self = shift;

    my $type = $self->_getByFieldName('formatType') || '';

    if ( $type =~ /^Streams MP3$/i ) {
        return RPS::File::Sale::FORMAT_STREAM;
    } elsif ( $type =~ /^(?:Device )?Play Counts$/i ) {
        return RPS::File::Sale::FORMAT_TETHERED;
    }

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

sub mediaType {
    return RPS::DB::Item::MediaType::kMediaTypeAudio;
}

sub free {
    my $self = shift;

    my $flag = $self->_getByFieldName('free') || '';
    return $flag =~ /^TRIAL$/i ? 1 : 0;
}

sub _isValidSaleRecord {
    my $self = shift;

    return $self->_getByFieldName('price') || $self->_getByFieldName('units');

    return 0;
}

sub price { 0 }

sub units {
    my $self = shift;

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

    return $units;
}

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


1;
