package BookPub::Import::Importer::FollettHigherEducation;

use strict;

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

sub _headerIdentifier { ( rTitle => 'Title' ) }

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        12 => {
            countryCode => 'AH',
            rState      => 'Z',
            rPostalCode => 'AA',
            dateBegin   => 'K',
            rIsbn10     => 'P',
            rIsbn13     => 'O',
            rTitle      => 'S',
            rAuthor     => 'T',
            rSales      => 'L',
            rReturns    => 'L',
            rListPrice  => 'AB',
            rRevenue    => 'AC',
        },
        13 => {
            countryCode  => 'AB',
            rState       => 'S',
            rPostalCode  => 'T',
            dateBegin    => 'E',
            rIsbn13      => 'J',
            rTitle       => 'N',
            rAuthor      => 'M',
            rSales       => 'W',
            rReturns     => 'W',
            rListPrice   => 'X',
            rRevenue     => 'Y',
            rInstitution => 'Q',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        12 => {
            stateTax    => 'AJ',
            countyTax   => 'AL',
            cityTax     => 'AN',
            districtTax => 'AP',
        },
        13 => {
            stateTax    => 'AD',
            countyTax   => 'AF',
            cityTax     => 'AH',
            districtTax => 'AJ',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType        { BookPub::DB::Item::Sale::kProductTypeEBook }
sub priceType          { 'agency' }
sub currencyCode       { 'USD' }
sub rListPriceCurrency { 'USD' }

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub rTaxAmount {
    my $self = shift;
    my $taxAmount;

    if ( defined $self->_getByFieldName('stateTax') ) {
        $taxAmount += $self->_getByFieldName('stateTax');
    }

    if ( defined $self->_getByFieldName('countyTax') ) {
        $taxAmount += $self->_getByFieldName('countyTax');
    }

    if ( defined $self->_getByFieldName('cityTax') ) {
        $taxAmount += $self->_getByFieldName('cityTax');
    }

    if ( defined $self->_getByFieldName('districtTax') ) {
        $taxAmount += $self->_getByFieldName('districtTax');
    }

    $taxAmount = abs($taxAmount);
    $taxAmount *= -1 if ( $self->revenue < 0 );

    return $taxAmount;
}

sub rTitle {
    my $title = shift->SUPER::rTitle;

    if ( $title =~ /^(.+)\(ebook\)$/ ) {
        $title = $1;
    }

    return $title;
}

sub rSubtitle {
    my $subtitle = shift->SUPER::rSubtitle;

    if ( $subtitle =~ /^(.+)\(ebook\)$/ ) {
        $subtitle = $1;
    }

    return $subtitle;
}

sub rPostalCode {
    my $code = shift->SUPER::rPostalCode;

    return $code eq '0' ? undef : $code;
}

sub rState {
    my $self   = shift;
    my $rState = $self->SUPER::rState;

    my $state;

    # We're going to attempt to find the right abbreviation for the state.
    # But if we don't find one, we'll just leave it blank and keep going.
    if ($rState) {
        my $countryCode = $self->countryCode();

        if ( $countryCode && length($countryCode) != 2 ) {
            my $countryObj = Common::Country->Get($countryCode);
            if ($countryObj) {
                $countryCode = $countryObj->alpha2();
            }
        }

        if ( my $postal = Common::Postal->new($countryCode) ) {
            if ( $postal->getName($rState) ) {

                # acceptable state abbreviation
                $state = $rState;
            } else {

                # look up by the full state name
                $state = $postal->getAbbr($rState);
            }
        }
    }

    return !$state || $state eq 'NULL' || $state eq '(blank)' ? undef : $state;
}

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

    if ( $type eq 'Sale' || $type eq 'Sale Correction' || $type eq 'Sale CorrectionSale' ) {
        return 1;
    }
    return;
}

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

    if ( $type eq 'Refund' || $type eq 'Sale Reversal' || $type eq 'Sale ReversalRefund' || $type eq 'Refund Correction' ) {
        return 1;
    }
    return;
}

sub units {
    my $self = shift;

    if ( $self->rSales() ) {
        return 1;
    } elsif ( $self->rReturns() ) {
        return -1;
    } else {
        $self->fail("Can't determine units");
    }
}

1;
