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

use strict;
use warnings;

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

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

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

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

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,
        'bulk buy - audiobook'     => 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 priceType {
    my $self      = shift;
    my $priceType = 'rrp';

    # Price type is RRP for audiobooks for ALL clients.
    if ( $self->productType eq BookPub::DB::Item::Sale::kProductTypeAudioBook ) {
        return $priceType;
    }

    my $clientID = Common::RSApp::GetClientID;

    # Hachette UK
    if ( $clientID == 355 ) {
        if ( $self->countryCode =~ /^(AT|AU|CA|BE|CY|FI|FR|DE|GR|IE|IT|LU|MT|NL|PT|SK|SI|ES|CH|GB|UK)$/ ) {
            $priceType = 'agency';
        } elsif ( $self->countryCode =~ /^(NZ)$/ ) {
            $priceType = 'agencyplustax';
        }
    }

    # Macmillan US
    elsif ( $clientID == 318 ) {
        if ( $self->countryCode =~ /^(US|CA)$/ ) {
            $priceType = 'agency';
        }
    }

    # Macmillan AU
    elsif ( $clientID == 348 ) {
        if ( $self->countryCode =~ /^(AU|NZ|CA|EU|UK|US)$/ ) {
            $priceType = 'agencyplustax';
        }
    }

    # Macmillan UK
    elsif ( $clientID == 359 ) {
        $priceType = 'agency';
    }

    return $priceType;
}

sub rListPrice {
    my $self = shift;

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

    # Leave rListPrice unconverted for non-list price required customers
    if ( $self->listPriceRequired && $rListPriceCurrency ne $self->currencyCode ) {
        $rListPrice *= $self->_getByFieldName('currencyConv');
    }

    return $rListPrice;
}

sub rListPriceCurrency {
    my $self = shift;

    my $rListPriceCurrency = $self->_getByFieldName('nativeLPCurrency');
    if ( $self->listPriceRequired && $rListPriceCurrency ne $self->currencyCode ) {
        $rListPriceCurrency = $self->currencyCode;
    }

    return $rListPriceCurrency;
}

sub rTaxAmount {
    my $self = shift;

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

    $amount = abs($amount) * -1 if ($self->currencyCode eq 'CAD' && $units < 0 && $revenue < 0);
    $amount = abs($amount)  if ($self->currencyCode eq 'CAD' && $units > 0 && $revenue > 0);

    return $amount;
}

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";
}

sub rTaxUnitPrice {
    my $self = shift;

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

1;

