package BookPub::Import::Importer::VitalSource::Version17;

use strict;
use warnings;
use POSIX;

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

sub _fieldMap {
    {
        rServiceName         => 'E',
        dateBegin            => 'B',
        dateEnd              => 'B',
        purchaseType         => 'W',
        rProductType         => 'V',
        rAuthor              => 'U',
        rTitle               => 'T',
        rIsbn13              => 'R',
        rInstitution         => 'G',
        rState               => 'I',
        rPostalCode          => 'J',
        countryCode          => 'K',
        serviceProductID     => 'Q',
        rUnits               => 'AA',
        rUnitPrice           => 'AG',
        rListPrice           => 'AD',
        rListPriceCurrency   => 'AB',
        rDiscount            => 'AF',
        rRevenue             => 'AH',
        currencyCode         => 'AI',
        rDuration            => 'W',
        rDeliveryMethod      => 'F',
        rPromoCode           => 'X',
        rChannel             => 'E',
        rModel               => 'C',
        rTransactionCategory => 'D',
    };
}

sub _headerIdentifier { rIsbn13 => 'EISBN' }

sub _unverifiedFieldMap {
    {
        altRIsbn13      => 'P',
        altRInstitution => 'E',
        exchangeRate    => 'AJ',
    };
}

sub _useIsSaleRec { 1 }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType  { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rPriceType    { shift->priceType }

my %months = (
    jan => 1,
    feb => 2,
    mar => 3,
    apr => 4,
    may => 5,
    jun => 6,
    jul => 7,
    aug => 8,
    sep => 9,
    oct => 10,
    nov => 11,
    dec => 12,
);

sub _getDateBegin {
    my $self   = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';

    # 12-APR-18
    if ( $date =~ m/^(\d{1,2})-(\w{3})-(\d{2})$/ ) {
        return sprintf( "%04d-%02d-%02d", "20$3", $months{ lc $2 }, $1 );
    } elsif ( $date =~ /^(\d{4})[-\.](\d{2})[-\.](\d{2})$/ ) {
        return sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    }

    $self->fail("Unrecognized date: '$date'.");
}

sub rServiceName {
    my $self = shift;

    my $rServiceName = $self->_getByFieldName('rServiceName') || '';
    return $self->filename =~ /_BNED\.\w+$/i && $rServiceName =~ /^Barnes & Noble Education/i
        ? 'Barnes & Noble Education'
        : 'Vital Source';
}

sub purchaseType {
    return $_[0]->_getByFieldName('rDuration')
        ? BookPub::DB::Item::Sale::kPurchaseTypeLoan
        : BookPub::DB::Item::Sale::kPurchaseTypeDownload;
}

sub priceType {
    return Common::RSApp::GetClientID =~ /^318$/ ? 'agency' : 'rrp';
}

sub rIsbn13 {
    return $_[0]->_getByFieldName('rIsbn13') || $_[0]->_getByFieldName('altRIsbn13');
}

sub rInstitution {
    return $_[0]->_getByFieldName('rInstitution') || $_[0]->_getByFieldName('altRInstitution');
}

sub rUnits {
    my $self = shift;

    my $rUnits = $self->_getByFieldName('rUnits') || 0;
    return sprintf "%.0f", $rUnits;
}

sub rListPrice {
    my $self = shift;

    my $rListPrice          = $self->_getByFieldName('rListPrice')         || 0;
    my $rListPriceCurrency  = $self->_getByFieldName('rListPriceCurrency') || '';
    my $currencyCode        = $self->_getByFieldName('currencyCode')       || '';
    my $exchangeRate        = $self->_getByFieldName('exchangeRate')       || 1;
    my $baseCurrency        = Common::Client::Current->Locale->currencyFormat->currencyCode();

    $rListPrice *= $exchangeRate if (
        $self->listPriceRequired
        && $rListPrice
        && $rListPriceCurrency ne $currencyCode
        && $rListPriceCurrency ne $baseCurrency
    );

    return $rListPrice;
}

sub rListPriceCurrency {
    my $self = shift;

    my $rListPriceCurrency = $self->_getByFieldName('rListPriceCurrency') || '';
    my $currencyCode       = $self->_getByFieldName('currencyCode')       || '';
    my $baseCurrency       = Common::Client::Current->Locale->currencyFormat->currencyCode();

    $rListPriceCurrency = $currencyCode if (
           $self->listPriceRequired
        && $rListPriceCurrency ne $currencyCode
        && $rListPriceCurrency ne $baseCurrency
    );

    return $rListPriceCurrency;
}


1;