package BookPub::Import::Importer::Scribd::Version6;

use strict;

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

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        6 => {
            purchaseType       => 'N',
            priceType          => 'H',
            currencyCode       => 'D',
            countryCode        => 'S',
            dateBegin          => 'O',
            dateEnd            => 'P',
            rIsbn13            => 'I',
            rTitle             => 'J',
            rAuthor            => 'K',
            rImprint           => 'L',
            rListPrice         => 'F',
            rListPriceCurrency => 'G',
            rRevenue           => 'C',
        },
        9 => {
            dateBegin              => 'T',
            purchaseType           => 'N',
            priceType              => 'H',
            rAuthor                => 'K',
            rTitle                 => 'J',
            rIsbn13                => 'I',
            rImprint               => 'L',
            countryCode            => 'S',
            rListPrice             => 'F',
            rListPriceCurrency     => 'G',
            rPurchasePrice         => 'E',
            rPurchasePriceCurrency => 'G',
            rRevenue               => 'C',
            currencyCode           => 'D',
            rDuration              => 'P',
            rPreviousDuration      => 'O',
            rTransactionCategory   => 'M',
        },
        12 => {
            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           => 'E',
            rPurchasePriceCurrency   => 'G',
            rRevenue                 => 'C',
            currencyCode             => 'D',
            rTaxAmount               => 'W',
            rTaxUnitPrice            => 'X',
            rDuration                => 'P',
            rPreviousDuration        => 'O',
            rModel                   => 'M',
            rTransactionCategory     => 'N',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub rServiceName  { "Scrib'd" }
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 {
    my $self = shift;

    return $self->SUPER::dateEnd() if ($self->_version == 6);

    $self->dateBegin();
}

sub isFree {
    my $self = shift;

    return 'N' if $self->_version != 12;

    return $self->_getByFieldName('rRevenue') ? 'N' : 'Y';
}

sub priceType {
    my $self = shift;

    if ( $self->version != 12 ) {
        return $self->SUPER::priceType();
    }

    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;
