package BookPub::Import::Importer::CourseSmart::Version6;

use strict;

use Date::Calc qw(Days_in_Month);
use Data::Dumper;

use lib '/app/tools/common/lib';

use lib '/app/tools/bookpub/lib';

use base 'BookPub::Import::Importer';

sub _fieldMap {
    {
        purchaseType => 'G',
        rState       => 'I',
        rPostalCode  => 'J',
        dateBegin    => 'E',
        isbn         => 'N',
        rTitle       => 'R',
        rAuthor      => 'Q',
        rUnits       => 'U',
        rRevenue     => 'W',
    };
}

sub _headerIdentifier { ( dateBegin => 'Transaction Date' ) }

sub priceType     { 'rrp' }
sub currencyCode  { 'USD' }
sub countryCode   { 'US' }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub _dateFormat   { [ 'numerical_year_first', 'iso' ] }
sub _useIsSaleRec { 1 }
sub rDiscount     { 0.30 }

sub revenue {
    my $self = shift;

    my $revenue  = $self->_getByFieldName('rRevenue');
    my $discount = $self->rDiscount;

    return $revenue * ( 1 - $discount );
}

sub purchaseType {
    my $self = shift;
    my $type = $self->SUPER::purchaseType();

    # an Unredeemed purchaseType is a return, so we're treating it as a subscription download
    if ( lc($type) eq 'subscription' || lc($type) eq 'unredeemed' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    } else {
        $self->fail("Unexpected purchase type: $type");
    }
}

# hash of two-letter Canadian province abbreviations
my %provinces = (
    'AB' => 1,
    'BC' => 1,
    'MB' => 1,
    'NB' => 1,
    'NL' => 1,
    'NT' => 1,
    'NS' => 1,
    'NU' => 1,
    'ON' => 1,
    'PE' => 1,
    'QC' => 1,
    'SK' => 1,
    'YT' => 1,
);

sub state {
    my $self = shift;
    my $state;
    if ( my $rState = $self->rState() ) {
        if ( my $postal = Common::Postal->new( $self->countryCode() ) ) {
            if ( $postal->getName($rState) ) {

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

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

            if ( !$state ) {

                # if the state is a Canadian province, return the abbreviation for it
                if ( $provinces{$rState} ) {
                    return $rState;
                }

                # Apple seems to run with PI right now, an obsolete code for Philippine
                # Islands. We'll let that slide (but we won't normalize with it either).
                if ( uc($rState) ne 'PI' ) {
                    $self->fail( "Invalid state '$rState' (country: " . $self->countryCode . ")" );
                }
            }
        }
    }

    return $state;
}

sub postalCode {
    my $self = shift;
    my $postalCode;

    if ( my $rPostalCode = $self->rPostalCode() ) {
        if ( $rPostalCode =~ /^(\d{5})-$/ ) {
            $rPostalCode = $1;
        }

        if ( my $postal = Common::Postal->new( $self->countryCode() ) ) {
            $postalCode = $postal->formatPostalCode($rPostalCode);

            if ( !$postalCode ) {
                return;

                # Rather than throwing an error, we'll just leave out the invalid zip codes.
                #$self->fail("Invalid postal code '$rPostalCode'");
            }
        }
    }

    return $postalCode;
}

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

    if ( $state eq 'UNKNOWN' ) {
        return;
    }

    return $state;

}

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

    if ( $code eq 'UNKNOWN' ) {
        return;
    }

    return $code

}

###
1;    # Play nicely.
###
