package BookPub::Import::Importer::ProdeviaLearning;

use strict;
use Data::Dumper;

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

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            dateBegin  => 'A1',
            rAuthor    => 'B',
            rTitle     => 'A',
            rIsbn13    => 'C',
            rUnits     => 'D',
            rUnitPrice => 'E',
            rRevenue   => 'F',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _dateNormalization         { 'quarter' }
sub _dateFormat                { 'quarter' }
sub _getDateEnd                { shift->{_dateBegin} }
sub countryCode                { 'US' }
sub currencyCode               { 'USD' }
sub rListPriceCurrency         { 'USD' }
sub priceType                  { 'rrp' }
sub rFormat                    { BookPub::DB::Item::Sale::kFormatTypePDF }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType               { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

sub _getDateBegin {
    my $self = shift;

    if ( $self->{_dateBegin} ) {
        return $self->{_dateBegin};
    }

    my $date = $self->_getByFieldName('dateBegin');

    if ( $date =~ /(\d)Q(\d{4})$/i ) {
        $self->{_dateBegin} = 'Q' . $1 . "-" . $2;
        return $self->{_dateBegin};
    }

    # Didn't find it?  Give up.
    if ( !$self->{_dateBegin} ) {
        $self->errstr('Unable to find date in header');
        return undef;
    }
}

sub _isSaleRec {
    my $self   = shift;
    my $title  = $self->_getByFieldName('rTitle');
    my $isbn   = $self->_getByFieldName('rIsbn13');
    my $author = $self->_getByFieldName('rAuthor');
    my $price  = $self->_getByFieldName('unitPrice');

    # Skipping the subtotal line, which is blank for these columns.
    if ( !$title && !$isbn && !$author && !$price ) {
        return undef;
    }

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

###
1;    # Play nicely.
###
