package BookPub::Import::Importer::BakerAndTaylor::Version4;

use strict;
use Data::Dumper;
use Date::Calc qw(Add_Delta_Days);

use lib '/app/tools/common/lib';
use Common::Util;

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

# This field map will only be used for the tax tab
#
my %taxFieldMap = (

    # For data
    rState      => 3,
    rPostalCode => 7,
    rImprint    => 32,

    # For verification
    isbn     => 25,
    rUnits   => 33,
    rRevenue => 39,
);

my %taxData;

sub _preProcess {
    my $self = shift;
    my %args = @_;

    Log->info("Starting _preProcess");
    $self->SUPER::_preProcess(%args);

    my $sheetnum = 0;

    foreach my $sheet ( @{ $args{sheets} } ) {
        my $sheetname = $args{sheet_names}->[$sheetnum];
        $sheetnum++;

        # We need to go through the tax tab and store some of that data in a hash
        # so that we can access it while processing the Sales tab.
        if ( $sheetname eq 'Tax' ) {
            my $linenum     = 0;
            my $foundHeader = 0;
            foreach my $line (@$sheet) {

                $linenum++;

                if ( $line->[0] eq 'Publisher' ) {
                    $foundHeader = 1;
                    next;
                } elsif ( $foundHeader == 0 ) {
                    next;
                }

                # Grab the data and store it
                $taxData{$linenum}{rState}      = $line->[ $taxFieldMap{rState} ];
                $taxData{$linenum}{rPostalCode} = $line->[ $taxFieldMap{rPostalCode} ];
                $taxData{$linenum}{rImprint}    = $line->[ $taxFieldMap{rImprint} ];

                $taxData{$linenum}{isbn}     = $line->[ $taxFieldMap{isbn} ];
                $taxData{$linenum}{rUnits}   = $line->[ $taxFieldMap{rUnits} ];
                $taxData{$linenum}{rRevenue} = $line->[ $taxFieldMap{rRevenue} ];
            }

        }
    }

}

sub rState {
    my $self  = shift;
    my $state = $taxData{ $self->linenum }{rState};
    return $state;
}

sub rPostalCode {
    my $self       = shift;
    my $postalCode = $taxData{ $self->linenum }{rPostalCode};

    # The zip codes sometimes have an A in place of a dash.
    $postalCode =~ s/A/-/;

    return $postalCode;
}

sub rImprint {
    my $self    = shift;
    my $imprint = $taxData{ $self->linenum }{rImprint};
    return $imprint;
}

sub isbn {
    my $self    = shift;
    my $isbn    = $self->SUPER::isbn() || return;
    my $linenum = $self->linenum;
    my $taxIsbn = $taxData{$linenum}{isbn};

    if ( $isbn ne $taxIsbn ) {
        $self->fail("ISBNs for line $linenum do not match ($isbn != $taxIsbn).");
        return undef;
    }

    return $isbn;
}

sub rUnits {
    my $self     = shift;
    my $rUnits   = $self->SUPER::rUnits() || return;
    my $linenum  = $self->linenum;
    my $taxUnits = $taxData{ $self->linenum }{rUnits};

    if ( $rUnits != $taxUnits ) {
        $self->fail("Unit values for line $linenum do not match ($rUnits != $taxUnits).");
        return undef;
    }

    return $rUnits;
}

sub rRevenue {
    my $self       = shift;
    my $rRevenue   = $self->SUPER::rRevenue() || return;
    my $linenum    = $self->linenum;
    my $taxRevenue = $taxData{ $self->linenum }{rRevenue};

    if ( $rRevenue != $taxRevenue ) {
        $self->fail("Revenue values for line $linenum do not match ($rRevenue != $taxRevenue)");
        return undef;
    }

    return $rRevenue;
}

sub isFree {
    my $self    = shift;
    my $price   = $self->_getByFieldName('rListPrice');
    my $revenue = $self->_getByFieldName('rRevenue');
    return ( $price && $revenue ) ? 'N' : 'Y';
}

sub _dateFormat { [ 'excel', 'iso' ] }

sub dateBegin {
    my $self = shift;
    my $dateBegin = $self->SUPER::dateBegin() || return;

    if ( $dateBegin =~ m/(\d{4})-(\d{2})-(\d{2})/ ) {

        # The dates are importing as two days ahead of what is displayed in the sales file.
        my $offset = -2;

        my ( $y, $m, $d ) = Add_Delta_Days( $1, $2, $3, $offset );
        $dateBegin = $y . "-" . $m . "-" . $d;
        $dateBegin = Common::Util::normalize_date($dateBegin);
    }

    return $dateBegin;
}

sub _isValidSaleRecord {
    my $self = shift;
    if ( $self->_getByFieldName('rUnits') eq '-' ) {
        return undef;
    }

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

###
1;    # Play nicely.
###
