package BookPub::Import::Importer::VitalSource::Version19;

use strict;
use warnings;
use POSIX;

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

sub _fieldMap {
    {
        rServiceName             => 'H',
        dateBegin                => 'B',
        dateEnd                  => 'B',
        rPurchaseType            => 'AF',
        purchaseType             => 'Z',
        rProductType             => 'Y',
        rAuthor                  => 'X',
        rTitle                   => 'W',
        rIsbn13                  => 'U',
        rInstitution             => 'J',
        rState                   => 'L',
        rPostalCode              => 'M',
        countryCode              => 'N',
        rClientProductID         => 'V',
        serviceProductID         => 'T',
        rUnits                   => 'AD',
        rUnitPrice               => 'AJ',
        rListPrice               => 'AG',
        rListPriceCurrency       => 'AE',
        rNativeListPrice         => 'AG',
        rNativeListPriceCurrency => 'AE',
        rDiscount                => 'AI',
        rRevenue                 => 'AK',
        currencyCode             => 'AL',
        rDuration                => 'Z',
        rDeliveryMethod          => 'I',
        rPromoCode               => 'AA',
        rChannel                 => 'H',
        rModel                   => 'E',
        rTransactionCategory     => 'C'
    };
}

sub _headerIdentifier { rIsbn13 => 'EISBN' }

sub _unverifiedFieldMap {
    {
        altRIsbn13      => 'S',
        altRInstitution => 'H',
        exchangeRate    => 'AM',
    };
}

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;