package BookPub::Import::Importer::Fictionwise::Version1;

use strict;

use lib '/app/tools/common/lib';
use Common::Log;

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

my %fieldMap = (
    date     => 0,
    isbn     => 1,
    format   => 2,
    currency => 4,
    author   => 7,
    title    => 8,
    revenue  => 9,
);

#public methods

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

    my $lines = $args{lines};

    # i really don't like this next bit, but they seem to be arbitrarily moving the final rev col
    # so we gotta hunt for it. check line 12 as it should be well past the headers
    my $revColFound = 0;
    for ( my $i = $fieldMap{revenue} ; $i < $fieldMap{revenue} + 5 ; $i++ ) {
        if ( $lines->[12][$i] =~ /\d+/ ) {
            Common::Log::Print("found rev in col $i");
            $fieldMap{revenue} = $i;
            $revColFound = 1;
            last;
        }
    }
    unless ($revColFound) {
        $self->errstr('cannot find revenue column');
        Common::Log::Print('revenue col must have moved again!');
        return undef;
    }

    my %errMsg = ();
    my $saleCount;

    for ( my $i = 2 ; $i < scalar @$lines ; $i++ ) {
        my $title = $lines->[$i][ $fieldMap{title} ];
        my $isbn  = $lines->[$i][ $fieldMap{isbn} ];
        unless ( ( $title and lc $title ne 'title' ) or ( $isbn and $isbn =~ m/^\d{9,}$/ ) ) {
            Common::Log::Print("skip line $i - title: '$title', isbn: '$isbn'");
            next;
        }
        substr( $isbn, 0, 0 ) = 0 if ( length $isbn == 9 );    # add the missing leading 0

        my $dateTmp   = $lines->[$i][ $fieldMap{date} ];
        my $formatTmp = $lines->[$i][ $fieldMap{format} ];
        my $currency  = $lines->[$i][ $fieldMap{currency} ];
        my $author    = $lines->[$i][ $fieldMap{author} ];
        my $revenue   = $lines->[$i][ $fieldMap{revenue} ];

        my $date = Common::Util::normalize_date($dateTmp);
        unless ($date) {
            $self->errstr("invalid date: $dateTmp");
            unless ( $errMsg{$dateTmp} ) {
                Common::Log::Print("invalid date '$dateTmp' ($i)");
                $errMsg{$dateTmp} = 1;
            }
        }

        my $format = $self->_parseFormat($formatTmp);
        unless ($format) {
            $self->errstr("invalid format: $formatTmp");
            unless ( $errMsg{$formatTmp} ) {
                Common::Log::Print("invalid format '$formatTmp' ($i)");
                $errMsg{$formatTmp} = 1;
            }
        }

        my $saleRec = BookPub::Import::SaleRec->new();

        $saleRec->lineNum( $i + 1 );
        $saleRec->dateBegin($date);
        $saleRec->dateEnd($date);

        length $isbn == 10 ? $saleRec->isbn10($isbn) : $saleRec->isbn13($isbn);
        $saleRec->title($title);
        $saleRec->author($author);
        $saleRec->unitPrice( abs($revenue) );
        $saleRec->totalRevenue($revenue);
        $saleRec->currencyCode($currency);
        $saleRec->formatType($format);

        # defaults
        # no units col provided so ASSume 1
        $saleRec->units(1);
        $saleRec->sales(1);
        $saleRec->countryCode('US');
        $saleRec->productType(BookPub::DB::Item::Sale::kProductTypeEBook);
        $saleRec->purchaseType(BookPub::DB::Item::Sale::kPurchaseTypeDownload);

        #$args{dumper}($saleRec);
        $self->_insertSale( sale => $saleRec );
        $saleCount++;
    }

    return $saleCount;
}

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