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

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';

use base 'BookPub::Import::Importer::Kobo::Version35';

sub _fieldMap {
    {
        dateBegin                => 'A',
        purchaseType             => 'E',
        rProductType             => 'E',
        rAuthor                  => 'M',
        rTitle                   => 'N',
        rIsbn13                  => 'L',
        rImprint                 => 'K',
        rState                   => 'C',
        rPostalCode              => 'D',
        countryCode              => 'B',
        rUnits                   => 'F',
        rAdjustments             => 'Y',
        rListPrice               => 'P',
        rNativeListPrice         => 'P',
        rNativeListPriceCurrency => 'S',
        rPurchasePrice           => 'V',
        rPurchasePriceCurrency   => 'X',
        rCOGS                    => 'Q',
        rRevenue                 => 'Z',
        currencyCode             => 'AA',
        rTaxAmount               => 'AB',
        rPromoCode               => 'H',
        rComments                => 'G',
        rDistributor             => 'I',
    };
}

sub _unverifiedFieldMap {
    {
        nativeLPCurrency => 'S',
        currencyConv     => 'T',
    };
}

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

sub _useIsSaleRec { 1 }
sub _headerIdentifier { ( rIsbn13 => 'eISBN' ) }

sub purchaseType {
    my $self = shift;

    my $purchaseType = lc( $self->_getByFieldName('purchaseType') || '' );
    $purchaseType =~ s/\s+/ /g;
    $purchaseType =~ s/^\s|\s$//g;

    my %typesMap = (
        'e-book'                   => BookPub::DB::Item::Sale::kPurchaseTypeDownload,
        'audiobook'                => BookPub::DB::Item::Sale::kPurchaseTypeDownload,
        'audiobook - credit'       => BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL,
        'audiobook - credit trial' => BookPub::DB::Item::Sale::kPurchaseTypePromotional,
        'audiobook - walmart card' => BookPub::DB::Item::Sale::kPurchaseTypeDownload,
        'e-book - walmart card'    => BookPub::DB::Item::Sale::kPurchaseTypeDownload,
        'bulk buy - ebook'         => BookPub::DB::Item::Sale::kPurchaseTypeDownload,
    );

    return $typesMap{$purchaseType} if exists $typesMap{$purchaseType};

    $self->fail("Could not determine purchase type from $purchaseType");
}

sub productType {
    my $self = shift;
    my $type = lc( $self->rProductType ) || return;

    if ( $type =~ /e-?book/i ) {
        $type = BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ( $type =~ /audiobook/i ) {
        $type = BookPub::DB::Item::Sale::kProductTypeAudioBook;
    } else {
        $self->fail("Unexpected product type: $type");
    }

    return $type;
}

sub rTaxUnitPrice {
    my $self = shift;

    if ( $self->units ) {
        return $self->rTaxAmount / $self->units;
    }
}

sub isFree {
    my $self = shift;

    my $revenue = $self->_getByFieldName("rRevenue") || 0;
    my $units = $self->_getByFieldName("rUnits") || 0;

    return "Y" if ($revenue == 0 && $units != 0);
    return "N";
}

1;

