package BookPub::Import::Importer::RecordedBooks::Version4;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin                => 'A3',
        rProductType             => 'A2',
        rAuthor                  => 'E',
        rTitle                   => 'D',
        rIsbn13                  => 'B',
        countryCode              => 'N',
        serviceProductID         => 'B',
        rUnits                   => 'F',
        rDiscount                => 'K',
        rRevenue                 => 'M',
        currencyCode             => 'M',
        rListPrice               => 'G',
        rListPriceCurrency       => 'G',
        rNativeListPrice         => 'I',
        rNativeListPriceCurrency => 'I',
    };
}

sub _unverifiedFieldMap {
    {
        currencyConversion  => 'L',
    }
}

sub _headerIdentifier { rTitle => 'TITLE' }

my %months = (
    jan => 1,
    feb => 2,
    mar => 3,
    apr => 4,
    may => 5,
    jun => 6,
    jul => 7,
    aug => 8,
    sep => 9,
    oct => 10,
    nov => 11,
    dec => 12,
);

sub _useIsSaleRec        { 1 }
sub _dateFormat          { 'iso' }
sub _dateNormalization   { 'month' }
sub priceType            { 'rrp' }
sub productType          { $_[0]->rProductType }
sub currencyCode         { $_[0]->{_currency_code} }
sub purchaseType         { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceTypeQualifierID {  BookPub::DB::Item::PriceTypeQualifier::kCorporate } # Library

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

sub dateBegin {
    my $self = shift;

    return $self->{_begin_date} if $self->{_begin_date};

    my $date = $self->_getByFieldName('dateBegin') || '';
    if ( my ($month, $year) = $date =~ /([a-z]{3}).*[ -_](\d{4})$/i ) {
        $month = $months{lc $month} if exists $months{lc $month};
        return $self->{_begin_date} = sprintf "%04d-%02d-%02d", $year, $month, 1 ;
    }

    $self->fail("Unable to determine dateBegin from: '$date'.");
}

sub rProductType {
    my $self = shift;

    my $type = $self->_getByFieldName('rProductType') || '';
    return BookPub::DB::Item::Sale::kProductTypeAudioBook if $type =~ /^eAudio Digital Download Earnings$/i;
    return BookPub::DB::Item::Sale::kProductTypeEBook     if $type =~ /^eBook Digital Download Earnings$/i;

    $self->fail("Unable to determine rProductType from: '$type'.")
}

sub rListPrice {
    my $self = shift;

    my ($rListPrice, $rListPriceCurrency) = $self->_rListPriceItems;

    if ( $self->listPriceRequired && $rListPriceCurrency ne $self->currencyCode ) {
        $rListPrice *= $self->_getByFieldName('currencyConversion');
    }

    return $rListPrice;
}

sub rListPriceCurrency {
    my $self = shift;

    my ($rListPrice, $rListPriceCurrency) = $self->_rListPriceItems;

    if ( $self->listPriceRequired && $rListPriceCurrency ne $self->currencyCode ) {
        $rListPriceCurrency = $self->currencyCode;
    }

    return $rListPriceCurrency;
}

sub rNativeListPriceCurrency { $_[0]->{_native_currency} }

sub _rListPriceItems {
    my $self = shift;

    my $rListPrice       = $self->_getByFieldName('rListPrice')       || 0;
    my $rNativeListPrice = $self->_getByFieldName('rNativeListPrice') || 0;

    return $rListPrice
        ? ( $rListPrice, $self->{_currency} )
        : ( $rNativeListPrice, $self->{_native_currency} );
}

sub _processHeader {
    my $self = shift;

    return unless $self->_isHeader;

    my $rListPrice = $self->_getByFieldName('rListPrice') || '';
    $self->{_currency} = $rListPrice =~ /^US.*/i
        ? 'USD'
        : $self->fail("Unable to determine rListPriceCurrency from: '$rListPrice'.");

    my $rNativeListPrice = $self->_getByFieldName('rNativeListPrice') || '';
    $self->{_native_currency} = $rNativeListPrice =~ /^CA.*/i
        ? 'CAD'
        : $self->fail("Unable to determine rNativeListPriceCurrency from: '$rNativeListPrice'.");

    my $currencyCode = $self->_getByFieldName('currencyCode') || '';
    $self->{_currency_code} = $currencyCode =~ /.*\((\w{3})\)$/i
        ? $1
        : $self->fail("Unable to determine currencyCode from: '$currencyCode'.");

    return 1;
}

sub _isSaleRec {
    my $self = shift;

    return $self->rIsbn13 && $self->rTitle && $self->rDiscount && $self->rAuthor;
}


1;