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

use strict;

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

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

# 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 = {
        'kobo' => {
            currencyCode       => 'P',
            conversionRate     => 'N',
            countryCode        => 'C',
            rState             => 'D',
            rPostalCode        => 'E',
            dateBegin          => 'A',
            isbn               => 'G',
            rTitle             => 'I',
            rAuthor            => 'H',
            rUnits             => 'F',
            rListPrice         => 'J',
            rListPriceCurrency => 'M',
            rCOGS              => 'K',
            rRevenue           => 'O',
            rChannel           => 'B',
        },
        'partners' => {
            currencyCode       => 'P',
            conversionRate     => 'N',
            countryCode        => 'C',
            rState             => 'D',
            rPostalCode        => 'E',
            dateBegin          => 'A',
            isbn               => 'G',
            rTitle             => 'I',
            rAuthor            => 'H',
            rUnits             => 'F',
            rListPrice         => 'J',
            rListPriceCurrency => 'M',
            rCOGS              => 'K',
            rRevenue           => 'O',
            rChannel           => 'B',
        },
        'refund' => {
            currencyCode       => 'P',
            conversionRate     => 'N',
            countryCode        => 'C',
            rState             => 'D',
            rPostalCode        => 'E',
            dateBegin          => 'A',
            isbn               => 'G',
            rTitle             => 'I',
            rAuthor            => 'H',
            rReason            => 'Q',
            rUnits             => 'F',
            rListPrice         => 'J',
            rListPriceCurrency => 'M',
            rCOGS              => 'K',
            rRevenue           => 'O',
            rChannel           => 'B',
        },
        'promo' => {
            currencyCode       => 'R',
            conversionRate     => 'P',
            countryCode        => 'E',
            rState             => 'F',
            rPostalCode        => 'G',
            dateBegin          => 'C',
            isbn               => 'I',
            rTitle             => 'K',
            rAuthor            => 'J',
            rUnits             => 'H',
            rListPrice         => 'L',
            rListPriceCurrency => 'O',
            rDiscount          => 'M',
            rRevenue           => 'Q',
            rPromoCode         => 'B',
        },
    };

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

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

    return $map->{$sheet};
}

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 ) {
        return 'agency';
    } else {
        return 'rrp';
    }
}

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

    if ( lc( $self->sheetname ) =~ /(refund|gifts|promo)/i ) {
        return $units * -1;
    }

    return $units;
}

sub revenue {
    my $self    = shift;
    my $units   = $self->units();
    my $revenue = $self->SUPER::revenue();

    if ( $units && $units < 0 && $revenue && $revenue > 0 ) {
        return $revenue * -1;
    }

    return $revenue;
}

sub rListPrice {
    my $self = shift;

    my $sheet             = lc( $self->sheetname );
    my $listPrice         = $self->_getByFieldName('rListPrice');
    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency');
    my $currencyCode      = $self->_getByFieldName('currencyCode');

    # We want to convert all list prices to the same currency as the revenue for RLPG and Wiley
    if ( $currencyCode ne $listPriceCurrency && ( Common::RSApp::GetClientID == 346 || Common::RSApp::GetClientID == 324 ) ) {

        my $conversionRate = $self->_getByFieldName('conversionRate');
        $listPrice *= $conversionRate;
    }

    return $listPrice;
}

sub rListPriceCurrency {
    my $self = shift;

    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency');

    # We're converting the list prices for RLPG and Wiley, so we need to set this to the revenue's currency code.
    if ( Common::RSApp::GetClientID == 346 || Common::RSApp::GetClientID == 324 ) {
        $listPriceCurrency = $self->currencyCode;
    }

    return $listPriceCurrency;
}

sub _useIsSaleRec { 1 }

sub _isSaleRec {
    my $self = shift;
    return $self->rTitle && $self->dateBegin;
}

1;
