package BookPub::Import::Importer::Scribd::Version14;

use strict;

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

sub _fieldMap {
    {
        dateBegin                => 'T',
        purchaseType             => 'N',
        priceType                => 'H',
        rAuthor                  => 'K',
        rTitle                   => 'J',
        rIsbn13                  => 'I',
        rImprint                 => 'L',
        countryCode              => 'S',
        rListPrice               => 'F',
        rListPriceCurrency       => 'D',
        rNativeListPrice         => 'X',
        rNativeListPriceCurrency => 'G',
        rPurchasePrice           => 'AB',
        rPurchasePriceCurrency   => 'G',
        rRevenue                 => 'C',
        currencyCode             => 'D',
        rTaxAmount               => 'W',
        rTaxUnitPrice            => 'X',
        rUnitPrice               => 'C',
        rDuration                => 'M',
        rPreviousDuration        => 'O',
        rTransactionCategory     => 'N',
        rPromoCode               => 'Y',
    }
}

sub rServiceName  { "Scribd" }
sub rPurchaseType { $_[0]->purchaseType() }

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';

    if ( $date =~ /^\d{5}$/) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( 'yyyy-mm-dd', $date );
        return $date;
    }

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

sub dateEnd { shift->dateBegin() }

sub rUnits {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue') || 0;
    return $revenue < 0 ? -1 : 1;
}

sub isFree {
    my $self = shift;

    return ((!$self->rRevenue || $self->rRevenue =~ /^0\.0+$/) && $self->rPromoCode =~ /^free$/i)  ? 'Y' : 'N';
}

sub priceType {
    my $self = shift;

    my $type = $self->_getByFieldName('priceType') || '';
    if ( !$type || $type =~ /^01 Wholesale Excl\. Tax$/i ) {
        return 'rrp';
    } elsif ( $type =~ /^02 Wholesale Incl\. Tax$/i ) {
        return 'rrp+tax';
    }

    $self->fail( "Unexpected price type of '" . $type . "' encountered" );
}

1;
