package BookPub::Import::Importer::MomentumBooks;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( rIsbn13 => 'Products::Primary ISBN' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            countryCode => 'G',
            rIsbn13     => 'A',
            rTitle      => 'B',
            rAuthor     => 'C',
            rUnits      => 'F',
            rListPrice  => 'D',
            rRevenue    => 'D',

        },
    );

    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 currencyCode               { 'AUD' }
sub _dateNormalization         { 'month' }

sub _isSaleRec {
    my $self = shift;

    # We're going to check for the subtotal line, which only contains
    # totals for columns D, E, and F.
    if ( !$self->countryCode && !$self->rIsbn13 && !$self->rTitle && !$self->rAuthor ) {
        return 0;
    }

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

sub _getDateBegin {
    my $self = shift;

    my $filename = $self->filename();
    $self->fail("Date missing from filename")
      unless ( $filename =~ /(\w{3}) (\d{4})/ );

    my %textMonthVals = (
        "jan" => 1,
        "feb" => 2,
        "mar" => 3,
        "apr" => 4,
        "may" => 5,
        "jun" => 6,
        "jul" => 7,
        "aug" => 8,
        "sep" => 9,
        "oct" => 10,
        "nov" => 11,
        "dec" => 12
    );

    my $month = $textMonthVals{ lc($1) };
    my $year  = $2;
    if ($month) {
        return sprintf( "%04d-%02d-%02d", $year, $month, 1 );
    }
}

1;
