package BookPub::Import::Importer::EBSCO::Version4;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Country;

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

sub _fieldMap {
    # SERVICE: 80, VERSION: 4
    {
        dateBegin                => 'L',
        dateEnd                  => 'L',
        rPurchaseType            => 'J',
        purchaseType             => 'J',
        rAuthor                  => 'F',
        rTitle                   => 'E',
        rIsbn13                  => 'D',
        rImprint                 => 'A',
        rInstitution             => 'G',
        countryCode              => 'H',
        rUnits                   => 'Q',
        rAdjustments             => 'J',
        rListPrice               => 'M',
        rListPriceCurrency       => 'N',
        rDiscount                => 'W',
        rRevenue                 => 'X',
        currencyCode             => 'H13',
        rDuration                => 'K',
        rModel                   => 'K',
        rTransactionCategory     => 'J',
        rNativeListPrice         => 'M',
        rNativeListPriceCurrency => 'N',
    };
}

sub _unverifiedFieldMap {
    {
        rListPriceUSD            => 'P',
        adjListPrice             => 'V',
        altCurrencyCode          => 'H10',
        consortia                => 'U',
    };
}

sub _headerIdentifier { rRevenue => 'ROYALTY DUE' }
sub rProductType      { 'eBook' }
sub priceType         { 'RRP' }
sub _useIsSaleRec     { 1 }
sub _isSaleRec        { $_[0]->SUPER::_isSaleRec() if !$_[0]->{_endOfData} }

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

sub currencyCode {
    my $self = shift;

    my $code = $self->_getByFieldName("currencyCode") || "";
    ($code = $self->_getByFieldName("altCurrencyCode") || "") unless $code;
    return $code if ($code =~ /^\w{3}$/);

    $self->fail("Cannot determine currency code from '$code'");
}

sub _processLine {
    my $self = shift;

    my $discount = $self->_getByFieldName('rDiscount') || 0;
    $self->{_endOfData} = 1 if $discount =~ /Total/i;

    return $self->SUPER::_processLine(@_);
}

sub rListPriceCurrency {
    my $self = shift;

    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency') || '';
    my $revenueCurrency   = $self->currencyCode;

    # for Macmillan, Wiley, and RLPG clients
    if ( $self->isMacmillan || $self->isWiley || $self->isRLPG ) {
        $listPriceCurrency = $revenueCurrency;
    }

    return $listPriceCurrency;
}

sub rListPrice {
    my $self = shift;

    my $consortia = $self->_getByFieldName('consortia') || '';
    my $adjListPrice = $self->_getByFieldName('adjListPrice');
    my $adj = $self->_getByFieldName('rAdjustments') || '';

    my $listPrice = $self->_getByFieldName('rListPrice') || '';
    $listPrice = $adjListPrice if ($adj !~ /^ADJ$/i && $adjListPrice && $consortia =~ /^SUPC$/i);

    # for Macmillan, Wiley, and RLPG clients
    if ( $self->isMacmillan || $self->isWiley || $self->isRLPG ) {
        $listPrice = $self->_getByFieldName('rListPriceUSD') || '';
        $listPrice = $adjListPrice if ($adj !~ /^ADJ$/i && $adjListPrice && $consortia =~ /^SUPC$/i);
    }

    return $listPrice;
}

sub rDuration {
    my $self = shift;

    my $duration = $self->_getByFieldName('rDuration') || '';
    return $duration =~ /^pdl(.*)$/i ? $1 : $duration;
}

sub countryCode {
    my $self = shift;

    my $code = $self->_getByFieldName('countryCode') || '';
    if ( my $oCountry = Common::Country->Get($code) ) {
        return $oCountry->alpha2();
    }

    return $self->fail("Unknown country code '$code'");
}

sub rUnits {
    my $self = shift;

    my $units = $self->_getByFieldName('rUnits') || 0;
    return sprintf "%d", $units;
}

sub purchaseType {
    my $self = shift;

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

    if ( $purchaseType =~ /pdl/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    }
    elsif ( $purchaseType =~ /purchase|upgrade|pdo/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    }
    elsif ( $purchaseType =~ /adj|cancellation/i ) {
        my $model = $self->_getByFieldName('rModel') || '';

        if ( $model =~ /pdl(1|7|14|28)|cam325|cam365|cam\[#\]/i ) {
            return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
        }
        if ( $model =~ /1b[13u]u/i ) {
            return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
        }

        $self->fail("Unknown model '$model'");
    }

    $self->fail("Unknown purchase type '$purchaseType'");
}

sub isMacmillan {
    Log->debug( "Client ID: " . Common::RSApp::GetClientID() );
    return ( Common::RSApp::GetClientID() == 318 ) ? 1 : 0;
}

sub isWiley {
    Log->debug( "Client ID: " . Common::RSApp::GetClientID() );
    return ( Common::RSApp::GetClientID() == 324 ) ? 1 : 0;
}

sub isRLPG {
    Log->debug( "Client ID: " . Common::RSApp::GetClientID() );
    return ( Common::RSApp::GetClientID() == 346 ) ? 1 : 0;
}


1;
