package BookPub::Import::Importer::RecordedBooks::Version3;

use strict;

use Spreadsheet::ParseExcel::Utility;

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

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

sub _fieldMap {
    {
        dateBegin          => 'A2',
        rAuthor            => 'D',
        rTitle             => 'C',
        rIsbn13            => 'B',
        rImprint           => 'F',
        countryCode        => 'L',
        rReturns           => 'I',
        rSales             => 'H',
        rUnits             => 'J',
        rListPrice         => 'G',
        rListPriceCurrency => 'M11',
        rDiscount          => 'K',
        rRevenue           => 'M',
        currencyCode       => 'M11',
    };
}

sub _useIsSaleRec { 1 }

sub _dateFormat        { 'iso' }
sub _dateNormalization { 'month' }
sub priceType          { 'rrp' }

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

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

    if ( $code =~ m/(\w{3})$/ ) {
        $code = $1;
    } else {
        $self->fail("Can't find currencyCode in $code");
    }
    return $code;
}

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

    if ( $code =~ m/(\w{3})$/ ) {
        $code = $1;
    } else {
        $self->fail("Can't find listPriceCurrency in $code");
    }
    return $code;
}

sub _isSaleRec {
    my $self = shift;

    # Trying to be as specific as possible when skipping the footer.
    return if ( !$self->rIsbn13
        && !$self->rTitle
        && !$self->rListPrice
        && !$self->rDiscount
        && !$self->rAuthor );

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

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

    if ( $date =~ m/(\d{2})-(\d{4})$/ ) {
        $date = $2 . "-" . $1 . "-01";
    } else {
        $self->fail("Can't find date in $date");
    }
    return $date;
}

1;
