package BookPub::Import::Importer::Christianaudio::Version5;

use strict;

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

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

sub _fieldMap {
    {
        # Header
        rProductType => 'C',

        # Data
        dateBegin  => 'B',
        rIsbn13    => 'E',
        rTitle     => 'C',
        rUnits     => 'F',
        rListPrice => 'G',
        rRevenue   => 'J',
    };
}

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

sub priceType    { shift->{_priceType} }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub countryCode  { 'US' }
sub currencyCode { 'USD' }

sub _processHeader {
    my $self = shift;
    my $type = $self->_getByFieldName('rProductType');

    if ( $type =~ /audiobook/i ) {
        $self->{_productType} = BookPub::DB::Item::Sale::kProductTypeAudioBook;
        $self->{_priceType}   = 'rrp';
    }

    if ( $type =~ /ebook/i ) {
        $self->{_productType} = BookPub::DB::Item::Sale::kProductTypeEBook;
        $self->{_priceType}   = 'agency';
    }

}

sub productType {
    my $self = shift;

    return $self->{_productType};

}

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");
    }
}

###
1;    # Play nicely.
###
