package BookPub::Import::Importer::CourseSmart::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 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
# Note:
#  - No date column - Date month is parsed out of the first row.
#  - Negative prices == returns.
#
my %fieldMap = (
    'date'    => 2,
    'isbn10'  => 0,
    'isbn13'  => 1,
    'title'   => 2,
    'units'   => 3,
    'price'   => 4,
    'revenue' => 5,
);

#public methods

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

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

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

        #        Common::Log::Print("LINE: $linenum = " . Dumper($line));

        if ( 1 == $linenum ) {

            # First, get the date from the first line.
            #
            my $date;
            ( $beginDate, $endDate ) = $self->_getDates( $line->[ $fieldMap{'date'} ] );
            Common::Log::Print("beginDate = $beginDate, endDate = $endDate");
        } else {
            my $isbn10  = $line->[ $fieldMap{'isbn10'} ];
            my $isbn13  = $line->[ $fieldMap{'isbn13'} ];
            my $title   = $line->[ $fieldMap{'title'} ];
            my $units   = $line->[ $fieldMap{'units'} ];
            my $price   = $line->[ $fieldMap{'price'} ];
            my $revenue = $line->[ $fieldMap{'revenue'} ];

            # Skip the header...
            #
            next if ( 'eTextISBN10_Value' eq $isbn10 );

            if ( $isbn10 && $isbn13 ) {
                my $saleRec = BookPub::Import::SaleRec->new();

                $saleRec->lineNum($linenum);

                # We don't store price anymore - But we do store _negative_ units
                # to signify a return.
                #
                if ( $price < 0 ) {
                    $units *= -1;
                }

                $saleRec->dateBegin($beginDate);
                $saleRec->dateEnd($endDate);
                $saleRec->title($title);
                $saleRec->units($units);
                $saleRec->totalRevenue($revenue);

                # Strip out the dashes from the ISBN values.
                #
                $isbn10 =~ s/-//g;
                $isbn13 =~ s/-//g;
                $saleRec->isbn10($isbn10);
                $saleRec->isbn13($isbn13);

                # defaults
                #
                $saleRec->currencyCode('USD');
                $saleRec->countryCode('US');
                $saleRec->productType(BookPub::DB::Item::Sale::kProductTypeBook);

                #                $saleRec->formatType(BookPub::DB::Item::Sale::kFormatTypeEBook);
                # !!! Just a temporary hack to test matching.
                #
                $saleRec->formatType(BookPub::DB::Item::Sale::kFormatTypeHardback);

                $saleRec->deliveryType(BookPub::DB::Item::Sale::kDeliveryTypeDownload);

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

#
# Private methods
#

sub _getDates {
    my ( $self, $dateString ) = @_;

    Common::Log::Print("dateString: $dateString");
    if ( $dateString =~ /CourseSmart - (.*)/ ) {
        Common::Log::Print("dateString parsed: $1");
        return $self->SUPER::_getDates( date_begin => $1 );
    }
    return ( undef, undef );
}

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