package BookPub::Import::Importer::LearnOutLoud;

use strict;

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

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        3 => {
            countryCode => 'I',
            isbn        => 'H',
            rTitle      => 'B',
            rAuthor     => 'C',
            rUnits      => 'A',
            rListPrice  => 'D',
            rDiscount   => 'E',
            rRevenue    => 'G',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        3 => {
            totalCol => 'F',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub priceType                  { 'rrp' }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType                { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub currencyCode               { 'USD' }

sub _getDateBegin {
    my $self = shift;

    my $filename = $self->filename();

    $self->fail("Could not pull report date from filename: $filename")
      unless ( $filename =~ /byQuantityfor(\w{3})(\d{4})/i );

    return $1 . " " . $2;
}

sub _dateFormat { [ 'text', 'iso' ] }

sub _dateNormalization { 'month' }

sub _isSaleRec {
    my $self = shift;

    my $totalCol = $self->_getByFieldName('totalCol');

    # We need to ignore the total line
    if ( $totalCol =~ /Total Due/i ) {
        return 0;
    }

    return $self->SUPER::_isSaleRec();
}

sub rTitle {
    my $self  = shift;
    my $title = $self->SUPER::rTitle();
    if ( $title =~ /^(.*) \(Audio Download\)$/ ) {
        $title = $1;
    }
    return $title;
}

sub rSubtitle {
    my $self     = shift;
    my $subtitle = $self->SUPER::rSubtitle();
    if ( $subtitle =~ /^(.*) \(Audio Download\)$/ ) {
        $subtitle = $1;
    }
    return $subtitle;
}

1;
