package BookPub::Import::Importer::Christianaudio::Version4;

use strict;

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

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

sub _fieldMap {
    {
        rIsbn13    => 'D',
        rTitle     => 'B',
        rUnits     => 'E',
        rListPrice => 'F',
        rDiscount  => 'H',
        rRevenue   => 'I',
    };
}

sub _unverifiedFieldMap {
    { subscriptionPrice => 'G', };
}

sub priceType    { 'rrp' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeAudioBook }

sub countryCode  { 'US' }
sub currencyCode { 'USD' }

# filename is of format hbg-YYYY-MM.xls
#
sub _getDateBegin {
    my $self     = shift;
    my $filename = $self->filename;

    if ( $filename =~ /(\d{4}-\d{2})\./ ) {
        return $1 = $1;
    } else {
        die "Unable to determine date from filename";
    }

    return;
}

sub _getDateEnd {
    shift->_getDateBegin();
}

sub _dateNormalization { 'month' }

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

    if ( defined($cashPrice) && $cashPrice ne '' && $cashPrice ne '-' && $cashPrice ne '0' ) {
        return $cashPrice;
    } elsif ( defined($subscriptionPrice) && $subscriptionPrice ne '' && $subscriptionPrice ne '-' && $subscriptionPrice ne '0' ) {
        return $subscriptionPrice;

        # Just in case neither list nor subscription price exists
    } else {
        $self->fail("Unable to determine list price");
    }
}

sub discount {
    my $self     = shift;
    my $discount = $self->_getByFieldName('rDiscount');
    $discount = 1 - $discount;

    return $discount;
}

1;
