package BookPub::Import::Importer::Readbooks;

use strict;

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

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

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            currencyCode       => 'L',
            countryCode        => 'M',
            rState             => 'N',
            rPostalCode        => 'P',
            dateBegin          => 'A',
            dateEnd            => 'B',
            rProductType       => 'D',
            rIsbn13            => 'C',
            rTitle             => 'J',
            rSales             => 'I',
            rReturns           => 'V',
            rListPrice         => 'E',
            rListPriceCurrency => 'F',
            rPurchasePrice     => 'G',
            rRevenue           => 'K',
        },
        2 => {
            currencyCode           => 'L',
            countryCode            => 'M',
            rState                 => 'N',
            rPostalCode            => 'P',
            dateBegin              => 'X',
            dateEnd                => 'X',
            rProductType           => 'D',
            rIsbn13                => 'C',
            rTitle                 => 'J',
            rUnits                 => 'I',
            rListPrice             => 'E',
            rListPriceCurrency     => 'F',
            rPurchasePrice         => 'G',
            rPurchasePriceCurrency => 'H',
            rTaxAmount             => 'U',
            rRevenue               => 'K',
        },
        3 => {
            dateBegin              => 'A',
            rTitle                 => 'K',
            rIsbn13                => 'J',
            rPostalCode            => 'F',
            countryCode            => 'I',
            rUnits                 => 'C',
            rListPrice             => 'L',
            rListPriceCurrency     => 'W',
            rPurchasePrice         => 'N',
            rPurchasePriceCurrency => 'W',
            rDiscount              => 'S',
            rRevenue               => 'T',
            currencyCode           => 'W',
            rTaxAmount             => 'P',
        },
        4 => {
            rTitle                 => 'E',
            rIsbn13                => 'D',
            countryCode            => 'C',
            rUnits                 => 'B',
            rListPrice             => 'F',
            rListPriceCurrency     => 'Q',
            rPurchasePrice         => 'H',
            rPurchasePriceCurrency => 'Q',
            rDiscount              => 'M',
            rRevenue               => 'N',
            currencyCode           => 'Q',
            rTaxAmount             => 'J',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            subtotal => 'A',
        },
        2 => {
            subtotal => 'A',
        },
    );

    return $fieldMap{ $self->_version() };
}

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

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

sub rSales {
    my $self    = shift;
    my $sales   = $self->_getByFieldName('rSales');
    my $returns = $self->_getByFieldName('rReturns');

    if ( $returns > 0 ) {
        $sales = 0;
    }

    return $sales;
}

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

    if ( defined $self->_getByFieldName('rReturns') ) {
        my $returns = $self->_getByFieldName('rReturns');

        if ( $returns > 0 ) {
            $revenue = abs($revenue) * -1;
        }
    }

    return $revenue;
}

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

    # date is in format YYYYMMDD
    if ( $dateStr =~ /^(\d{4})?-(\d{2})?-(\d{2})$/ ) {
        $date = sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    } else {
        $date = undef;
    }

    return $date;
}

sub dateEnd {
    my $self    = shift;
    my $dateStr = $self->_getByFieldName('dateEnd');
    my $date;

    # date is in format YYYYMMDD
    if ( $dateStr =~ /^(\d{4})?-(\d{2})?-(\d{2})$/ ) {
        $date = sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    } else {
        $date = undef;
    }

    return $date;
}

sub _isSaleRec {
    my $self = shift;

    my $subtotal = $self->_getByFieldName('subtotal');

    # We're going to skip some lines.
    if ( $subtotal =~ /REFUNDS/i || $subtotal =~ /TOTAL/i ) {
        return 0;
    }

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

1;
