package BookPub::Import::Importer::GrandCanyonUniv;

use strict;

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

sub _headerIdentifier { ( rIsbn13 => 'Print Text ISBN #' ) }

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

    my %fieldMap = (
        1 => {
            dateBegin  => 'L1',
            rAuthor    => 'E',
            rTitle     => 'D',
            rIsbn13    => 'I',
            rUnits     => 'L',
            rUnitPrice => 'K',
            rListPrice => 'J',
            rRevenue   => 'M',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub priceTypeQualifierID       { BookPub::DB::Item::PriceTypeQualifier::kCorporate }      # Library
sub priceType                  { 'rrp' }
sub rInstitution               { 'Grand Canyon University' }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub countryCode                { 'US' }
sub currencyCode               { 'USD' }
sub rListPriceCurrency         { 'USD' }
sub rDiscount                  { '0.3003' }
sub _dateFormat                { [ 'numerical', 'quarter', 'iso' ] }

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

    if ( !$author && !$title && !$isbn ) {
        $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 _getDateBegin {
    my $self = shift;
    $self->{_dateBegin} || $self->_setDates;
    return $self->{_dateBegin};
}

sub _getDateEnd {
    my $self = shift;
    $self->{_dateEnd} || $self->_setDates;
    return $self->{_dateEnd};
}

sub _setDates {
    my $self = shift;
    my ( $begin, $end );
    my $startDate = $self->_getByFieldName('dateBegin');

    if ( $startDate =~ /(Q[1234]) (\d{4})/ ) {
        ( $begin, $end ) = $self->_getDates( date_begin => $2 . $1, type => 'quarter' );
    } else {
        $self->fail("Cannot get date from header: $startDate");
    }

    $self->{_dateBegin} = $begin;
    $self->{_dateEnd}   = $end;
}

1;
