package BookPub::Import::Importer::Amazon::Version38;

use strict;

use lib '/app/tools/common/lib';
use Common::Country;

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

use Date::Calc qw( Add_Delta_YM );

sub _fieldMap {
    {
        serviceProductID       => 'B',
        currencyCode           => 'Y',
        countryCode            => 'Z',
        dateBegin              => 'A',
        rFormat                => 'I',
        isbn                   => 'E',
        rTitle                 => 'F',
        rAuthor                => 'G',
        rImprint               => 'H',
        rUnits                 => 'L',
        rSales                 => 'J',
        rReturns               => 'K',
        rAdjustments           => 'N',
        rListPrice             => 'O',
        rListPriceCurrency     => 'P',
        rPurchasePrice         => 'S',
        rPurchasePriceCurrency => 'T',
        rRevenue               => 'X',
    };
}

sub _headerIdentifier { ( serviceProductID => 'ASIN' ) }
sub _dateNormalization { 'month' }

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType                  { 'agency' }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }

sub units {

    # We need to make sure that the sign of the units matches the sign of the revenue
    my $self    = shift;
    my $units   = $self->_getByFieldName('rUnits');
    my $revenue = $self->rRevenue;

    if ( ( $revenue < 0 && $units > 0 ) || ( $revenue > 0 && $units < 0 ) ) {
        $units *= -1;
    }

    return $units;
}

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

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

    return $price;
}

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 _isSaleRec {
    my $self = shift;

    # If we've set the end of data flag in processLine, we are done.
    return if ( $self->{_endOfData} );

    return $self->SUPER::_isSaleRec();
}

sub _processLine {
    my $self = shift;
    my $id   = $self->serviceProductID();

    if ( $id && lc($id) eq 'invoice number' ) {
        $self->{_endOfData} = 1;
    }

    return $self->SUPER::_processLine(@_);
}

sub rTaxUnitPrice {
    my $self = shift;

    my $purchase = $self->_getByFieldName('rPurchasePrice');
    my $list     = $self->_getByFieldName('rListPrice');

    my $listPriceCurrency     = $self->_getByFieldName('rListPriceCurrency');
    my $purchasePriceCurrency = $self->_getByFieldName('rPurchasePriceCurrency');

    # If the two prices are in different currencies, we cannot calculate the tax amount.
    if ( lc($listPriceCurrency) ne lc($purchasePriceCurrency) ) {
        return;
    }

    my $tax = $purchase - $list;

    return $tax;
}

1;
