package BookPub::Import::Importer::Amazon::Version22;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( rIsbn13 => 'eisbn' ) }

sub _fieldMap {
    {
        # Data
        rPriceType         => 'V',
        serviceProductID   => 'G',
        countryCode        => 'V',
        rState             => 'U',
        rPostalCode        => 'W',
        dateBegin          => 'A',
        rIsbn13            => 'E',
        rTitle             => 'F',
        rUnits             => 'H',
        rListPrice         => 'M',
        rListPriceCurrency => 'N',
        rPurchasePrice     => 'Z',
        rRevenue           => 'AE',
    };
}

sub _unverifiedFieldMap {
    { canadianConversionRate => 'AB', };
}

sub _useIsSaleRec { 1 }

sub currencyCode { 'USD' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _dateNormalization { 'month' }

sub priceType {
    my $self        = shift;
    my $countryCode = lc( $self->countryCode );
    if ( $countryCode eq 'canada' || $countryCode eq 'united states' ) {
        return 'agency';
    } else {
        return 'rrp';
    }
}

sub rListPriceCurrency {

    # We're going to convert all list prices to USD, so let's just set this to USD.
    return 'USD';
}

sub rListPrice {
    my $self      = shift;
    my $listPrice = $self->_getByFieldName('rListPrice');

    if ($listPrice) {
        my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency');

        if ( $listPriceCurrency ne 'USD' ) {
            my $conversionRate = $self->{_conversionRate};
            if ( !$conversionRate ) {
                $self->fail("Conversion Rate missing");
            }

            $listPrice /= $conversionRate;
        }
    }

    return $listPrice;
}

sub _processHeader { shift->_storeConversionRate() }

sub _storeConversionRate {
    my $self = shift;

    unless ( $self->{_conversionRate} ) {
        my $conversionRate = $self->_getByFieldName('canadianConversionRate');
        if ( $conversionRate =~ /Sale Amount Canadian \$ \((.+)\)/ ) {
            $self->{_conversionRate} = $1;
        }
    }
}

sub rDiscount {
    my $self      = shift;
    my $listPrice = $self->rListPrice;
    my $discount;

    if ($listPrice) {
        my $revenue = $self->rRevenue;
        my $units   = $self->rUnits;
        my $price   = $revenue / $units;

        $discount = 1 - ( $price / $listPrice );
    }

    return $discount;
}

sub rTitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $title;
}

sub rSubtitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $subtitle;
}

sub _isSaleRec {
    my $self = shift;

    # We're going to check for the subtotal line, which only contains
    # totals for units and revenue.
    if (   $self->rUnits
        && $self->rRevenue
        && !$self->rPriceType
        && !$self->serviceProductID
        && !$self->countryCode
        && !$self->rState
        && !$self->rPostalCode
        && !$self->dateBegin
        && !$self->rIsbn13
        && !$self->rTitle
        && !$self->listPrice
        && !$self->rPurchasePrice ) {
        return 0;
    }

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

1;
