package BookPub::Import::Importer::OverDrive::Version11;

use strict;
use warnings;

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

sub _fieldMap {
    {
        # Detail
        rPurchaseType            => 'I',
        rComments                => 'I',
        rPriceType               => 'AM',
        currencyCode             => 'H',
        countryCode              => 'AK',
        dateBegin                => 'N',
        rProductType             => 'AC',
        rIsbn13                  => 'T',
        rTitle                   => 'W',
        rAuthor                  => 'X',
        rImprint                 => 'AB',
        rSales                   => 'AE',
        rReturns                 => 'AF',
        rListPrice               => 'BP',
        rListPriceCurrency       => 'AN',
        rPurchasePrice           => 'AP',
        rDiscount                => 'AO',
        rUnitPrice               => 'AL',
        rRevenue                 => 'BB',
        rInstitution             => 'M',
        serviceProductID         => 'V',
        rFee                     => 'AT',
        rNativeListPrice         => 'AL',
        rNativeListPriceCurrency => 'AN'
    };
}

sub _unverifiedFieldMap {
    {
        # Detail
        altListPrice => 'AL',
        returnPrice  => 'AQ',
    };
}

sub rServiceName { 'OverDrive' }

sub rListPrice {
    my $self = shift;
    if ( $self->units() ) {
        my $listPrice = $self->_getByFieldName('rListPrice');

        if ( !$listPrice ) {
            $listPrice = $self->_getByFieldName('altListPrice');
        }

        $listPrice /= $self->units();
        my $priceCurrency = $self->_getByFieldName('rListPriceCurrency');
        my $currencyCode  = $self->_getByFieldName('currencyCode');

        if ( $self->listPriceRequired && $priceCurrency ne $currencyCode ) {

            # We're just going to have to use purchase price as the list price.
            $listPrice = $self->rPurchasePrice;
        }

        return abs($listPrice);
    }
}

sub priceTypeQualifierID {
    my $self = shift;
    my $type = lc( $self->rComments );

    if ( $type =~ /^(?:institutional(?: - CPC)?|Education - (?:Class Set|CPC))$/i ) {
        return BookPub::DB::Item::PriceTypeQualifier::kCorporate;
    }

    if ( $type =~ /education/i  ) {
        return BookPub::DB::Item::PriceTypeQualifier::kSchoolLibrary;
    }

    return;
}

sub rComments {
    my $self = shift;

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

    # This exception is for MMUS only, see RSD-7490
    if ( Common::RSApp::GetClientID == 318 ) {
        if ( $rComments !~ /^(?:(?:Institutional|Education)(?: - CPC)?|Agency|Wholesale|Education(?: - Class Set)?)$/i ) {
            $self->fail("Unexpected sale type class '$rComments'");
        }
    }

    return $rComments;
}


1;
