package BookPub::Import::Importer::EBSCO2;

use strict;
use Data::Dumper;

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

sub _headerIdentifier { ( dateBegin => 'TRANS DATE' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        3 => {
            dateBegin            => 'C',
            rPurchaseType        => 'K',
            rAuthor              => 'G',
            rTitle               => 'F',
            rIsbn13              => 'E',
            rImprint             => 'A',
            rInstitution         => 'H',
            countryCode          => 'I',
            rUnits               => 'N',
            rListPrice           => 'M',
            rListPriceCurrency   => 'K10',
            rDiscount            => 'S',
            rRevenue             => 'T',
            currencyCode         => 'K10',
            rDuration            => 'L',
            rModel               => 'L',
            rTransactionCategory => 'K',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub priceType                  { 'rrp' }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _isSaleRec {
    my $self = shift;

    # If we've set the end of data flag in processLine, we are done.
    return if ( $self->{_endOfData} );

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

sub _processLine {
    my $self     = shift;
    my $discount = $self->_getByFieldName('rDiscount');

    if ( $discount && $discount =~ /Total/i ) {
        $self->{_endOfData} = 1;
    }

    return $self->SUPER::_processLine(@_);
}

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

    if ( lc($purchaseType) eq 'pdl' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } elsif ( lc($purchaseType) eq 'purchase' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } elsif ( lc($purchaseType) eq 'pdo' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } elsif ( lc($purchaseType) eq 'upgrade' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } elsif ( lc($purchaseType) eq 'adj' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } elsif ( lc($purchaseType) eq 'cancellation' ) {
        my $model = $self->_getByFieldName('rModel');
        if ( $model =~ /Day/ ) {
            return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
        } elsif ( $model =~ /1B\wU/ ) {
            return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
        } elsif ( $model =~ /PDL\d+/ ) {
            return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
        } else {
            $self->fail("Unknown model '$model'");
        }
    } elsif ($purchaseType) {
        $self->fail("Unknown purchase type '$purchaseType'");
    }
}

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

    if ( $duration =~ /^PDL(.*)$/ ) {
        $duration = $1;
    }

    return $duration;
}

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

    $units = sprintf( "%d", $units );

    return $units;
}

###
1;    # Play nicely.
###
