package BookPub::Import::Importer::OverDrive::Version5;

use strict;

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

sub _fieldMap {
    {
        countryCode        => 'H',
        rState             => 'G',
        dateBegin          => 'A',
        rFormat            => 'I',
        isbn               => 'C',
        rTitle             => 'D',
        rSubtitle          => 'E',
        rAuthor            => 'F',
        rListPrice         => 'K',
        rListPriceCurrency => 'K1',
        rDiscount          => 'L',
        rRevenue           => 'N',
        currencyCode       => 'N1',
    };
}

sub _unverifiedFieldMap {
    {
        rListPriceNative => 'J',
        rRevenueNative   => 'M',
    };
}

sub _headerIdentifier { ( rTitle => 'Title' ) }

sub _useIsSaleRec { 1 }
sub units         { 1 }
sub rSales        { 1 }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub dateEnd       { shift->dateBegin() }

sub priceType {
    my $self = shift;

    my $clientID = Common::RSApp::GetClientID();
    if ( $clientID == 348 ) {
        return 'agency';
    } else {
        return 'rrp';
    }

}

sub rListPrice {
    my $self = shift;
    my $listPrice;

    my $clientID = Common::RSApp::GetClientID();
    if ( $clientID == 348 || $clientID == 359 ) {
        $listPrice = $self->_getByFieldName('rListPriceNative');
    } else {
        $listPrice = $self->_getByFieldName('rListPrice');
    }

    return $listPrice;
}

sub rRevenue {
    my $self = shift;
    my $revenue;

    my $clientID = Common::RSApp::GetClientID();
    if ( $clientID == 348 || $clientID == 359 ) {
        $revenue = $self->_getByFieldName('rRevenueNative');
    } else {
        $revenue = $self->_getByFieldName('rRevenue');
    }

    return $revenue;
}

sub currencyCode {
    my $self = shift;
    unless ( exists $self->{_currencyCode} ) {
        my $clientID = Common::RSApp::GetClientID();
        if ( $clientID == 348 ) {
            $self->{_currencyCode} = "AUD";
        } elsif ( $clientID == 359 ) {
            $self->{_currencyCode} = "GBP";
        } else {
            my $header = $self->_getByFieldName('currencyCode');
            $header =~ m/^Amt Owed ([A-Z]{3})$/
              || $self->fail("Unable to determine currency from header '$header'");
            $self->{_currencyCode} = $1;
        }
    }
    return $self->{_currencyCode};
}

sub rListPriceCurrency {
    my $self = shift;
    unless ( exists $self->{_listPriceCurrency} ) {
        my $clientID = Common::RSApp::GetClientID();
        if ( $clientID == 348 ) {
            $self->{_listPriceCurrency} = "AUD";
        } elsif ( $clientID == 359 ) {
            $self->{_listPriceCurrency} = "GBP";
        } else {
            my $header = $self->_getByFieldName('rListPriceCurrency');
            $header =~ m/^SRP([A-Z]{3})$/
              || $self->fail("Unable to determine list price currency from header '$header'");
            $self->{_listPriceCurrency} = $1;
        }
    }
    return $self->{_listPriceCurrency};
}

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

    $format =~ /\s(\w+)\saudiobook/i;
    my $type = lc($1);

    if ( $self->productType eq BookPub::DB::Item::Sale::kProductTypeAudioBook ) {
        return $type;
    } else {
        return $self->_parseFormat($format);
    }
}

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

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

    return undef;
}

sub rState {
    my $self = shift;

    my $state;
    my $rState = $self->_getByFieldName('rState');
    if ( $rState =~ / \((\w{2})\)$/ ) {
        $rState = $1;
        if ( my $postal = Common::Postal->new( $self->countryCode() ) ) {
            if ( $postal->getName($rState) ) {

                # acceptable state abbreviation
                $state = $rState;
            }
        }
    }

    return $state;
}

1;
