package BookPub::Import::Importer::Amazon::Version29;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( currencyCode => 'payment_amount_currency' ) }

sub _fieldMap {
    {
        serviceProductID   => 'G',
        currencyCode       => 'BD',
        countryCode        => 'Y',
        rState             => 'AB',
        rPostalCode        => 'X',
        dateBegin          => 'D',
        rIsbn13            => 'F',
        rTitle             => 'H',
        rAuthor            => 'I',
        rUnits             => 'K',
        rListPrice         => 'M',
        rListPriceCurrency => 'N',
        rPurchasePrice     => 'O',
        rRevenue           => 'BC',
    };
}

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

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

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

    if ( $currencyCode =~ /(\w{3})/ ) {
        $currencyCode = $1;
    }

    # If there's no revenue, they aren't giving us a currency code.
    # Should be safe to default to the list price currency.
    if ( !$currencyCode && $self->SUPER::revenue() == 0 ) {
        $currencyCode = $self->_getByFieldName('rListPriceCurrency');
    }

    return $currencyCode;
}

sub rUnits {
    my $self            = shift;
    my $units           = $self->_getByFieldName('rUnits');
    my $transactionType = $self->_getByFieldName('transactionType');

    if ( uc($transactionType) eq 'REFUND' && $units > 0 ) {
        $units *= -1;
    }

    return $units;
}

sub rRevenue {
    my $self            = shift;
    my $revenue         = $self->_getByFieldName('rRevenue') || 0;
    my $transactionType = $self->_getByFieldName('transactionType');

    if ( uc($transactionType) eq 'REFUND' && $revenue > 0 ) {
        $revenue *= -1;
    }

    return $revenue;
}

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

    my %monthAbbrToNumber;
    $monthAbbrToNumber{"JAN"} = 1;
    $monthAbbrToNumber{"FEB"} = 2;
    $monthAbbrToNumber{"MAR"} = 3;
    $monthAbbrToNumber{"APR"} = 4;
    $monthAbbrToNumber{"MAY"} = 5;
    $monthAbbrToNumber{"JUN"} = 6;
    $monthAbbrToNumber{"JUL"} = 7;
    $monthAbbrToNumber{"AUG"} = 8;
    $monthAbbrToNumber{"SEP"} = 9;
    $monthAbbrToNumber{"OCT"} = 10;
    $monthAbbrToNumber{"NOV"} = 11;
    $monthAbbrToNumber{"DEC"} = 12;

    # The date is being returned in this format: 16-DEC-2012 14:16:18
    # I don't even...
    #
    if ( $date =~ /(\d{2})-(\w{3})-(\d{4})/ ) {
        my $day       = $1;
        my $monthAbbr = uc($2);
        my $year      = $3;

        my $month = $monthAbbrToNumber{$monthAbbr};

        $date = $year . "-" . $month . "-" . $day;
    } elsif ( $date =~ /(\d{2})\/(\d{2})\/(\d{4})/ ) {

        # Great news! It's now coming in as: 31/03/2013 03:03:20 PM
        # So we'll deal with that now, too.
        $date = $3 . "-" . $2 . "-" . $1;

    } elsif ( $date =~ /(\w{3}) (\d{2}) \d{2}:\d{2}:\d{2} \w{3} (\d{4})/ ) {

        # Truly, they have outdone themselves.
        # New date format: Mon Jul 01 20:05:57 PDT 2013
        my $day       = $2;
        my $monthAbbr = uc($1);
        my $year      = $3;

        my $month = $monthAbbrToNumber{$monthAbbr};

        $date = $year . "-" . $month . "-" . $day;
    }

    return $date;
}

sub _isSaleRec {
    my $self = shift;

    my $transactionStatus = uc( $self->_getByFieldName('transactionStatus') );
    my $revenue           = $self->SUPER::revenue();

    # We're going to skip some of the lines if they have a certain status AND no revenue.
    if ( ( $transactionStatus eq "REFUNDED_NOT_RECEIVED" || $transactionStatus eq "FULFILLED_NOT_PAID" ) && $revenue == 0 ) {
        return 0;
    } else {
        return $self->SUPER::_isSaleRec();
    }
}

1;
