package BookPub::Import::Importer::InAudio::Version6;

use strict;

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

sub _headerIdentifier { ( rIsbn13 => 'ISBN #' ) }

sub rListPriceCurrency { 'USD' }
sub formatType         { return }

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName(
        service_id => shift->serviceID
    );
}

# we need to call these here to override the same sub in the base class

sub rDiscount {
    my $self = shift;

    my $discount = $self->_getByFieldName('rDiscount');
    return 1 - $discount;
}

sub productType {
    my $self = shift;

    my $productType = $self->_getByFieldName('rProductType');

    if ( $productType =~ /^Digital Library\s?(?:Sales|,Retail)/i ) {
        return BookPub::DB::Item::Sale::kProductTypeAudioBook;
    } elsif ( $productType =~ /^Digital Retail Sales/i ) {
        return BookPub::DB::Item::Sale::kProductTypeAudioBook;
    } elsif ( $productType =~ /^Digital Subscription Sales/i ) {
        return BookPub::DB::Item::Sale::kProductTypeAudioBook;
    } elsif ( $productType =~ /^Digital Sales by Item/i ) {
        return BookPub::DB::Item::Sale::kProductTypeAudioBook;
    } elsif ( $productType =~ /eReader/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }

    $self->fail("Unknown product type: $productType");
}

sub _processLine {
    my $self    = shift;
    my $channel = $self->_getByFieldName('rChannel');
    my $isbn    = $self->_getByFieldName('rIsbn13');

    if ( ( $channel && $channel =~ /Grand Total/i ) || ( $isbn && $isbn =~ /Grand Total/i ) ) {
        $self->{_endOfData} = 1;
    }

    return $self->SUPER::_processLine(@_);
}

1;
