package BookPub::Import::Importer::Kobo::Version4;

use strict;

use lib '/app/tools/bookpub/lib';
use base 'BookPub::Import::Importer';

sub _headerIdentifier { ( isbn => 'ISBN' ) }

# We have to do some odd things with the field map here because this importer has
# different column definitions of seperate tabs.  The map is defined by tab name below.
# The summary tab is ignored (returns and empty field set).
sub _fieldMap {
    my $self = shift;

    my $map = {
        'us non taxable' => {
            currencyCode => 'L',
            countryCode  => 'B',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'H',
            rAuthor      => 'G',
            rUnits       => 'E',
            rListPrice   => 'I',
            rCOGS        => 'J',
            rRevenue     => 'K',
        },
        'us taxable' => {
            currencyCode => 'L',
            countryCode  => 'B',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'H',
            rAuthor      => 'G',
            rUnits       => 'E',
            rListPrice   => 'I',
            rCOGS        => 'J',
            rRevenue     => 'K',
        },
        'ca' => {
            currencyCode => 'K',
            countryCode  => 'B',
            dateBegin    => 'A',
            isbn         => 'E',
            rTitle       => 'G',
            rAuthor      => 'F',
            rUnits       => 'D',
            rListPrice   => 'H',
            rCOGS        => 'I',
            rRevenue     => 'J',
        },
        'world' => {
            currencyCode => 'K',
            countryCode  => 'B',
            dateBegin    => 'A',
            isbn         => 'E',
            rTitle       => 'G',
            rAuthor      => 'F',
            rUnits       => 'D',
            rListPrice   => 'H',
            rCOGS        => 'I',
            rRevenue     => 'J',
        },
        'refunds' => {
            currencyCode => 'N',
            countryCode  => 'C',
            dateBegin    => 'A',
            isbn         => 'F',
            rTitle       => 'H',
            rAuthor      => 'G',
            rListPrice   => 'I',
            rCOGS        => 'J',
            rRevenue     => 'M',
        },
    };

    my $sheet = lc( $self->sheetname ) || return {};
    return {} if ( $sheet eq 'summary' );

    die "Unrecognized tab name '" . $self->sheetname . "'" unless ( $map->{$sheet} );

    return $map->{$sheet};
}

sub priceType    { 'agency' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

sub units {
    my $self  = shift;
    my $units = $self->rUnits();

    # The refund tab doesn't list units.
    if ( lc( $self->sheetname ) eq 'refunds' && !defined($units) ) {
        $units = -1;
    }

    return $units;
}

sub rRevenue {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('rRevenue');
    if ( $self->units < 0 && $revenue > 0 ) {
        return $revenue * -1;
    } else {
        return $revenue;
    }
}

sub _isValidSaleRecord {
    my $self = shift;
    if ( !$self->dateBegin && $self->rTitle ) {
        $self->fail("Invalid date");
    }

    return $self->SUPER::_isValidSaleRecord(@_);
}

1;
