package BookPub::Import::Importer::RedShelf::Version15;

use strict;

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

sub _fieldMap {
    {
        dateBegin            => 'B',
        rPurchaseType        => 'T',
        purchaseType         => 'T',
        rProductType         => 'S',
        rAuthor              => 'F',
        rTitle               => 'E',
        rIsbn13              => 'G',
        rImprint             => 'D',
        rInstitution         => 'C',
        rState               => 'Y',
        rPostalCode          => 'Z',
        countryCode          => 'AA',
        rClientProductID     => 'H',
        serviceProductID     => 'I',
        rUnits               => 'N',
        rListPrice           => 'J',
        rPurchasePrice       => 'K',
        rDiscount            => 'L',
        rRevenue             => 'N',
        rTaxAmount           => 'P',
        rFee                 => 'M',
        isFree               => 'AD',
        rDuration            => 'T',
        rDeliveryMethod      => 'W',
        rPromoCode           => 'AG',
        rChannel             => 'AI',
        rTransactionCategory => 'R',
    }
}

sub _unverifiedFieldMap {
    {
        publisherNetCredit => 'Q',
        status             => 'S',
        subtotal           => 'A',
        unusedIsbn         => 'J',
    }
}

sub rServiceName { 'RedShelf' }

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('rProductType');

    if ( $type =~ /^(?:accesscode|digital|ebook|courseware|classroom)$/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ( $type =~ /^(?:print|pod)$/i ) {
        return BookPub::DB::Item::Sale::kProductTypePhysicalBook;
    } else {
        return $self->SUPER::productType();
    }
}

sub rChannel {
    my $self    = shift;
    my $channel = $self->_getByFieldName('rChannel');
    my $country = $self->countryCode() || "";

    $self->fail("Channel not found") unless $channel;

    if ($country !~ /^US/i && $self->filename =~ /^MacMillan Trade/i) {
        if ($channel =~ /^equitable access$/i) {
            $self->fail("Found non-US 'Equitable Access' sale. Country: '$country'");
        } elsif ($channel =~ /^highereducation$/i) {
            $self->fail("Found non-US 'HIGHEREDUCATION' sale. Country: '$country'");
        } elsif ($channel =~ /^inclusive$/i) {
            $self->fail("Found non-US 'INCLUSIVE' sale. Country: '$country'");
        }
    }

    return $channel;
}

sub rState {
    my $self = shift;

    my $state = $self->_getByFieldName('rState');
    $state = 'Alabama' if $state =~ /^Alaba$/i;

    return $state;
}

sub rUnits {
    my $self = shift;

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

sub isFree {
    my $self = shift;

    my $rListPrice         = $self->rListPrice();
    my $isFree             = $self->_getByFieldName('isFree') || '';
    my $publisherNetCredit = $self->publisherNetCredit();

    return 'Y' if !$self->rRevenue && !$self->rPurchasePrice;

    if ( $isFree =~ /^(PROF|TA)$/i && !$rListPrice && !$publisherNetCredit ) {
        return  'Y';
    }

    return 'N';
}

sub rListPrice {
    my $self = shift;

    my $rListPrice = $self->_getByFieldName('rListPrice') || 0;
    $rListPrice = $self->_correctFormat($rListPrice);

    return $rListPrice;
}

sub rRevenue {
    my $self = shift;

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

    return $rRevenue;
}

sub rPurchasePrice {
    my $self = shift;

    my $rPurchasePrice = $self->_getByFieldName('rPurchasePrice') || 0;
    $rPurchasePrice = $self->_correctFormat($rPurchasePrice);

    return $rPurchasePrice;
}

sub publisherNetCredit {
    my $self = shift;

    my $publisherNetCredit = $self->_getByFieldName('publisherNetCredit') || 0;
    $publisherNetCredit = $self->_correctFormat($publisherNetCredit);

    return $publisherNetCredit;
}

sub rTaxAmount {
    my $self = shift;

    my $rTaxAmount = $self->_getByFieldName('rTaxAmount') || 0;
    $rTaxAmount = $self->_correctFormat($rTaxAmount);

    return $rTaxAmount;
}

sub _isSaleRec {
    my $self = shift;

    my $rListPrice = $self->rListPrice();
    my $isFree = $self->_getByFieldName('isFree') || '';
    my $publisherNetCredit = $self->publisherNetCredit();

    return 0 if ($isFree =~ /^STUDENT$/i && !$rListPrice && !$publisherNetCredit);
    return 0 unless ($self->dateBegin && defined $self->rRevenue);

    return $self->SUPER::_isSaleRec();
}

1;
