package BookPub::Import::Importer::txtr::Version1;

use strict;
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';

use constant BLOOMSBURY     => 'bloomsbury';
use constant BLOOMSBURYUSA  => 'bloomsburyus';
use constant BLOOMSBURYUK   => 'bloomsburyuk';
use constant HACHETTE       => 'hachetteus';
use constant BERRETTKOEHLER => 'berrettkoehler';

sub _fieldMap {
    {
        currencyCode       => 'L',
        countryCode        => 'U',
        dateBegin          => 'B',
        rIsbn13            => 'D',
        rTitle             => 'E',
        rListPrice         => 'H',
        rListPriceCurrency => 'F',
        rPurchasePrice     => 'S',
        rDiscount          => 'AD',
        rRevenue           => 'N',
    };
}

sub _headerIdentifier { ( rTitle => 'Title' ) }
sub purchaseType      { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType       { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin') || return;

    # the date is being returned in the "days since 1/1/1900" format, so we need
    # to convert it.  We'll check if it's a 5 digit number and greater than the
    # number of days since 1/1/1900 on 1/1/2006, which is 38716
    if ( $date =~ /^\d{5}$/ and $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }

    return $date

}

sub units {
    my $revenue = shift->rRevenue || return 1;
    return $revenue < 0 ? -1 : 1;
}

sub isFree {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('rRevenue');

    if ( $revenue == 0 ) {
        return 'Y';
    }
    return 'N';

}

sub priceType {
    my $self = shift;
    my $type = $self->{_agencyFromHeader} || return;

    # Let's strip out the spaces to make it easier to match.
    $type =~ s/ //g;

    # Make it lower case too.
    $type = lc($type);

    my $typeID =
        $type eq BLOOMSBURY     ? 'rrp'
      : $type eq BLOOMSBURYUSA  ? 'rrp'
      : $type eq BLOOMSBURYUK   ? 'rrp'
      : $type eq HACHETTE       ? 'agency'
      : $type eq BERRETTKOEHLER ? 'rrp'
      :                           undef;

    $self->fail("Unknown price type '$type'") unless ($typeID);

    return $typeID;
}

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

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

    my $sheetnum = 0;
    my $linenum  = 0;

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

        # We need to grab the row 4, col B value to set the priceType
        foreach my $line (@$sheet) {

            $linenum++;

            if ( $line->[0] eq 'Content Provider:' ) {
                $self->{_agencyFromHeader} = $line->[1];
                return;
            } else {
                next;
            }

        }
    }

}

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