package BookPub::Import::Importer::BarnesNobleFirstDay::v1;

use strict;

use lib '/app/tools/bookpub/lib';
use Date::Calc qw(Days_in_Month);

use parent 'BookPub::Import::Importer';

sub _fieldMap {
    {
        rServiceName           => 'D',
        dateBegin              => 'A6',
        dateEnd                => 'A6',
        rTitle                 => 'F',
        rIsbn13                => 'E',
        rInstitution           => 'G',
        rUnits                 => 'J',
        rUnitPrice             => 'K',
        rRevenue               => 'L',
        rChannel               => 'D',
    };
}

sub _headerIdentifier { rIsbn13 => 'ISBN' }

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

sub rServiceName  {
    my $self = shift;
    my $serviceName = $self->_getByFieldName("rServiceName");

    return 'Barnes & Noble - First Day' if ($serviceName eq "First Day");
}

sub rIsbn13  { return shift->_getByFieldName("rIsbn13") =~ s/^'//rg }


sub dateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName("dateBegin");

    if ($date =~ /(\d+)\/(\d+)\/(\d+)/){
        return sprintf "%04d-%02d-%02d", $3, $1, $2;
    }

    $self->fail("Can't determine dateBegin from $date.");
}

sub dateEnd { shift->dateBegin() }

# Sripping leading $-sign in revenue and unitPrice values
sub rRevenue   { return (shift->_getByFieldName('rRevenue') =~ s/^\$//gr) }
sub rUnitPrice { return (shift->_getByFieldName('rUnitPrice') =~ s/^\$//gr) }

sub _isSaleRec {
    my $self = shift;

    return unless (
           $self->rUnits
        && $self->rIsbn13
        && $self->rUnitPrice
    );

    return $self->BookPub::Import::Importer::_isSaleRec();
}

1;
