package BookPub::Import::Importer::Gardners::Version3;

use strict;

use Date::Calc qw( Add_Delta_YM );

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

sub _fieldMap {
    {
        currencyCode           => 'H',
        countryCode            => 'G',
        dateBegin              => 'N',
        rFormat                => 'B',
        rIsbn13                => 'A',
        rTitle                 => 'C',
        rUnits                 => 'E',
        rListPrice             => 'J',
        rListPriceCurrency     => 'H',
        rPurchasePrice         => 'I',
        rPurchasePriceCurrency => 'H',
        rRevenue               => 'K',
    };
}

sub _unverifiedFieldMap {
    {
        # Header
        defaultDateBegin => 'A1',    # Begin date for all items in the file if not explicitly specified in the record
    };
}

sub _dateNormalization {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin');
    if ( !$date ) {
        return 'month';
    }
}

# Need to grab the date from the header row
sub _processHeader {
    my $self = shift;

    $self->_storeDates();
}

sub _storeDates {
    my $self = shift;

    return if ( $self->{_dateBegin} );

    my $date = $self->_getByFieldName('defaultDateBegin');

    if ( $date && $date =~ /(\d{1,2})\/(\d{1,2})\/(\d{4})/ ) {
        my ( $year, $month, $day ) = Add_Delta_YM( $3, $2, $1, 0, -1 );

        $self->{_dateBegin} = sprintf( "%04d-%02d-%02d", $year, $month, $day );
        $self->{_monthBegin} = sprintf( "%02d", $month );
    }
}

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

    # if date is missing, use the date from header
    if ( !$date || $date eq '' ) {
        return $self->{_dateBegin};
    }

    # date cells in this sample file were mixed formatting, both number
    # and date.  When they were date, the European order (date, month, year)
    # wasn't preserved.  I get either format and compare it to the header date.
    # If necessary, I swap day and month.

    # date might be either MM-DD-YYYY or DD-MM-YYYY
    if ( $date && $date =~ /(\d{1,2})[\-\/](\d{1,2})[\-\/](\d{4})/ ) {
        $year  = substr( $date, 6, 4 );
        $month = substr( $date, 0, 2 );
        $day   = substr( $date, 3, 2 );
    }

    # date might be either YYYY-MM-DD or YYYY-DD-MM
    elsif ( $date && $date =~ /(\d{4})[\-\/](\d{1,2})[\-\/](\d{1,2})/ ) {
        $year  = substr( $date, 0, 4 );
        $month = substr( $date, 5, 2 );
        $day   = substr( $date, 8, 2 );
    } else {
        $self->fail("Unexpected date format, $date");
    }

    # compare the month value to the month value in the header and swap
    # month and day if they don't match
    if ( $month == $self->{_monthBegin} ) {
        return $date;
    } else {
        $date = sprintf( "%04d-%02d-%02d", $year, $day, $month );
        return $date;
    }

}

sub rFormat {
    my $self   = shift;
    my $format = $self->_getByFieldName('rFormat');

    if ( Common::RSApp::GetClientID == 359 && $format =~ /pdf/i ) {    # MMUK
        $self->fail("PDF sales not allowed for this service");
    }

    return $format;
}

sub rTaxUnitPrice {
    my $self = shift;

    return $self->rPurchasePrice - $self->rListPrice;
}

1;
