package BookPub::Import::Importer::Amazon::Version75;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin              => 'D',
        purchaseType           => 'BO',
        rAuthor                => 'I',
        rTitle                 => 'H',
        rIsbn13                => 'F',
        rState                 => 'AH',
        rPostalCode            => 'X',
        countryCode            => 'Y',
        serviceProductID       => 'G',
        rUnits                 => 'K',
        rListPrice             => 'M',
        rListPriceCurrency     => 'N',
        rPurchasePrice         => 'O',
        rPurchasePriceCurrency => 'P',
        rRevenue               => 'BK',
        currencyCode           => 'BL',
        rTaxAmount             => 'BM',
        isFree                 => 'L',
        rModel                 => 'BO',
        rTransactionCategory   => 'L',
    };
}

sub _unverifiedFieldMap {
    {
        transactionStatus => 'L',
        transactionType   => 'R',
    };
}

sub rState {
    my $self = shift;

    my $rState = $self->_getByFieldName('rState') || '';
    my $postal = Common::Postal->new('US');

    my $state = ( $postal->getName($rState) ? $rState : $postal->getAbbr($rState) ) || '';
    return $state if $state;

    return 'CA' if $rState =~ /(?:san jose|SACRAMENTO|calif|CA\.|California \(CA\))/i;
    return 'DC' if $rState =~ /D\.C\./i;
    return 'NY' if $rState =~ /(?:N\.Y\.|NEWYORK)/i;
    return 'NJ' if $rState =~ /(?:Tenafly NJ|Hudson)/i;
    return 'SC' if $rState =~ /(?:South Car[ol]+ina|S\.C\.)/i;
    return 'PA' if $rState =~ /Pennsylvania \(PA\)/i;
    return 'AZ' if $rState =~ /Arizona \(AZ\)/i;
    return 'MD' if $rState =~ /MD - Maryland/i;
    return 'KS' if $rState =~ /Kansas \(KS\)/i;
    return 'TX' if $rState =~ /Texas \(TX\)/i;
    return 'CO' if $rState =~ /co\/usa/i;
    return 'NC' if $rState =~ /N\.C\./i;
    return 'PR' if $rState =~ /P\.R\./i;
    return 'TN' if $rState =~ /(US-)?TN\.?/i;
    return 'VI' if $rState =~ /USVI/i;
    return 'NE' if $rState =~ /Ne\./i;
    return 'OK' if $rState =~ /stillwater/i;
    return 'MI' if $rState =~ /(Michigan \()?Mi\)?\.?/i;
    return 'GA' if $rState =~ /Georgia \(GA\)/i;
    return 'VA' if $rState =~ /Va\./i;
    return 'MA' if $rState =~ /Mass/i;
    return 'MO' if $rState =~ /MO - Missouri/i;

    return;
}

sub countryCode {
    my $self = shift;

    my $rState      = $self->_getByFieldName('rState')      || '';
    my $countryCode = $self->_getByFieldName('countryCode') || '';
    my $isUSState   = $self->rState ? 1 : 0;

    # trim state and country code
    $rState =~ s/^\s*|\s*$//g;
    $countryCode =~ s/^\s*|\s*$//g;

    # State belongs to the USA
    return 'US' if $isUSState;

    # Non-US state is reported
    if ( $rState && !$isUSState ) {
        $self->fail("The state '$rState' does not belong to the USA");
    }

    # State and CountryCode are blank
    if ( !$rState && !$countryCode ) {
        return 'US' if $self->purchaseType eq BookPub::DB::Item::Sale::kPurchaseTypeLoan;
        $self->fail("The state and country code is empty");
    }

    # State is blank but CountryCode is not blank
    if ( !$rState ) {
        return $countryCode if $countryCode =~ /^(?:US|AS|GU|MP|PR|VI|MH|FM|PW|UM)$/i;

        # for Wiley only (RSD-12137)
        if ( Common::RSApp::GetClientID == 324 && $countryCode =~ /^(?:AU|NZ)$/i ) {
            return $countryCode;
        }

        $self->fail("Non-US country code '$countryCode'");
    }

    # Any contingency
    $self->fail("Unable to determine country code from: '$countryCode' and state: '$rState'");
}

sub rServiceName { 'Amazon' }
sub rProductType { shift->productType() }

sub rTransactionCategory {
    return shift->_getByFieldName('transactionStatus');
}
sub rTaxAmount {
    my $self            = shift;
    my $revenue         = $self->_getByFieldName('rRevenue') || 0;
    my $tax             = $self->_getByFieldName('rTaxAmount') || 0;
    my $transactionType = $self->_getByFieldName('transactionType');
    if ( ( $transactionType =~ /REFUND/i && $tax > 0 ) || ( $transactionType !~ /REFUND/i && $revenue < 0 ) ) {
        $tax = abs($tax) * -1;
    }
     return $tax;
}

sub purchaseType {
    my $self = shift;

    my $type = $self->_getByFieldName('purchaseType') || '';
    if ( !$type || $type =~ /^Bulk$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    }

    if ( $type =~ /^Lending$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    }
}

sub isFree {
    my $self = shift;

    my $isFree = $self->_getByFieldName('isFree') || '';
    return 'Y' if ($isFree =~ /^(?:REFUNDED_NOT_RECEIVED|FULFILLED_NOT_PAID)$/i);

    return 'N';
}

1;
