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

use strict;

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

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

sub _fieldMap {
    my $self = shift;

    my $sheet = lc( $self->sheetname ) || return {};
    return {} if ( $sheet =~ /(invoice|summary)/i );

    die "Unrecognized tab name '" . $self->sheetname . "'" unless ( $sheet eq 'sales' || $sheet eq 'kobo' );

    my %fieldMap = (
        32 => {
            currencyCode       => 'V',
            conversionRate     => 'T',
            countryCode        => 'C',
            rState             => 'D',
            rPostalCode        => 'E',
            dateBegin          => 'A',
            isbn               => 'K',
            rTitle             => 'M',
            rAuthor            => 'L',
            rImprint           => 'J',
            rSales             => 'F',
            rReturns           => 'G',
            rListPrice         => 'O',
            rListPriceCurrency => 'S',

            #rPurchasePrice     => 'N',
            rPurchasePrice         => 'P',
            rPurchasePriceCurrency => 'S',
            rCOGS                  => 'Q',
            rTaxUnitPrice          => 'N',
            rRevenue               => 'U',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _dateFormat { [ 'iso', 'excel', 'text', 'eur' ] }

sub priceType {
    my $self     = shift;
    my $filename = $self->filename();
    if (   $filename =~ /wiley/i
        || $filename =~ /hachette uk/i ) {
        return 'agency';
    } else {
        return 'rrp';
    }
}

sub rTaxUnitPrice {
    my $self = shift;
    if ( defined $self->_getByFieldName('rTaxUnitPrice') ) {
        my $rPubPrice = $self->_getByFieldName('rTaxUnitPrice');

        my $rTaxUnitPrice = $rPubPrice - $self->rListPrice;

        $rTaxUnitPrice *= -1 if ( $self->revenue < 0 );

        return $rTaxUnitPrice;
    }

    my $rTaxUnitPrice = $self->rPurchasePrice - $self->rListPrice;

    return $rTaxUnitPrice;
}

1;
