package BookPub::Import::Importer::RecordedBooks::Version2;

use strict;

use Spreadsheet::ParseExcel::Utility;

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

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

sub _fieldMap {
    {
        countryCode => 'K',
        rIsbn13     => 'B',
        rTitle      => 'D',
        rAuthor     => 'E',
        rUnits      => 'F',
        rListPrice  => 'G',
        rDiscount   => 'I',
        rRevenue    => 'J',
    };
}

sub _useIsSaleRec { 1 }

sub _dateFormat          { 'text' }
sub _dateNormalization   { 'month' }
sub priceType            { 'rrp' }
sub priceTypeQualifierID {  BookPub::DB::Item::PriceTypeQualifier::kCorporate } # Library
sub currencyCode         { 'USD' }
sub purchaseType         { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub _getDateBegin        { shift->{_dateBegin} }
sub _getDateEnd          { shift->{_dateBegin} }

sub _preProcess {
    my $self = shift;
    my %args = @_;

    Log->info("Starting _preProcess");
    $self->SUPER::_preProcess(%args);

    # Trying to detect a header change that would mean there are more than ebooks here.
    # !!! Now with support for eAudio.
    my $foundEbook  = 0;
    my $foundEAudio = 0;

    foreach my $sheet ( @{ $args{sheets} } ) {
        foreach my $line (@$sheet) {

            # Since we're at the preProcess stage, it seems we have to reference rows by number.
            my $firstCell = $line->[0];
            if ( $firstCell =~ m/^(\w+) Distribution Earnings/ ) {
                if ( $1 eq 'eBook' ) {
                    $foundEbook = 1;
                }
            }

            if ( $firstCell =~ m/^(\w+) Digital Download Earnings/ ) {
                if ( $1 eq 'eAudio' ) {
                    $foundEAudio = 1;
                }
            }

            if ( $firstCell =~ m/(\w+ \d{4})$/i ) {
                $self->{_dateBegin} = "$1";
            }
        }

        if ( !$self->{_dateBegin} ) {
            $self->fail("Unable to find date in header");
        }

        if ( $foundEbook == 1 ) {
            $self->{_productType} = BookPub::DB::Item::Sale::kProductTypeEBook;
            $self->{_rProductType} = "eBook";
        } elsif ( $foundEAudio == 1 ) {
            $self->{_productType} = BookPub::DB::Item::Sale::kProductTypeAudioBook;
            $self->{_rProductType} = "eAudio";
        } else {
            $self->fail("Unable to find product type in header");
        }
    }
}

sub productType {
    my $self = shift;
    return $self->{_productType};
}

sub rProductType {
    my $self = shift;
    return $self->{_rProductType};

}

sub rServiceName { 'Recorded Books' }

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

    return $discount;
}

sub _isSaleRec {
    my $self = shift;

    # Trying to be as specific as possible when skipping the footer.
    return if ( !$self->countryCode
        && !$self->rIsbn13
        && !$self->rTitle
        && !$self->rListPrice
        && !$self->rDiscount
        && !$self->rAuthor );

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

1;
