package BookPub::Import::Importer::Google::Version24;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin                => 'B',
        dateEnd                  => 'B',
        rPurchaseType            => 'D',
        purchaseType             => 'D',
        rProductType             => 'W',
        priceType                => 'R',
        rAuthor                  => 'K',
        rTitle                   => 'J',
        rIsbn13                  => 'H',
        rImprint                 => 'I',
        countryCode              => 'Q',
        rReturns                 => 'E',
        rSales                   => 'E',
        rUnits                   => 'G',
        rListPrice               => 'M',
        rListPriceCurrency       => 'L',
        rNativeListPrice         => 'M',
        rNativeListPriceCurrency => 'L',
        rPurchasePrice           => 'P',
        rPurchasePriceCurrency   => 'N',
        rCOGS                    => 'R',
        rDiscount                => 'R',
        rRevenue                 => 'U',
        currencyCode             => 'T',
        isPreOrder               => 'F',
        rDuration                => 'D',
        rChannel                 => 'W',
        rTransactionCategory     => 'D',
    }
}

sub _unverifiedFieldMap {
    {
        _conversionRate => 'V'
    };
}

sub productType    { shift->rProductType }
sub conversionRate { shift->_getByFieldName('_conversionRate') || 1 }

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

sub rProductType {
    my $self = shift;

    my $rProductType = $self->_getByFieldName('rProductType') || '';
    return BookPub::DB::Item::Sale::kProductTypeAudioBook if $rProductType =~ /^Audiobook$/i;
    return BookPub::DB::Item::Sale::kProductTypeEBook     if $rProductType =~ /^E-Book|$/i;

    $self->fail("Unable to determine rProductType from: '$rProductType'.");
}

sub rListPrice {
    my $self = shift;

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

    $rListPrice =~ s/,//g;
    if ( $self->listPriceRequired && $rListPriceCurrency ne $self->currencyCode ) {
        $rListPrice *= $self->conversionRate;
    }

    return $rListPrice;
}

sub rListPriceCurrency {
    my $self = shift;

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

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

    return $rListPriceCurrency;
}

sub rNativeListPrice {
    my $self = shift;

    my $rNativeListPrice = $self->_getByFieldName('rNativeListPrice') || 0;
    $rNativeListPrice =~ s/,//g;

    return $rNativeListPrice;
}

sub rPurchasePrice {
    my $self = shift;

    my $rPurchasePrice = $self->_getByFieldName('rPurchasePrice') || 0;
    $rPurchasePrice =~ s/,//g;
    return $rPurchasePrice;
}


1;