package BookPub::Import::Importer::RedShelf::Version19;

use strict;
use warnings;

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

sub _headerIdentifier { rIsbn13 => 'eISBN13' }

sub _fieldMap {
    my $self = shift;

    my %map = (
        'original_tab' => {
            dateBegin            => 'B',
            rPurchaseType        => 'V',
            purchaseType         => 'V',
            rProductType         => 'U',
            rAuthor              => 'H',
            rTitle               => 'G',
            rIsbn13              => 'I',
            rImprint             => 'F',
            rInstitution         => 'E',
            rState               => 'AA',
            rPostalCode          => 'AB',
            countryCode          => 'AC',
            rClientProductID     => 'K',
            serviceProductID     => 'J',
            rListPrice           => 'L',
            rPurchasePrice       => 'M',
            rDiscount            => 'N',
            rRevenue             => 'P',
            rTaxAmount           => 'R',
            rFee                 => 'O',
            isFree               => 'AF',
            rDuration            => 'V',
            rDeliveryMethod      => 'Y',
            rPromoCode           => 'AI',
            rChannel             => 'AK',
            rTransactionCategory => 'T',
        },
        'alternate_tab1' => {
            dateBegin            => 'B',
            rPurchaseType        => 'T',
            purchaseType         => 'T',
            rProductType         => 'S',
            productType          => 'S',
            rTitle               => 'K',
            rIsbn13              => 'L',
            rInstitution         => 'D',
            rState               => 'AF',
            rPostalCode          => 'AG',
            countryCode          => 'AH',
            rClientProductID     => 'AJ',
            rUnits               => 'I',
            rListPrice           => 'N',
            rPurchasePrice       => 'O',
            rRevenue             => 'Q',
            rFee                 => 'R',
            isFree               => 'J',
            rDuration            => 'T',
            rPromoCode           => 'AI',
            rChannel             => 'V',
            rTransactionCategory => 'U',
        }
    );

    return $self->{_tab_pattern} ? $map{ $self->{_tab_pattern} } : {} ;
}

sub _unverifiedFieldMap {
    my $self = shift;

    my %map = (
        'original_tab' => {
            subtotal           => 'A',
            unusedIsbn         => 'K',
            status             => 'T',
            publisherNetCredit => 'P', # PublisherCredit
            _user              => 'C',
            _name              => 'D',
        },
        'alternate_tab1' => {
            subtotal           => 'A',
            status             => 'U',
            publisherNetCredit => 'Q', # PublisherCredit
        },
    );

    return $self->{_tab_pattern} ? $map{ $self->{_tab_pattern} } : {} ;
}

sub rServiceName { 'RedShelf' }

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


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

sub rProductType {
    my $self = shift;

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

    if ( $type =~ /^(?:accesscode|digital|ebook|courseware)$/i ) {
        return 'eBook';
    } elsif ( lc($type) eq 'print' ) {
        return 'POD';
    }

}

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

    my $discount = $self->_getByFieldName('rDiscount') || 0;

    return $discount;
}

sub rUnits {
    my $self = shift;

    if ( $self->{_tab_pattern} eq 'original_tab' ) {
        return $self->rRevenue < 0 ? -1 : 1;
    } elsif ( $self->{_tab_pattern} eq 'alternate_tab1' ) {
        my $rUnits = $self->_getByFieldName("rUnits") || 0;
        return $rUnits;
    }
}

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

    my $type = $self->_getByFieldName('purchaseType') || '';
    if ( lc $type eq 'lifetime' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    }

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

sub _isSaleRec {
    my $self = shift;

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

    if ( $self->{_tab_pattern} eq 'original_tab' ) {
        my $user = $self->_getByFieldName("_user") || '';
        my $name = $self->_getByFieldName("_name") || '';

        $self->fail("Contains consumer privacy data") if ($user || $name);
    }

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

    return 1;
}


sub _processSheet {
    my ( $self, $sheet, $sheetnum ) = @_;

    my $limit = 10;
    foreach my $aRow ( @$sheet ) {
        # we are trying to determine which tab we will be working with
        my $colA = $$aRow[0] || '';  # col. A
        my $colB = $$aRow[1] || '';  # col. B

        if ( $colA =~ /^ID$/i && $colB =~ /^Date$/i ) {
            $self->{_tab_pattern} = 'original_tab';
            last;
        } elsif ( $colA =~ /^Journal ID$/i && $colB =~ /^Transaction Date$/i ) {
            $self->{_tab_pattern} = 'alternate_tab1';
            last;
        }

        last unless --$limit;
    }

    Common::Log::Print("TAB PATTERN: '$self->{_tab_pattern}'");

    return $self->SUPER::_processSheet($sheet, $sheetnum);
}



1;
