package BookPub::Import::Importer::VitalSource::Version25;

use strict;
use warnings;
use POSIX;

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

sub _fieldMap {
    my $self = shift;

    my %map = (
        25 => {
            dateBegin                => 'B',
            dateEnd                  => 'B',
            rPurchaseType            => 'AJ',
            purchaseType             => 'AD',
            rProductType             => 'AC',
            rAuthor                  => 'AB',
            rTitle                   => 'AA',
            rIsbn13                  => 'W',
            rInstitution             => 'K',
            rState                   => 'M',
            rPostalCode              => 'N',
            countryCode              => 'O',
            rClientProductID         => 'X',
            serviceProductID         => 'V',
            rUnits                   => 'AH',
            rUnitPrice               => 'AO',
            rListPrice               => 'AK',
            rListPriceCurrency       => 'AI',
            rNativeListPrice         => 'AK',
            rNativeListPriceCurrency => 'AI',
            rDiscount                => 'AM',
            rRevenue                 => 'AP',
            currencyCode             => 'AQ',
            rFee                     => 'AN',
            rDuration                => 'AD',
            rDeliveryMethod          => 'J',
            rPromoCode               => 'AE',
            rChannel                 => 'I',
            rModel                   => 'E',
            rTransactionCategory     => 'C',
            rComments                => 'A',
        },
        26 => {
            dateBegin                => 'B',
            dateEnd                  => 'B',
            rPurchaseType            => 'AI',
            purchaseType             => 'AE',
            rProductType             => 'AD',
            rAuthor                  => 'AC',
            rTitle                   => 'AB',
            rIsbn13                  => 'X',
            rInstitution             => 'L',
            rState                   => 'N',
            rPostalCode              => 'O',
            countryCode              => 'P',
            rClientProductID         => 'Y',
            serviceProductID         => 'W',
            rUnits                   => 'AJ',
            rUnitPrice               => 'AO',
            rListPrice               => 'AK',
            rListPriceCurrency       => 'AM',
            rNativeListPrice         => 'AK',
            rNativeListPriceCurrency => 'AM',
            rDiscount                => 'AS',
            rRevenue                 => 'AQ',
            currencyCode             => 'AR',
            rFee                     => 'AN',
            rDuration                => 'AE',
            rDeliveryMethod          => 'K',
            rPromoCode               => 'AF',
            rChannel                 => 'J',
            rModel                   => 'F',
            rTransactionCategory     => 'C',
            rComments                => 'A',
        },
        27 => {
            dateBegin                => 'B',
            dateEnd                  => 'B',
            rPurchaseType            => 'AI',
            purchaseType             => 'AE',
            rProductType             => 'AD',
            rAuthor                  => 'AC',
            rTitle                   => 'AB',
            rIsbn13                  => 'X',
            rInstitution             => 'L',
            rState                   => 'N',
            rPostalCode              => 'O',
            countryCode              => 'P',
            rClientProductID         => 'Y',
            serviceProductID         => 'W',
            rUnits                   => 'AJ',
            rUnitPrice               => 'AP',
            rListPrice               => 'AK',
            rListPriceCurrency       => 'AM',
            rNativeListPrice         => 'AK',
            rNativeListPriceCurrency => 'AM',
            rDiscount                => 'AN',
            rRevenue                 => 'AR',
            currencyCode             => 'AS',
            rFee                     => 'AO',
            rDuration                => 'AE',
            rDeliveryMethod          => 'K',
            rPromoCode               => 'AF',
            rChannel                 => 'J',
            rModel                   => 'F',
            rTransactionCategory     => 'C',
            rComments                => 'A',
        },
    );

    return $map{ $self->_version };
}

sub _headerIdentifier { rIsbn13 => 'EISBN' }

sub _unverifiedFieldMap {
    my $self = shift;

    my %map = (
        25 => {
            altRIsbn13      => 'U',
            altRInstitution => 'I',
            exchangeRate    => 'AR',
        },
        26 => {
            altRIsbn13      => 'V',
            altRInstitution => 'L',
            exchangeRate    => 'AT',
        },
        27 => {
            altRIsbn13      => 'V',
            altRInstitution => 'J',
            exchangeRate    => 'AT',
        },
    );

    return $map{ $self->_version };
}

sub rProductType { shift->_getByFieldName('rProductType') }

sub productType  {
    my $self = shift;

    my $type = $self->_getByFieldName('rProductType') || "";
    if ( $type =~ /^(?:book|custom|Online Resource(?: Bundle)?)$/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }

    $self->fail("Unknown product type: $type");
}

sub rServiceName {
    my $self = shift;

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

    return 'Vital Source';

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

sub countryCode {
    my $self = shift;

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

    return 'KR' if $countryCode =~ /^Korea \(Republic of\)$/i;

    return $countryCode;
}

1;
