package BookPub::Import::Importer::ApolloGroup;

use strict;

use Data::Dumper;
use Date::Simple;

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

sub _fieldMap {
    my $self     = shift;
    my %fieldMap = (
        1 => {
            dateBegin    => 'A',
            rProductType => 'G',
            rTitle       => 'F',
            rIsbn13      => 'D',
            rUnits       => 'L',
            isFree       => 'K',
            rRevenue     => 'M',
        },
        2 => {
            dateBegin    => 'A',
            rProductType => 'G',
            rTitle       => 'F',
            rIsbn13      => 'D',
            rUnits       => 'J',
            rUnitPrice   => 'H',
            rListPrice   => 'H',
            isFree       => 'H',
            rRevenue     => 'K',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            price            => 'H',
            chapterBookPrice => 'K',
        },
        2 => {
            price            => 'H',
            chapterBookPrice => 'H',
        },
    );

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

sub _headerIdentifier { ( rIsbn13 => 'ISBN' ) }
sub _useIsSaleRec { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _dateFormat                { [ 'iso', 'us' ] }
sub _dateNormalization         { 'month' }

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType    { 'rrp' }

sub countryCode            { 'US' }
sub rListPriceCurrency     { 'USD' }
sub rPurchasePriceCurrency { 'USD' }
sub rInstitution           { 'Apollo Group' }
sub rDiscount              { 0 }
sub currencyCode           { 'USD' }

sub productType {
    my $self = shift;
    my $type = lc( $self->rProductType ) || return;

    if ( $type =~ /E-Book/i || $type =~ /Podcast/i || $type =~ /Video/i || $type =~ /EPUB3-VitalSource/i ) {
        $type = BookPub::DB::Item::Sale::kProductTypeEBook;
    } else {
        $self->fail("Unexpected product type: $type");
    }

    return $type;
}

sub _processLine {
    my $self    = shift;
    my $revenue = lc( $self->rRevenue );

    if ( $revenue =~ /Q\d{1} Total/i ) {

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

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

sub _isSaleRec {
    my $self  = shift;
    my $units = $self->_getByFieldName('rUnits');

    return if ( $units == 0 );

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

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

sub rUnitPrice {
    my $self  = shift;
    my $price = $self->_getByFieldName('price');

    if ( defined $self->_getByFieldName('chapterBookPrice') ) {
        my $chapterBookPrice = $self->_getByFieldName('chapterBookPrice');
        if ($chapterBookPrice) {
            return $chapterBookPrice;
        }
    }

    return $price;
}

sub rListPrice {
    my $self  = shift;
    my $price = $self->_getByFieldName('price');

    if ( defined $self->_getByFieldName('chapterBookPrice') ) {
        my $chapterBookPrice = $self->_getByFieldName('chapterBookPrice');
        if ($chapterBookPrice) {
            return $chapterBookPrice;
        }
    }

    return $price;
}

sub isFree {
    my $self  = shift;
    my $units = $self->_getByFieldName('rUnits');

    my $price;

    if ( defined $self->_getByFieldName('chapterBookPrice') ) {
        $price = $self->_getByFieldName('chapterBookPrice');
    } else {
        $price = $self->_getByFieldName('price');
    }

    if ( $units && ( $price eq '0' || $price eq '0.00' ) ) {
        return 'Y';
    } else {
        return 'N';
    }
}

1;
