package BookPub::Import::Importer::BarnesNoble::Version8;

use strict;
use Data::Dumper;

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

use lib '/app/tools/sale_import/lib/';
use Import::ValidationError;

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

sub _headerIdentifier { ( rTitle => 'Application' ) }

sub _fieldMap {
    {
        dateBegin => 'A',
        rTitle    => 'C',
        rUnits    => 'E',
        rRevenue  => 'D',
    };
}

sub _unverifiedFieldMap {
    { eGiftDateBegin => 'B', };
}

sub _useIsSaleRec { 1 }

sub priceType    { 'agency' }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub currencyCode { 'USD' }
sub countryCode  { 'US' }

sub _getDateBegin {
    my $self = shift;

    # We'll use the eGift Date if that column is populated.
    my $date = $self->_getByFieldName('eGiftDateBegin');

    if ( !$date ) {
        $date = $self->_getByFieldName('dateBegin');
    }

    return $date;
}

sub rListPrice {
    my $self = shift;

    my $units = $self->_getByFieldName('rUnits');

    if ($units) {
        my $revenue     = $self->_getByFieldName('rRevenue');
        my $costOfGoods = .7;                                     # 1 - discount rate of .3
        my $listPrice   = ( $revenue / $units ) / $costOfGoods;
        return $listPrice;
    }

    return undef;
}

sub rTitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $title;
}

sub rSubtitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $subtitle;
}

1;
