package BookPub::Import::Importer::Christianaudio::Version1;

use strict;

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

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

sub _fieldMap {
    {
        rIsbn13    => 'G',
        rTitle     => 'F',
        rUnits     => 'H',
        rListPrice => 'K',
        rRevenue   => 'N',
    };
}

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

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 '-' ) {
        return $cashPrice;
    } elsif ( defined($subscriptionPrice) && $subscriptionPrice ne '' && $subscriptionPrice ne '-' ) {
        return $subscriptionPrice;

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

1;
