package BookPub::Import::Importer::Virdocs;

use strict;

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 => {
            rIsbn13    => 'B',
            rTitle     => 'A',
            rAuthor    => 'C',
            rUnits     => 'E',
            rListPrice => 'F',
            rTaxAmount => 'H',
            rRevenue   => 'L',

        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            pricing => 'D',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _dateNormalization         { 'month' }
sub priceType                  { 'rrp' }
sub countryCode                { 'US' }
sub currencyCode               { 'USD' }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _getDateBegin {
    my $self = shift;

    my $filename = $self->filename;
    if ( $filename =~ /(\d{4})(\d{2})/ ) {
        return sprintf( "%04d-%02d-%02d", $1, $2, 1 );
    }

    $self->fail("Unable to parse date from file name");

}

sub rAuthor {
    my $self = shift;

    my $author = $self->_getByFieldName('rAuthor');
    if ( $author ne "-" ) {
        return $author;
    }

}

sub _isSaleRec {
    my $self = shift;

    # We're going to check for the subtotal line
    my $pricing = $self->_getByFieldName('pricing');
    if ( $pricing eq 'TOTAL' ) {
        return 0;
    }

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

1;
