package BookPub::Import::Importer::Scribd::Version13;

use strict;
use warnings;

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


sub _fieldMap {
    {
        dateBegin                => 'S',
        rPurchaseType            => 'X',   # the column may be missing
        purchaseType             => 'X',   # the column may be missing
        rAuthor                  => 'I',
        rTitle                   => 'H',
        rIsbn13                  => 'G',
        rImprint                 => 'K',
        countryCode              => 'R',
        rUnits                   => 'B',
        rListPrice               => 'E',
        rListPriceCurrency       => 'C',
        rNativeListPrice         => 'D',
        rNativeListPriceCurrency => 'F',
        rPurchasePrice           => 'W',
        rPurchasePriceCurrency   => 'F',
        rRevenue                 => 'B',
        currencyCode             => 'C',
        isFree                   => 'T',
        rDuration                => 'L',
        rPreviousDuration        => 'N',
        rModel                   => 'X',   # the column may be missing
    };
}

sub _headerIdentifier { rIsbn13 => 'ISBN'}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub rServiceName  { "Scrib'd" }
sub priceType     { 'rrp' }
sub rPurchaseType { shift->purchaseType() }
sub productType   { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub rProductType  { BookPub::DB::Item::Sale::kProductTypeAudioBook }

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 purchaseType {
    my $self = shift;

    my $type = $self->_getByFieldName('purchaseType') || '';
    if ( !$type ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    } elsif ( $type =~ /^unlimited_subscription$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    }

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

sub currencyCode {
    my $self= shift;

    my $ccode = $self->_getByFieldName('currencyCode') || '';
    return uc $ccode if $ccode;

    $self->fail("Unable to determine currencyCode from: '$ccode'");
}

sub rListPriceCurrency { shift->currencyCode() }

sub rNativeListPriceCurrency {
    my $self = shift;

    my $ccode = $self->_getByFieldName('rNativeListPriceCurrency') || '';
    return uc $ccode;
}

sub rPurchasePriceCurrency {
    my $self = shift;

    my $ccode = $self->_getByFieldName('rPurchasePriceCurrency') || '';
    return uc $ccode;
}

sub rUnits {
    my $self = shift;

    return $self->rRevenue < 0 ? -1 : 1;
}

sub rRevenue {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue') || 0;

    return $revenue;
}

sub isFree {
    my $self = shift;

    my $isFree = $self->_getByFieldName('isFree') || 0;
    if ( $isFree =~ /^free$/i || !$self->rRevenue) {
        return 'Y';
    }

    return 'N';
}


1;
