package BookPub::Import::Importer::Audible::Version5;

use strict;

use lib '/app/tools/common/lib/';
use Common::Country;

use lib '/app/tools/sale_import/lib/';
use Import::ValidationError;

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

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

sub _fieldMap {
    {
        # Summary Page
        dateBegin => 'B',

        # Detail Page
        countryCode => 'B',
        isbn        => 'F',
        rTitle      => 'D',
        rAuthor     => 'E',
        rUnits      => 'P',
        rListPrice  => 'G',
        rRevenue    => 'Q',
    };
}

sub _useIsSaleRec { 1 }

sub _isSaleRec {
    my $self    = shift;
    my $country = $self->_getByFieldName('countryCode');

    # Only process sales on the 'non-promotional' or 'details' tab
    my $validTab;
    my $validTabNames = _validTabNames();
    foreach my $validTabName (@$validTabNames) {
        if ( lc( $self->sheetname() ) eq lc($validTabName) ) {
            $validTab = 1;
        }
    }
    return unless $validTab;

    # Ignore inline total lines
    return if ( $country && $country =~ /Total/i );

    # Ignore refunds with 0 revenue for Hachette only.
    return if ( !$self->rRevenue() && $self->rUnits() < 0 && Common::RSApp::GetClientID == 308 );

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

sub priceType    { 'rrp' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeAudioBook }

sub currencyCode { 'USD' }

sub _processHeader {
    my $self = shift;
    unless ( $self->_getDateBegin() ) {
        my $input = $self->_getByFieldName('dateBegin');
        $self->{_dateBegin} = $input
          if ( $input =~ /^\d+$/
            || $input =~ /^\w{3}-\d{2}$/
            || $input =~ /^\w{3}-\d{4}$/
            || $input =~ /^\d{1,2}\/\d{1,2}\/\d{4}/ );
    }
}

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

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

    my $foundTab;
    my $sheetnum = 0;
    foreach my $sheet ( @{ $args{sheets} } ) {
        my $sheetname = $args{sheet_names}->[$sheetnum];
        $sheetnum++;

        my $validTabNames = _validTabNames();
        foreach my $validTabName (@$validTabNames) {
            if ( lc($sheetname) eq lc($validTabName) ) {
                $foundTab = 1;
            }
        }
    }

    if ( !$foundTab ) {
        $self->errstr('Unable to find tab with sales data');
        return undef;
    }

}

sub _validTabNames { [ 'Non-Promotional', 'Details', 'Sales Details' ] }
sub _dateFormat    { [ 'excel',           'text',    'us' ] }
sub _dateNormalization { 'month' }
sub _getDateBegin      { shift->{_dateBegin} }
sub _getDateEnd        { shift->_getDateBegin() }

sub rListPrice {
    my $listPrice = shift->SUPER::rListPrice();

    return $listPrice && $listPrice =~ /promo/i ? undef : $listPrice;
}

sub rTitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $title;
}

sub rSubtitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $subtitle;
}

1;
