package BookPub::Import::Importer::eCampus;

use strict;

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

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            dateBegin      => 'A',
            rAuthor        => 'F',
            rTitle         => 'E',
            rIsbn13        => 'C',
            rInstitution   => 'Q',
            rState         => 'O',
            rPostalCode    => 'P',
            rUnits         => 'G',
            rUnitPrice     => 'H',
            rListPrice     => 'J',
            rPurchasePrice => 'I',
            rRevenue       => 'H',
            rDuration      => 'D',
        },
    );

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

sub purchaseType           { BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL }
sub productType            { BookPub::DB::Item::Sale::kProductTypeEBook }
sub priceType              { 'rrp' }
sub currencyCode           { 'USD' }
sub rListPriceCurrency     { 'USD' }
sub rPurchasePriceCurrency { 'USD' }

sub _dateFormat { [ 'us', 'iso' ] }
sub _autoParseTitleAndSubtitle { 1 }
sub _useIsSaleRec              { 1 }

sub _processLine {
    my $self  = shift;
    my $title = $self->_getByFieldName('rTitle');

    if (   !$self->rAuthor()
        && !$self->rTitle()
        && !$self->rIsbn13()
        && !$self->rInstitution()
        && !$self->rState()
        && !$self->rPostalCode()
        && !$self->rListPrice() ) {

        $self->{_endOfData} = 1;
        return;
    }

    return $self->SUPER::_processLine(@_);
}

sub _isSaleRec {
    my $self = shift;

    # If we've set the end of data flag in processLine, we are done.
    return if ( $self->{_endOfData} );

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

sub dateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin');

    # the date is being returned in the "days since 1/1/1900" format, so we need
    # to convert it.  We'll check if it's a 5 digit number and greater than the
    # number of days since 1/1/1900 on 1/1/2006, which is 38716
    if ( $date =~ /^\d{5}$/ and $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }

    if ( $date =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/ ) {

        my $startDate = sprintf( "%04d-%02d-%02d", $3, $1, $2 );
        my $dateObj = new Common::Date( date => $startDate, type => 'iso' );

        return $dateObj->monthBegin();
    }
}

sub countryCode {
    my $self = shift;

    # Fail if there is not a US state in state field

    my $state          = $self->SUPER::rState();
    my $postalObject   = Common::Postal::US->new();
    my $stateName      = $postalObject->getName($state);
    my $caPostalObject = Common::Postal::CA->new();
    my $provinceName   = $caPostalObject->getName($state);

    if ($stateName) {
        return 'US';
    } elsif ($provinceName) {
        return 'CA';
    } else {
        $self->fail("$state is not a US state or CA province");
    }
}

1;
