package BookPub::Import::Importer::Scribd::Version11;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin                => 'W',
        rPurchaseType            => 'Q',
        purchaseType             => 'Q',
        priceType                => 'G',
        rAuthor                  => 'M',
        rTitle                   => 'L',
        rIsbn13                  => 'K',
        rImprint                 => 'N',
        countryCode              => 'V',
        rUnits                   => 'O',
        rUnitPrice               => 'AA',
        rListPrice               => 'F',
        rListPriceCurrency       => 'D',
        rNativeListPrice         => 'C',
        rNativeListPriceCurrency => 'D',
        rPurchasePrice           => 'C',
        rPurchasePriceCurrency   => 'D',
        rCOGS                    => 'H',
        rRevenue                 => 'I',
        currencyCode             => 'J',
        isFree                   => 'X',
        rDuration                => 'S',
        rPreviousDuration        => 'R',
        rTransactionCategory     => 'P',
    };
}

sub _headerIdentifier { rIsbn13 => 'ISBN' }

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType               { BookPub::DB::Item::Sale::kProductTypeEBook }
sub currencyCode               { uc shift->_getByFieldName('currencyCode') }
sub rDiscount                  { shift->_getByFieldName('rDiscount') || 0 }
sub isFree                     { shift->_getByFieldName('isFree') ? 'Y' : 'N' }
sub rUnits                     { sprintf "%.0f", shift->_getByFieldName('rUnits') }

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

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

    my $rListPriceCurrency = $self->_getByFieldName('rListPriceCurrency');
    return $self->listPriceRequired
        ? $self->currencyCode
        : $rListPriceCurrency;
}

sub purchaseType {
    my $self = shift;

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

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

sub priceType {
    my $self = shift;

    my $type     = $self->_getByFieldName('priceType') || '';
    my $clientID = Common::RSApp::GetClientID;

    if ( $type =~ /^none$/i ) {
        if ( $clientID =~ /^318$/ ) {                     # MMUS
            return $self->countryCode =~ /^(?:US|CA)$/i ? 'agency' : 'rrp';
        } elsif ( $clientID =~ /^(?:314|328|362)$/ ) {    # Bloomsburry
            return 'rrp';
        }
    } elsif ( $type =~ /^list$/i ) {
        return 'rrp';
    } elsif ( $type =~ /^agency$/i ) {
        return 'agency';
    }

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


1;
