package RPS::Import::Amazon2;

use strict;

use lib '/app/tools/common/lib';
use Common::Util qw(normalize_isrc);

use lib '/app/tools/rps/lib';
use RPS::File::Sale;

use base 'RPS::Import::Importer';

sub _fieldMap {
    {
        dateBegin    => 'C',
        dateEnd      => 'D',
        upc          => 'J',
        isrc         => 'K',
        artistName   => 'N',
        labelName    => 'O',
        albumName    => 'L',
        trackName    => 'M',
        countryCode  => 'B',
        price        => 'S',
        productType  => 'G',
        currencyCode => 'R',
        units        => 'P',
    };
}

sub _unverifiedFieldMap {
    {
        albumASIN => 'H',
        trackASIN => 'I',
    };
}

sub _headerIdentifier { ( upc => qr/(ALBUM_VENDOR|ALBM_VENDR)_PRODUCT_IDENTIFIER/i ) }

sub formatType { RPS::File::Sale::FORMAT_DOWNLOAD }
sub mediaType  { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub serviceProductID {
    my $self = shift;

    if ( $self->productType eq 'A' ) {
        return $self->_getByFieldName('albumASIN');
    } elsif ( $self->productType eq 'T' ) {
        return $self->_getByFieldName('trackASIN');
    }
}

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('productType');
    if ( $type =~ /^a$/i ) {
        return RPS::File::Sale::TYPE_ALBUM;
    } elsif ( $type =~ /^t$/i ) {
        return RPS::File::Sale::TYPE_TRACK,;
    }
    $self->fail("Unrecognized product type: $type");
}

sub isrc {
    Common::Util::normalize_isrc( shift->SUPER::isrc );
}

sub units {
    my $self = shift;

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

    return ( $price < 0 ) ? abs($units) * -1 : abs($units);
}

sub unitPrice {
    return abs( shift->SUPER::unitPrice() );
}

###
1;    #
###
