package BookPub::Import::Importer::JstorIthaka::v1;

use strict;
use warnings;

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

use lib '/app/tools/bookpub/lib';
use BookPub::Tracker::Service;

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

sub _fieldMap {
    my $self = shift;

    my %map = (
        1 => {
            dateBegin                => 'B',
            priceType                => 'R',
            rAuthor                  => 'N',
            rTitle                   => 'O',
            rIsbn13                  => 'L',
            rInstitution             => 'G',
            countryCode              => 'I',
            serviceProductID         => 'M',
            rUnits                   => 'T',
            rListPrice               => 'S',
            rListPriceCurrency       => 'AC',
            rNativeListPrice         => 'S',
            rNativeListPriceCurrency => 'AC',
            rPurchasePrice           => 'V',
            rPurchasePriceCurrency   => 'AC',
            rRevenue                 => 'AA',
            currencyCode             => 'AC',
            rTaxAmount               => 'X',
        },
        2 => {
            dateBegin                => 'AD',
            priceType                => 'AA',
            rAuthor                  => 'L',
            rTitle                   => 'M',
            rIsbn13                  => 'J',
            rInstitution             => 'D',
            countryCode              => 'G',
            serviceProductID         => 'K',
            rUnits                   => 'P',
            rListPrice               => 'Q',
            rListPriceCurrency       => 'AB',
            rNativeListPrice         => 'T',
            rNativeListPriceCurrency => 'AB',
            rRevenue                 => 'Z',
            currencyCode             => 'AB',
            rTaxAmount               => 'S',
        },
    );

    return $map{ $self->version };
}

sub _unverifiedFieldMap {
    my $self = shift;

    my %map = (
        1 => {
            _inv_num => 'A',
        },
        2 => {}
    );

    return $map{ $self->version };
}

sub _headerIdentifier { rTitle => 'Title' }


sub rServiceName { 'JSTOR' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }


sub _getDateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';
    # MM/DD/YYYY
    if ( $date =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/ ) {
        return sprintf "%04d-%02d-%02d", $3, $1, $2;
    }

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

sub priceType {
    my $self = shift;

    my $type = $self->_getByFieldName('priceType') || '';
    if ( $type =~ /^Multi User Price$/i ) {
        return 'rrp';
    }

    $self->fail("Unable to determine price type from: '$type'");
}

sub rTitle {
    my $self = shift;

    my $rTitle = $self->_getByFieldName('rTitle') || '';
    unless ( $rTitle ) {
        $self->fail("Unbale to dtermine rTitle from: '$rTitle'");
    }

    return $rTitle;
}

sub countryCode {
    my $self = shift;

    my $code = $self->_getByFieldName('countryCode') || '';
    my $countryObj = Common::Country->Get($code);

    $self->fail("Unable to determine country code from: '$code'") unless $countryObj;

    return $countryObj->alpha2();
}

sub rUnitPrice {
    my $self = shift;

    if ( $self->rUnits ) {
        return $self->rRevenue / $self->rUnits;
    }

    return 0;
}

sub rUnits {
    my $self = shift;

    my $rUnits = int( $self->_getByFieldName('rUnits') || 0);
    if ( $self->rRevenue > 0 ) {
        return abs($rUnits);
    } elsif ( $self->rRevenue < 0 )  {
        return abs($rUnits) * -1;
    }

    return $rUnits;
}

sub rListPrice {
    my $self = shift;

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

sub discount {
    my $self = shift;

    # 1-(AA/S/T)
    if ( $self->rListPrice && $self->rUnits ) {
        return 1 - ( $self->rRevenue / $self->rListPrice / $self->rUnits );
    }

    $self->fail("Unable to calculte discount, since rListPrice or rUnits is zero");
}

sub rRevenue {
    my $self = shift;

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

sub currencyCode {
    my $self = shift;

    my $currencyCode = $self->_getByFieldName('currencyCode') || '';
    unless ( $currencyCode ) {
        $self->fail("Unable to determine currency code from: '$currencyCode'");
    }

    return $currencyCode;
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y': 'N';
}

sub _isSaleRec   {
    my $self = shift;

    if ( $self->version == 1 ) {
        my $invNum = $self->_getByFieldName('_inv_num') || '';
        return if $invNum =~ /^Total$/i;
    }

    return 1;
}


1;