package BookPub::Import::Importer::Intel;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        5 => {
            purchaseType => 'D',
            currencyCode => 'M',
            countryCode  => 'L',
            rState       => 'R',
            rPostalCode  => 'S',
            dateBegin    => 'G2',
            dateEnd      => 'G2',
            rIsbn13      => 'B',
            rTitle       => 'O',
            rAuthor      => 'N',
            rImprint     => 'P',
            rUnits       => 'H',
            rSales       => 'E',
            rReturns     => 'G',
            rListPrice   => 'I',
            rDiscount    => 'J',
            rRevenue     => 'K',

        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        5 => {
            rentals => 'F',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _dateNormalization         { 'month' }
sub _dateFormat                { 'text' }
sub priceType                  { 'agency' }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _getDateBegin {
    my $self      = shift;
    my $dateBegin = $self->_getByFieldName('dateBegin');
    $dateBegin =~ s/'//;

    return $dateBegin;
}

sub _getDateEnd {
    my $self    = shift;
    my $dateEnd = $self->_getByFieldName('dateEnd');
    $dateEnd =~ s/'//;

    return $dateEnd;
}

sub purchaseType {
    my $self         = shift;
    my $purchaseType = $self->_getByFieldName('purchaseType');

    if ( lc($purchaseType) eq 'buy' )  { return BookPub::DB::Item::Sale::kPurchaseTypeDownload }
    if ( lc($purchaseType) eq 'rent' ) { return BookPub::DB::Item::Sale::kPurchaseTypeLoan }

    $self->fail("License Type ($purchaseType) not supported");
}

# sales are from two columns, sold and rented
sub rSales {
    my $self = shift;

    my $sales   = $self->_getByFieldName('rSales');
    my $rentals = $self->_getByFieldName('rentals');

    if ( $sales == 0 ) {
        return $rentals;
    } else {
        return $sales;
    }
}

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

    return abs($listPrice);
}

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