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

use strict;

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

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

use Date::Calc qw( Add_Delta_YM );

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            dateBegin          => 'A3',
            dateEnd            => 'A3',
            countryCode        => 'A',
            rIsbn13            => 'C',
            rTitle             => 'E',
            rAuthor            => 'G',
            rImprint           => 'K',
            rListPrice         => 'M',
            rListPriceCurrency => 'A',
            rUnits             => 'O',
            rRevenue           => 'Q',
            currencyCode       => 'A2',
            rDiscount          => 'A7',
        },
        2 => {
            dateBegin          => 'A3',
            dateEnd            => 'A3',
            countryCode        => 'A',
            rIsbn13            => 'C',
            rTitle             => 'E',
            rAuthor            => 'G',
            rImprint           => 'K',
            rListPrice         => 'M',
            rListPriceCurrency => 'A',
            rUnits             => 'P',
            rRevenue           => 'R',
            currencyCode       => 'A2',
            rDiscount          => 'A7',
        },
        4 => {
            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           => 'J',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            altCurrencyCode => 'C8',
            subtotal        => 'P',
        },
        2 => {
            altCurrencyCode => 'A8',
            subtotal        => 'M',
        },
        4 => {
            altCurrencyCode => 'A5',
            subtotal        => 'I',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub priceType                  { 'agency' }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin') || return;

    if ( $date && $date =~ /(\d{2})\/(\d{2})\/(\d{4}) - \d{2}\/\d{2}\/\d{4}/ ) {
        return sprintf( "%04d-%02d-%02d", $3, $2, $1 );
    }
}

sub _getDateEnd {
    my $self = shift;
    my $date = $self->_getByFieldName('dateEnd') || return;

    if ( $date && $date =~ /\d{2}\/\d{2}\/\d{4} - (\d{2})\/(\d{2})\/(\d{4})/ ) {
        return sprintf( "%04d-%02d-%02d", $3, $2, $1 );
    }
}

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;
    }

    $currency = $self->_getByFieldName('altCurrencyCode') || return;

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

# Just a flat 10 percent
sub rTaxAmount {
    my $self = shift;
    return $self->revenue * .1;
}

sub rListPriceCurrency {
    my $self        = shift;
    my $country     = $self->_getByFieldName('rListPriceCurrency');
    my $countryCode = $Common::Consts::COUNTRY_CODE{ lc $country };
    if ( $countryCode eq "NZ" ) {
        return "AUD";
    }
    if ( $countryCode ne "" ) {
        my $currencyFormat = Common::CurrencyFormat->new( countryCode => $countryCode );
        my $currencyCode = $currencyFormat->currencyCode();
        return $currencyCode;
    }
    $self->fail("Unable to derive currency code from $country");
}

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

    if ( $discount && $discount =~ /^(\d*)%?/ ) {
        $discount = ( 100 - $1 ) / 100;
        return $discount;
    }
    $self->fail("Unable to find discount in header");
}

sub _processLine {
    my $self      = shift;
    my $listPrice = $self->rListPrice();

    if ( $listPrice && $listPrice =~ m/TOTAL/i ) {
        $self->{_endOfData} = 1;
    }

    my $subtotal = $self->_getByFieldName('subtotal');
    if ( $subtotal && $subtotal =~ m/TOTAL/i ) {
        $self->{_endOfData} = 1;
    }

    return $self->SUPER::_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;
