package BookPub::Import::Importer::OverDrive::Version19;

use strict;

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

sub _fieldMap {
    {
        dateBegin                => 'A',
        rProductType             => 'I',
        rFormat                  => 'I',
        rAuthor                  => 'F',
        rTitle                   => 'D',
        rSubtitle                => 'E',
        rIsbn13                  => 'C',
        rInstitution             => 'G',
        countryCode              => 'H',
        serviceProductID         => 'B',
        rListPrice               => 'K',
        rListPriceCurrency       => 'K1',
        rNativeListPrice         => 'J',
        rNativeListPriceCurrency => 'J1',
        rDiscount                => 'L',
        rRevenue                 => 'N',
        currencyCode             => 'N1',
    }
}

sub _headerIdentifier { rIsbn13 => 'ISBN' }
sub _useIsSaleRec     { 1 }
sub rServiceName      { 'OverDrive' }


sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rPurchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rUnits        { shift->rRevenue >= 0 ? 1 : -1 }
sub priceType     { 'rrp' }

sub currencyCode {
    my $self = shift;
    unless ( exists $self->{_currencyCode} ) {
        my $code = $self->_getByFieldName('currencyCode') || "";
        $code =~ /^Amt owed (\w{3})$/ || $self->fail("Unable to determine currency from header '$code'");
        $self->{_currencyCode} = $1;
    }
    return $self->{_currencyCode};
}

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

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

sub productType {
    my $self = shift;
    my $format = $self->_getByFieldName('rFormat') || return;

    return BookPub::DB::Item::Sale::kProductTypeAudioBook if ( $format =~ /audio/i );
    return BookPub::DB::Item::Sale::kProductTypeEBook     if ( $format =~ /ebook/i );
    return BookPub::DB::Item::Sale::kProductTypeEBook     if ( $format =~ /overdrive read/i );
    return BookPub::DB::Item::Sale::kProductTypeAudioBook if ( $format =~ /overdrive listen/i );

    $self->fail( "Product type not set for " . $format );
}
sub formatType {
    my $self = shift;

    my $format = $self->_getByFieldName('rFormat') || "";
    return $self->_parseFormat($format);
}

sub isFree {
    my $self = shift;

    return (((!$self->rRevenue || $self->rRevenue =~ /^0.0+/) && $self->rUnits) ? 'Y' : 'N');
}

1;
