package BookPub::Import::Importer::RedShelf::Version16;

use strict;

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

sub _fieldMap {
    {
        dateBegin            => 'B',
        rPurchaseType        => 'K',
        purchaseType         => 'K',
        rProductType         => 'J',
        priceType            => 'W',
        rAuthor              => 'E',
        rTitle               => 'D',
        rIsbn13              => 'F',
        rImprint             => 'F',
        rInstitution         => 'C',
        rState               => 'R',
        rPostalCode          => 'S',
        countryCode          => 'T',
        rClientProductID     => 'G',
        rUnits               => 'H',
        rUnitPrice           => 'H',
        rRevenue             => 'H',
        rTaxAmount           => 'M',
        isFree               => 'H',
        rDuration            => 'K',
        rDeliveryMethod      => 'O',
        rTransactionCategory => 'I',
    }
}

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

sub rServiceName  { 'RedShelf' }
sub currencyCode  { 'USD' }
sub rProductType  { shift->productType }
sub rListPriceCurrencyCode { 'USD' }
sub _useIsSaleRec { 1 }

sub _getDateBegin {
    my $self = shift;

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

    # MM/DD/YYYY
    if ( my ($m, $d, $y) = $dateBegin =~ /(\d{1,2})\D(\d{1,2})\D(\d{4})/ ) {
        ($m, $d) = ($d, $m) if $m > 12;
        return sprintf "%04d-%02d-%02d", $y, $m, $d;
    }
    # YYYY-DD-MM (see RSD-11116)
    elsif ( ($y, $d, $m) = $dateBegin =~ /(\d{4})\D(\d{1,2})\D(\d{1,2})/ ) {
        return sprintf "%04d-%02d-%02d", $y, $m, $d;
    }

    $self->fail("Unable to deterine date begin from: '$dateBegin'");
}

sub rListPrice {
    my $self = shift;

    return $self->rUnitPrice() if (Common::RSApp::GetClientID =~ /^(?:346|324|318)$/ );    # Macmillan, Wiley, RLPG

    $self->SUPER::rListPrice();
}

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

    if ( $type =~ /^(?:accesscode|digital|ebook)$/i || $type =~ /^(?:Classroom|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 rChannel {
    return "HIGHEREDUCATION" if (Common::RSApp::GetClientID() == 365);
}

sub countryCode {
    my $self = shift;

    my $country = $self->_getByFieldName('countryCode') || '';
    if ( !$country && Common::RSApp::GetClientID =~ /^(?:314|361|346|365|624|)$/ ) {  # BBUS(A), RLPG, SAGE, BBUK
        return 'US';
    }

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

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

    my $state = $self->_getByFieldName('rState') || "";
    $state =~ s/[^A-Z]//gi;
    $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 $rRevenue = $self->_getByFieldName('rRevenue') || 0;

    return ($rRevenue ? 'N' : 'Y');
}

sub rRevenue {
    my $self = shift;

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

    return $rRevenue;
}

sub rTaxAmount {
    my $self = shift;

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

    return $rTaxAmount;
}

sub _isSaleRec {
    my $self    = shift;

    return unless ($self->_getByFieldName('dateBegin') && $self->_getByFieldName('rTitle'));

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

1;
