package BookPub::Import::Importer::Amazon::Version20;

use strict;
use warnings;

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

# Same format as version 5, but agency instead of rrp.

# and we've decided to fully utilize sales, returns and adjustments
# sub _advancedUnitProcessing { 1 }
# I'm opposed to leaving "commented-out code" in place, the line above
# should be deleted.  But I'm leaving this here to note that we're
# no longer using aUP for versions 20, 52 and 53.
# Upstream changes to adjustment processing in version 5 introduced
# some discrepencies.

sub _fieldMap {
    my $self = shift;

    my $fieldMap = $self->SUPER::_fieldMap();

    $fieldMap->{rAdjustments}             = 'N';
    $fieldMap->{rChannel}                 = 'W';
    $fieldMap->{rNativeListPrice}         = 'Q';
    $fieldMap->{rNativeListPriceCurrency} = 'R';
    $fieldMap->{rPurchaseType}            = 'W';

    if ( $self->version == 52 ) {
        $fieldMap->{rRevenue}         = 'Y';
        $fieldMap->{rIncentiveAmount} = 'X';
    }
    if ( $self->version == 55 ) {
        $fieldMap->{rPurchasePriceCurrency} = 'P';
        $fieldMap->{rChannel}               = 'W';
        $fieldMap->{rIncentiveAmount}       = 'Y';
        $fieldMap->{rRevenue}               = 'Z';
    }
    if ( $self->version == 67 ) {
        $fieldMap->{rRevenue}         = 'AB';
        $fieldMap->{rIncentiveAmount} = 'AA';
        $fieldMap->{rModel}           = 'Y';
    }
    if ( $self->version == 73 ) {
        $fieldMap->{rIncentiveAmount} = 'X';
        $fieldMap->{rPurchaseType}    = undef;
        $fieldMap->{rChannel}         = undef;
    }
    if ( $self->version == 77 ) {
        $fieldMap->{rModel} = 'AB';
    }

    return $fieldMap;
}

sub priceType {
    my $self = shift;

    my $clientID = Common::RSApp::GetClientID();

    # MMUK
    if ( $clientID == 359 ) {
        return 'agencyplustax' if $self->filename =~ /MTCEU[ _]US/;
    }

    if ( $clientID == 355 ) {
        return 'agencyplustax';
    }

    return 'agency';
}

sub incentiveAmount {
    my $self = shift;

    my $incentiveAmount;

    if ( defined( $self->rIncentiveAmount ) ) {
        $incentiveAmount = -$self->rIncentiveAmount;
    }

    return $incentiveAmount;
}

sub purchaseType    {
    my $self = shift;

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

    return BookPub::DB::Item::Sale::kPurchaseTypeDownload if $self->version == 73;
    return BookPub::DB::Item::Sale::kPurchaseTypeLoan     if $type =~ /Lending/i;
    return BookPub::DB::Item::Sale::kPurchaseTypeDownload unless $type;

    $self->fail( "Could not determine purchase type from $type" );
}

1;
