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

use strict;

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

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

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        7 => {
            dateBegin    => 'A3',
            dateEnd      => 'A3',
            rAuthor      => 'D',
            rTitle       => 'C',
            rIsbn13      => 'B',
            rImprint     => 'F',
            countryCode  => 'A',
            rUnits       => 'I',
            rListPrice   => 'G',
            rCOGS        => 'H',
            rRevenue     => 'J',
            currencyCode => 'A5',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        7 => {
            totalCheck => '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 }

# we're going to override the base discount method since we want to derive our own
# discount, not base it on the COGS value
sub discount {
    my $self      = shift;
    my $unitPrice = $self->unitPrice;
    my $listPrice = $self->listPrice + 0;
    my $discount;

    my $listPriceCurrency = $self->rListPriceCurrency();
    my $unitPriceCurrency = $self->currencyCode();

    if (   defined($unitPrice)
        && defined($listPrice)
        && $listPrice != 0
        && ( !$listPriceCurrency || $listPriceCurrency eq $unitPriceCurrency ) ) {
        $discount = 1 - $unitPrice / $listPrice;
    } else {

        # No discount stored if we dont't have it and can't derive it.
    }

    return $discount && $discount > 1 ? $discount / 100 : $discount;
}

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

    my $total = $self->_getByFieldName('totalCheck');
    if ( $total && $total =~ m/TOTAL AMOUNT/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();
}

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

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 =~ /Payment Currency:(\w{3})$/ ) {
        return $1;
    }
}

1;
