package BookPub::Import::Importer::Scribd::Version3;

use strict;

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

sub _headerIdentifier { ( rIsbn13 => 'ISBN' ) }

sub productType { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rUnits      { 1 }

sub _fieldMap {
    {
        purchaseType => 'M',
        priceType    => 'G',
        currencyCode => 'F',
        countryCode  => 'P',
        dateBegin    => 'N',
        dateEnd      => 'O',
        rIsbn13      => 'H',
        rTitle       => 'I',
        rAuthor      => 'J',
        rListPrice   => 'E',
        rRevenue     => 'C',
    };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub purchaseType {
    my $self = shift;
    my $type = lc( $self->_getByFieldName('purchaseType') );

    if ( $type eq 'read' || $type eq 'browse' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    } else {
        $self->fail( "Unexpected purchase type of " . $type . " encountered" );
    }
}

sub priceType {
    my $self = shift;

    my $clientID = Common::RSApp::GetClientID();

    if ( $clientID == 318 ) {    # Macmillan US
        my $countryCode = $self->countryCode();

        if ( $countryCode eq 'US' || $countryCode eq 'CA' ) {
            return 'agency';
        } else {
            return 'rrp';
        }
    } else {
        my $type = lc( $self->_getByFieldName('priceType') );

        if ( $type eq 'rrp' || $type eq 'agency' ) {
            return $type;
        } elsif ( $type eq 'list' || $type eq 'none' || !$type ) {
            return 'rrp';
        } else {
            $self->fail( "Unexpected price type of '" . $type . "' encountered" );
        }
    }
}

sub rListPriceCurrency {
    my $self = shift;
    my $code;

    $code = $self->_getByFieldName('rListPriceCurrency');

    return uc($code);
}

sub currencyCode {
    my $self = shift;
    my $code;

    $code = $self->_getByFieldName('currencyCode');

    return uc($code);
}

1;
