package BookPub::Import::Importer::UAGC::v1;

use strict;
use warnings;

use Date::Calc qw(Days_in_Month Decode_Month);

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

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

my %months = (
    "jan" => 1,
    "feb" => 2,
    "mar" => 3,
    "apr" => 4,
    "may" => 5,
    "jun" => 6,
    "jul" => 7,
    "aug" => 8,
    "sep" => 9,
    "oct" => 10,
    "nov" => 11,
    "dec" => 12
);

sub _headerIdentifier { rAuthor => 'AuthorName' }

sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rPurchaseType              { shift->purchaseType }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType               { shift->productType }
sub priceType                  { 'rrp' }
sub rServiceName               { 'UAGC' }
sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub _processHeader {
    my $self = shift;

    unless ( $self->{_date_begin} && $self->{_date_end} ) {
        # M/D/YY-M/DD/YY
        my $period = $self->_getByFieldName("dateBegin");
        if ( $period =~ /^(\d{1,2}).(\d{1,2}).(\d{2})[-](\d{1,2}).(\d{1,2}).(\d{2})$/ ) {
            $self->{_date_begin} = sprintf "20%02d-%02d-%02d", $3, $1, $2;
            $self->{_date_end}   = sprintf "20%02d-%02d-%02d", $6, $4, $5;
        } elsif ( $period =~ /^\w{3} \d{2}$/ ) {
            ( $self->{_date_begin}, $self->{_date_end} ) = $self->_getDates(
                date_begin => $period,
                type       => "text"
            );
        # Oct-Dec 22
        } elsif ( $period =~ /^(\w{3})(?:\w{1,6})?\-(\w{3})(?:\w{1,6})? (\d{2})$/ ) {
            ( $self->{_date_begin}, $self->{_date_end} ) = $self->_getDates(
                date_begin => sprintf( "%02d%02d%02d", "20$3", $months{lc($1)}, 1 ),
                date_end   => sprintf( "%02d%02d%02d", "20$3", $months{lc($2)}, Days_in_Month( "20$3", $months{lc($2)} ) ),
            );
        }
    }

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

sub _getDateBegin {
    return shift->{_date_begin};
}

sub _getDateEnd {
    return shift->{_date_end};
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}

sub rUnits {
    my $self = shift;

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

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

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

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

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

sub _isSaleRec {
    my $self = shift;

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

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

1;
