package BookPub::Import::Importer::EBook::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
# Note:
#  - No date column - Date month is parsed out of the first row.
#  - Negative prices == returns.
#
my %_fieldMap = (
    'creditor'         => 0,
    'publisher'        => 1,
    'imprint'          => 2,
    'title'            => 3,
    'print_isbn_10'    => 4,
    'print_isbn_13'    => 5,
    'e_isbn_10'        => 6,
    'e_isbn_13'        => 7,
    'format_isbn_10'   => 8,
    'format_isbn_13'   => 9,
    'retailer'         => 10,
    'date_purchased'   => 11,
    'reference_number' => 12,
    'format'           => 13,
    'price'            => 14,
    'discount'         => 15,
    'net_price'        => 16,
    'refund'           => 17,
);

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_purchased' );
            my $imprint         = $self->_getColumnValue( $line, 'imprint' );
            my $title           = $self->_getColumnValue( $line, 'title' );
            my $isbn10          = $self->_getColumnValue( $line, 'format_isbn_10' );
            my $isbn13          = $self->_getColumnValue( $line, 'format_isbn_13' );
            my $retailer        = $self->_getColumnValue( $line, 'retailer' );
            my $referenceNumber = $self->_getColumnValue( $line, 'reference_number' );
            my $format          = $self->_getColumnValue( $line, 'format' );
            my $netPrice        = $self->_getColumnValue( $line, 'net_price' );
            my $refundFlag      = $self->_getColumnValue( $line, 'refund' );

            # The 'netPrice' may have dollar signs...
            #
            $netPrice =~ s/\$//g;

            # Note that we don't have units - I will assume 1 unit per line...
            #

            if ( $isbn10 || $isbn13 ) {
                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);

                # We use a positive price, negative units to represent returns.
                #
                my $units = 1;

                if ( 'REFUNDED' eq $refundFlag ) {

                    # We want a positive price, negative units.
                    #
                    $netPrice *= -1;
                    $units = -1;
                }

                $saleRec->units($units);
                $saleRec->unitPrice( $netPrice / $units ) if $units;
                $saleRec->totalRevenue($netPrice);

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

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

                my $formatType = $self->_parseFormat($format);

                # !!! Make this dependent on the Format returned...
                # !!! This is becoming more and more pointless...
                #
                $saleRec->formatType($formatType);
                $saleRec->productType(BookPub::DB::Item::Sale::kProductTypeEBook);
                $saleRec->purchaseType(BookPub::DB::Item::Sale::kPurchaseTypeDownload);

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

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