package BookPub::Import::Importer::CourseSmart::Version10;

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',
        rProductType => 'Y',
        rIsbn10      => 'M',
        rIsbn13      => 'N',
        rTitle       => 'R',
        rAuthor      => 'Q',
        rImprint     => 'F',
        rUnits       => 'U',
        rListPrice   => 'W',
        rDiscount    => 'Z',
        rRevenue     => 'AA',
    };
}

sub _headerIdentifier { ( rImprint => 'Imprint_Name' ) }

sub priceType     { 'rrp' }
sub currencyCode  { 'USD' }
sub _dateFormat   { [ 'numerical_year_first', 'iso' ] }
sub _useIsSaleRec { 1 }

sub countryCode {
    my $self     = shift;
    my $filename = $self->filename;

    if ( $filename =~ /US/ && $filename !~ /Canada/ ) {
        return "US";
    } elsif ( $filename =~ /Canada/ && $filename !~ /US/ ) {
        return "CA";
    } else {
        $self->fail("Could not find country code in filename: $filename");
    }
}

sub productType { BookPub::DB::Item::Sale::kProductTypeEBook }

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

    if ( lc($type) eq 'subscription' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } 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() ) {
        my $code = $self->countryCode();
        if ( $code eq "US" ) {
            my $postal = Common::Postal->new($code);
            if ( $postal->getName($rState) ) {

                # acceptable state abbreviation
                $state = $rState;
            }
        } elsif ( $code eq "CA" ) {
            if ( $provinces{$rState} ) {
                $state = $rState;
            }
        } else {
            $self->fail("Invalid country code: $code");
        }
    }
    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.
###
