package BookPub::Import::Importer::Follett_CafeScribe::Version1;

use strict;

use Date::Calc qw(Days_in_Month);
use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Consts;
use Common::Log;
use Common::Assert;

use lib '/app/tools/data_classes/lib';

use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::Sale;
use BookPub::Import::SaleRec;
use base 'BookPub::Import::Importer';

# map version number to data columns
#
my %_fieldMap = (
    'reseller_name'     => 0,
    'report_year_month' => 1,
    'reseller_po#'      => 2,
    'digital_isbn_13'   => 3,
    'print_isbn_13'     => 4,
    'title'             => 5,
    'author'            => 6,
    'gross_revenue'     => 7,
    'gross_units'       => 8,
    'cancelled_units'   => 9,
    'net_units'         => 10,
    'net_revenue'       => 11,
    'date'              => 12,
    'school'            => 13,
    'zipcode'           => 14,
);

sub _getFieldMap {
    my ($self) = @_;

    return \%_fieldMap;
}

sub _getColumnValue {
    my ( $self, $line, $colName ) = @_;

    my $fieldMap = $self->_getFieldMap();
    return undef unless defined $fieldMap->{$colName};
    return $line->[ $fieldMap->{$colName} ];
}

#public methods

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

    my $lines = $args{lines};
    my $saleCount;

    my $fieldMap = $self->_getFieldMap();

    for ( my $i = 0 ; $i <= scalar @$lines ; $i++ ) {
        my $line    = $lines->[$i];
        my $linenum = $i + 1;

        if ( 1 == $linenum ) {

            # Just skip the first line:  It's the header.
            #
            next;
        } else {
            my $dateBegin = $self->_getColumnValue( $line, 'date' );
            my $isbn13    = $self->_getColumnValue( $line, 'digital_isbn_13' );
            my $title     = $self->_getColumnValue( $line, 'title' );
            my $author    = $self->_getColumnValue( $line, 'author' );
            my $units     = $self->_getColumnValue( $line, 'net_units' );
            my $revenue   = $self->_getColumnValue( $line, 'net_revenue' );
            my $channel   = $self->_getColumnValue( $line, 'school' );

            # !!! Skipping lines with no net units.
            #
            if ( $isbn13 && $units ) {
                my $saleRec = BookPub::Import::SaleRec->new();

                $saleRec->lineNum($linenum);

                my ( $beginDate, $endDate ) = $self->_getDates( date_begin => $dateBegin );

                $saleRec->dateBegin($beginDate);
                $saleRec->dateEnd($endDate);

                $saleRec->title($title);
                $saleRec->author($author);

                $saleRec->units($units);
                $saleRec->totalRevenue($revenue);

                $saleRec->isbn13($isbn13);
                $saleRec->channel($channel);

                $saleRec->currencyCode('USD');
                $saleRec->countryCode('US');

                $saleRec->formatType(BookPub::DB::Item::Sale::kFormatTypeEBook);
                $saleRec->productType(BookPub::DB::Item::Sale::kProductTypeBook);
                $saleRec->deliveryType(BookPub::DB::Item::Sale::kDeliveryTypeDownload);

                $self->_insertSale( sale => $saleRec );
                $saleCount++;
            }
        }
    }
    return $saleCount;
}

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