package BookPub::Import::Importer::Gardners::Version11;

use strict;

use Date::Calc qw( Add_Delta_YM );

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

sub _fieldMap {
    {
        dateBegin              => 'O',
        rPurchaseType          => 'J',
        rPriceType             => 'E3',    #Header
        priceTypeQualifierID   => 'E3',    #Header
        rTitle                 => 'B',
        rIsbn13                => 'A',
        rInstitution           => 'N',
        countryCode            => 'M',
        rUnits                 => 'C',
        rListPrice             => 'G',
        rListPriceCurrency     => 'D',
        rPurchasePrice         => 'E',
        rPurchasePriceCurrency => 'D',
        rDiscount              => 'I',
        rRevenue               => 'H',
        currencyCode           => 'D',
        rTaxUnitPrice          => 'F',
        rChannel               => 'P',
        rModel                 => 'J',
    };
}

sub _unverifiedFieldMap {
    {
        # Header
        defaultDateBegin => 'A1',    # Begin date to use to differentiate between EUR and US dates
    };
}

sub _dateNormalization { }

sub _dateFormat { ['eur'] }

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

    $self->_storeHeaderDate();
}

sub _storeHeaderDate {
    my $self = shift;

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

    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->{_headerDateBegin} = sprintf( "%04d-%02d-%02d", $year, $month, $day );
        $self->{_headerMonthBegin} = sprintf( "%02d", $month );
    }
}

sub dateBegin {
    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 dateEnd {
    my $self = shift;

    $self->dateBegin();
}

sub rTaxUnitPrice {
    my $self       = shift;
    my $totalPrice = $self->_getByFieldName('rTaxUnitPrice');
    my $listPrice  = $self->_getByFieldName('rListPrice');

    if ( $totalPrice && $listPrice ) {
        return $totalPrice - $listPrice;
    } else {
        $self->fail("Missing either rTaxUnitPrice, $totalPrice, or rListPrice $listPrice");
    }
}

sub priceType {
    my $self = shift;
    my $type = $self->_getByFieldName('rPriceType');
    if ( $type =~ /rrp/i ) {
        return 'rrp';
    } else {
        $self->fail("Unexpected priceType: $type");
    }
}

sub priceTypeQualifierID {
    my $self      = shift;
    my $qualifier = $self->_getByFieldName('priceTypeQualifierID');
    if ( $qualifier =~ /institutional/i ) {
        return BookPub::DB::Item::PriceTypeQualifier::kCorporate;    # Library
    } else {
        $self->fail("Unexpected priceTypeQualifier: $qualifier");
    }
}

1;
