package BookPub::Import::Importer::EDMC;

use strict;

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

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

sub _headerIdentifier { ( rUnits => 'ISBN TOTAL' ) }

sub _fieldMap {
    my $self     = shift;
    my %fieldMap = (
        1 => {
            rTitle     => 'B',
            rIsbn13    => 'A',
            rUnits     => 'C',
            rListPrice => 'D',
            rUnitPrice => 'D',
            rRevenue   => 'E',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub priceType          { 'rrp' }
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' }

sub dateBegin {
    my $self     = shift;
    my $filename = $self->filename();
    my ( $year, $month, $day );

    # used cached version, if it exists
    if ( $self->{_dateBegin} ) { return $self->{_dateBegin} }

    if ( $filename =~ /EDMC (\d{4}) Invoice Break (\d{1,2})/i ) {
        $year  = $1;
        $month = $2;

        # month is the month before $1, so we need to make it Dec,
        # if it's January and decrement the year
        if ( $month == 1 ) {
            $month = 12;
            $year -= 1;
        } else {
            $month -= 1;
        }

        # cache for other records
        $self->{_dateBegin} = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
        return $self->{_dateBegin};
    } else {
        $self->fail("Unexpected filename format, can't determine date: $filename");
    }
}

sub dateEnd {
    my $self     = shift;
    my $filename = $self->filename();
    my ( $year, $month, $day );

    # used cached version, if it exists
    if ( $self->{_dateEnd} ) { return $self->{_dateEnd} }

    if ( $filename =~ /EDMC (\d{4}) Invoice Break (\d{1,2})/i ) {
        $year  = $1;
        $month = $2;

        # month is the month before $1, so we need to make it Dec,
        # if it's January and decrement the year
        if ( $month == 1 ) {
            $month = 12;
            $year -= 1;
        } else {
            $month -= 1;
        }
        $day = Date::Simple::days_in_month( $year, $month );

        # cache for other records
        $self->{_dateEnd} = sprintf( "%04d-%02d-%02d", $year, $month, $day );
        return $self->{_dateEnd};
    } else {
        $self->fail("Unexpected filename format, can't determine date: $filename");
    }
}

sub rInstitution {
    my $self = shift;

    my $institution = $self->sheetname();

    if (   $institution !~ /^(argosy|south university|art institute) - (ground|online)$/i
        && $institution !~ /^brown mackie college$/i ) {
        $self->fail("Unrecognized institution: $institution");
    }

    return $institution;
}

sub _isSaleRec {
    my $self = shift;

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

    my $title = $self->_getByFieldName('rTitle');
    if ( $title =~ /term total/i ) {
        return;
    }

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

1;
