package BookPub::Import::Importer::Gardners::Version6;

use strict;

use Date::Calc qw( Add_Delta_YM );

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

sub _fieldMap {
    {
        # Detail
        currencyCode => 'D',
        countryCode  => 'M',
        dateBegin    => 'P',
        rIsbn13      => 'A',
        rTitle       => 'B',
        rUnits       => 'C',
        rListPrice   => 'E',
        rDiscount    => 'I',
        rRevenue     => 'H',
    };
}

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

sub _headerIdentifier { ( rTitle => 'TITLE' ) }

sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType     { 'rrp' }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub _dateFormat   { [ 'eur', 'iso' ] }
sub _useIsSaleRec { 1 }

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 _processLine {
    my $self = shift;
    my $isbn = $self->_getByFieldName('rIsbn13');

    if ( $isbn eq 'Total Qty Sold' ) {
        $self->{_endOfData} = 1;
    }

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

sub _isSaleRec {
    my $self = shift;

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

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

1;
