package BookPub::Import::Importer::Zovio::Version4;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin          => 'O1',
        dateEnd            => 'O1',
        rAuthor            => 'F',
        rTitle             => 'C',
        rIsbn13            => 'D',
        rInstitution       => 'J',
        rState             => 'L',
        rPostalCode        => 'M',
        countryCode        => 'K',
        rUnits             => 'O',
        rUnitPrice         => 'H',
        rListPriceCurrency => 'I',
        rRevenue           => 'P',
        currencyCode       => 'I',
        rModel             => 'N',
    }
}

sub _headerIdentifier { ( rAuthor => 'AuthorName' ) }

sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub priceType                  { 'rrp' }
sub rServiceName               { 'Zovio' }
sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

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

    if ( $dateBegin =~ /(\d\d)\/(\d\d)\/(\d\d)-.+$/ ) {
        my ($month, $day, $year)  = ($1, $2, $3);

        $dateBegin = sprintf( "20%02d-%02d-%02d", $year, $month, $day );
    }

    return $dateBegin;
}

sub _getDateEnd {
    my $self    = shift;
    my $dateEnd = $self->_getByFieldName('dateEnd');

    if ( $dateEnd =~ /-(\d\d)\/(\d\d)\/(\d\d)$/ ) {
        my ($month, $day, $year)  = ($1, $2, $3);

        $dateEnd = sprintf( "20%02d-%02d-%02d", $year, $month, $day );
    }

    return $dateEnd;
}

sub _processSheet {
    my ( $self, $sheet, $sheetnum ) = @_;

    return unless $self->{sheetname} =~ /Totals?|ISBNs?/i;

    return $self->BookPub::Import::Importer::_processSheet( $sheet, $sheetnum );
}

sub rUnits {
    my $self = shift;

    my $rUnits = $self->_getByFieldName('rUnits') || 0;

    return $rUnits =~ /^[-+]?\d+$/ ? $rUnits : 0;
}

sub _isSaleRec {
    my $self = shift;

    my $rUnits = $self->_getByFieldName('rUnits') || 0;
    return unless $rUnits;

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


1;
