package BookPub::Import::Importer::VitalSource::Version20;

use strict;
use warnings;
use POSIX;

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

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

sub _headerIdentifier { rIsbn13 => 'EISBN' }

sub _unverifiedFieldMap {
    {
        altRIsbn13      => 'T',
        altRInstitution => 'I',
        exchangeRate    => 'AO',
        # see RSD-7899
        altrServiceName => 'A',
    };
}

# see RSD-7899
sub rServiceName {
    my $self = shift;

    my $rServiceName    = $self->_getByFieldName('rServiceName')    || '';
    my $altrServiceName = $self->_getByFieldName('altrServiceName') || '';

    # Trade
    if ( $self->filename =~ /_BNED(?:_eCom)?\.\w+$/i && $self->filename =~ /Trade/i ) {
        if ( $altrServiceName =~ /^BNED/i ) {
            return 'Barnes & Noble Education';
        }
    }

    # Higher Education
    if ( $self->filename =~ /_BNED\.\w+$/i && $self->filename =~ /Higher.Education/i ) {
        return $rServiceName =~ /^Barnes &(?:amp;)? Noble Education/i
            ? 'Barnes & Noble Education'
            : 'Vital Source';
    }

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

    # RSD-8789
    if ( $self->filename =~ /_BNED(?:_eCom)?\.\w{3,4}$/i ) {
        return 'Barnes & Noble Education';
    }

    $self->fail("Unable to determine rServiceName.");
}

sub rModel {
    my $self = shift;

    my $model = $self->_getByFieldName('rModel') || "";

    $self->fail("CA Inclusive Access sales found. ") if ($model =~ /Inclusive Access/i && $self->countryCode =~ /^canada/i);

    return $model;
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}


1;
