package BookPub::Import::Importer::BolindaDigital::Version5;

use strict;

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

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        5 => {
            currencyCode       => 'A4',
            countryCode        => 'A',
            dateBegin          => 'A2',
            dateEnd            => 'A2',
            rIsbn13            => 'B',
            rTitle             => 'C',
            rAuthor            => 'D',
            rImprint           => 'F',
            rUnits             => 'I',
            rListPrice         => 'G',
            rListPriceCurrency => 'A',
            rDiscount          => 'H',
            rRevenue           => 'K',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        5 => {
            subtotalCheck => 'J',
        },
    );

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

sub priceType {
    my $self = shift;
    if ( Common::RSApp::GetClientID() == 328 ) {    # Bloomsbury World Wide
        return 'rrp';
    } elsif ( Common::RSApp::GetClientID() == 335 ) {    # BKPub
        return 'rrp';
    } elsif ( Common::RSApp::GetClientID() == 359 ) {    # MMUK
        return 'rrp';
    } else {
        return 'agency';
    }
}

sub currencyCode {
    my $self = shift;
    my $currency = $self->_getByFieldName('currencyCode') || return;

    if ( $currency && $currency =~ /\[(\w{3})\]$/ ) {
        return $1;
    }
    if ( $currency && $currency =~ /Payment Currency:(\w{3})$/ ) {
        return $1;
    }

    if ( $currency && $currency =~ /Amount Due (\w{3})$/ ) {
        return $1;
    }
}

sub rListPriceCurrency {
    my $self        = shift;
    my $country     = $self->_getByFieldName('rListPriceCurrency');
    my $countryCode = $Common::Consts::COUNTRY_CODE{ lc $country };

    if ( $countryCode eq "NZ" || $countryCode eq 'AU' ) {
        return "AUD";
    }
    if ( $countryCode eq "GB" ) {
        return "GBP";
    }
    $self->fail("Unable to derive currency code from $country");
}

sub rTaxAmount {
    my $self = shift;
    return;
}

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

    if ( $discount && $discount =~ /^(\d*)%?/ ) {
        $discount = ( 100 - $1 ) / 100;
        return $discount;
    }
    $self->fail("Discount is in an unexpected format: $discount");
}

sub _processLine {
    my $self    = shift;
    my $revenue = $self->rRevenue();

    if ( $revenue && $revenue =~ m/Amount Due/i ) {
        $self->{_endOfData} = 1;
    }

    my $subtotalCheck = $self->_getByFieldName('subtotalCheck');
    if (
        $subtotalCheck
        && (   $subtotalCheck =~ m/Amount Due/i
            || $subtotalCheck =~ m/TOTAL PAYMENT DUE/i )
      ) {
        $self->{_endOfData} = 1;
    }

    return $self->BookPub::Import::Importer::_processLine(@_);
}

sub _isSaleRec {
    my $self = shift;

    # If we've set the end of data flag in processLine, we are done.
    return if ( $self->{_endOfData} );

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

1;
