package BookPub::Import::Importer::VitalSource::Version14;

use strict;

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

sub _fieldMap {
    {
        dateBegin            => 'B',
        purchaseType         => 'Q',
        rAuthor              => 'O',
        rTitle               => 'N',
        rIsbn13              => 'L',
        rInstitution         => 'F',
        rPostalCode          => 'G',
        countryCode          => 'H',
        rClientProductID     => 'M',
        serviceProductID     => 'K',
        rUnits               => 'S',
        rUnitPrice           => 'X',
        rListPrice           => 'T',
        rListPriceCurrency   => 'V',
        rDiscount            => 'W',
        rRevenue             => 'Y',
        currencyCode         => 'Z',
        rDuration            => 'Q',
        rDeliveryMethod      => 'E',
        rTransactionCategory => 'C',
    };
}

sub _unverifiedFieldMap {
    {
        altInstitution => 'D',
        conversion     => 'AA',
    };
}

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

sub _useIsSaleRec { 1 }
sub _dateFormat   { [ 'text', 'iso' ] }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType  { BookPub::DB::Item::Sale::kProductTypeEBook }
sub priceType     { 'rrp' }
sub rPriceType    { 'rrp' }
sub rServiceName  { 'Vital Source' }

sub _getDateBegin {
    my $self   = shift;
    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,
    );
    my $date = $self->_getByFieldName('dateBegin');

    # 12-APR-18
    if ( $date !~ m/^(\d{1,2})-(\w{3})-(\d\d)/ ) {
        $self->fail("Unrecognized date: '$date'");
    }
    my $year  = "20" . $3;
    my $month = $months{ lc($2) };
    my $day   = $1;

    $date = sprintf( "%04d-%02d-%02d", $year, $month, $day );

    return $date;
}

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

    if ( !$type ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } else {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    }
}

sub rInstitution {
    my $self    = shift;
    my $inst    = $self->_getByFieldName('rInstitution');
    my $altInst = $self->_getByFieldName('altInstitution');

    if ($inst) {
        return $inst;
    } else {
        return $altInst;
    }
}

sub rListPrice {
    my $self           = shift;
    my $price          = $self->_getByFieldName('rListPrice');
    my $conversionRate = $self->_getByFieldName('conversion');

    if ( $self->listPriceRequired ) {
        $price *= $conversionRate;
    }

    return $price;
}

sub rListPriceCurrency {
    my $self            = shift;
    my $priceCurrency   = $self->_getByFieldName('rListPriceCurrency');
    my $revenueCurrency = $self->_getByFieldName('currencyCode');

    if ( $self->listPriceRequired ) {
        return $revenueCurrency;
    }

    return $priceCurrency;
}

1;

