package BookPub::Import::Importer::OverDrive::Version17;

use strict;
use warnings;

use base 'BookPub::Import::Importer::OverDrive';

sub _fieldMap {
    return {
        dateBegin                => 'B',
        rUnits                   => 'R',
        rProductType             => 'E',
        rFormat                  => 'E',
        priceType                => 'Y',
        priceTypeQualifierID     => 'Y',
        rAuthor                  => 'I',
        rTitle                   => 'G',
        rSubtitle                => 'H',
        rIsbn13                  => 'D',
        rImprint                 => 'K',
        rInstitution             => 'X',
        rState                   => 'L',
        serviceProductID         => 'C',
        countryCode              => 'M',
        rListPrice               => 'N',
        rListPriceCurrency       => 'N1',
        rPurchasePrice           => 'O',
        rPurchasePriceCurrency   => 'P',
        rNativeListPrice         => 'N',
        rNativeListPriceCurrency => 'N1',
        rDiscount                => 'Q',
        rRevenue                 => 'Z',
        currencyCode             => 'Z1',
        isPreOrder               => 'U',
        rChannel                 => 'Y',
        rModel                   => 'W'
    }
}

sub rServiceName { "OverDrive" }
sub productType  { shift->rProductType() }
sub rPurchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

# We need to mark the sale as a library sale
sub rComments {
    my $self = shift;

    my $priceTypeQualifierID = $self->_getByFieldName('priceTypeQualifierID') || '';
    if ( $priceTypeQualifierID =~ /^Institutional - (Public|School|Other|Academic)(?: Library)?$/i ) {
        return 'institutional'
    }
}

sub priceTypeQualifierID {
    my $self = shift;

    my $qualifier_id = $self->_getByFieldName('priceTypeQualifierID') || '';
    if ( $qualifier_id =~ /^Institutional - (Public|School|Other|Academic)(?: Library)?$/i ) {
        return BookPub::DB::Item::PriceTypeQualifier::kCorporate;
    }

    return undef;
}

sub rNativeListPriceCurrency {
    my $self = shift;
    unless ( exists $self->{_nativeCurrencyCode} ) {
        my $header = $self->_getByFieldName('rNativeListPriceCurrency');
        $header =~ /^SRP (\w{3})$/i
          || $self->fail("Unable to determine currency from header '$header'");
        $self->{_nativeCurrencyCode} = $1;
    }
    return $self->{_nativeCurrencyCode};
}

sub rProductType {
    my $self = shift;
    my $format = $self->rFormat() || "";

    return BookPub::DB::Item::Sale::kProductTypeEBook     if ( $format =~ /(?:ebook|read)/i );
    return BookPub::DB::Item::Sale::kProductTypeAudioBook if ( $format =~ /(?:audiobook|OverDrive Listen)/i );

    $self->fail( "Product type not set for " . $format );
}

sub rRevenue {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue') || 0;
    $revenue =~ s/^0\.0+$/0/;

    return $revenue;
}

sub rUnitPrice {
    my $self = shift;

    my $revenue = $self->rRevenue() || 0;
    my $units   = $self->rUnits()   || 0;

    return ($revenue / $units) if $units;
}

sub isFree {
    my $self = shift;

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

1;
