package BookPub::Import::Importer::Amazon::Version7;

use strict;

use lib '/app/tools/bookpub/lib';
use base 'BookPub::Import::Importer';

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( rAuthor => 'Author' ) }

sub _fieldMap {
    {
        dateBegin          => 'A',
        serviceProductID   => 'B',
        currencyCode       => 'U',
        countryCode        => 'V',
        rFormat            => 'I',
        isbn               => 'E',
        rTitle             => 'F',
        rAuthor            => 'G',
        rImprint           => 'H',
        rUnits             => 'L',
        rSales             => 'J',
        rReturns           => 'K',
        rPurchasePrice     => 'O',
        rListPrice         => 'Q',
        rListPriceCurrency => 'R',
        rDiscount          => 'S',
        rRevenue           => 'T',
    };
}

sub priceType    { 'rrp' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin') || return;

    if ( $date && $date =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/ ) {
        my ( $year, $month, $day ) = Add_Delta_YM( $3, $2, $1, 0, -1 );

        return sprintf( "%04d-%02d-%02d", $year, $month, $day );
    }
}

sub _dateNormalization { 'month' }

sub rRevenue {
    my $self = shift;
    my $price = $self->_getByFieldName('rRevenue') || return;

    if ( $price =~ s/[\(\)]//g ) {
        $price *= -1;
    }

    return $price;
}

sub rListPrice {
    my $self = shift;
    my $price = $self->_getByFieldName('rListPrice') || return;

    if ( $price =~ s/[\(\)]//g ) {
        $price *= -1;
    }

    return $price;
}

sub _isValidSaleRecord {
    my $self = shift;

    return ( $self->rTitle || $self->rAuthor || $self->rIsbn13 ) && $self->dateBegin;
}

1;
