package BookPub::Import::Importer::CreateSpace::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 = (
    'isbn10'  => 3,
    'isbn13'  => 4,
    'title'   => 7,
    'author'  => 6,
    'units'   => 8,
    'price'   => 16,
    'revenue' => 17,
);

#public methods

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

    my $origFileName = $args{file}{OrigFileName};

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

    my $foundHeader = 0;

    # Parse the date from the filename...
    # ex:    CreateSpace POD Sales Summary Sep 2009
    #
    my $unparsedDate;
    if ( $origFileName =~ /.*Summary (\w+ \w+)/ ) {
        $unparsedDate = $1;
    }
    my ( $beginDate, $endDate ) = $self->_getDates( date_begin => $origFileName, type => 'text' );

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

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

        if ( !$foundHeader ) {
            if ( 'Sales Channel' eq $line->[0] ) {
                $foundHeader = 1;
                next;
            }
        } else {

            # JPK - The example file we have has two distinct sections.
            # So we want to skip over the second header...
            #
            next if ( 'Sales Channel' eq $line->[0] );

            my $isbn10  = $line->[ $fieldMap{'isbn10'} ];
            my $isbn13  = $line->[ $fieldMap{'isbn13'} ];
            my $title   = $line->[ $fieldMap{'title'} ];
            my $author  = $line->[ $fieldMap{'author'} ];
            my $units   = $line->[ $fieldMap{'units'} ];
            my $price   = $line->[ $fieldMap{'price'} ];
            my $revenue = $line->[ $fieldMap{'revenue'} ];

            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;
                }

                # In this file, we sometimes see title:  subtitle, so try and parse that.
                #
                my $subtitle;
                if ( $title =~ /^(.*): (.*)/ ) {
                    $title    = $1;
                    $subtitle = $2;
                }

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

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

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

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

                #                $saleRec->formatType(BookPub::DB::Item::Sale::kFormatTypeHardback);
                $saleRec->purchaseType(BookPub::DB::Item::Sale::kPurchaseTypeDownload);

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

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