package BookPub::Import::Importer::Google::Version15;

use strict;
use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::Assert;

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

sub _fieldMap {
    {
        isPreOrder         => 'E',
        currencyCode       => 'R',
        countryCode        => 'O',
        dateBegin          => 'B',
        rIsbn13            => 'H',
        rTitle             => 'K',
        rAuthor            => 'L',
        rImprint           => 'J',
        rUnits             => 'G',
        rSales             => 'D',
        rReturns           => 'D',
        rListPrice         => 'N',
        rListPriceCurrency => 'M',
        rDiscount          => 'P',
        rRevenue           => 'S',
    };
}

sub _unverifiedFieldMap {
    {
        eIsbn               => 'I',
        priceConversionRate => 'T',
    };
}

sub rListPriceCurrency {
    my $self     = shift;
    my $currency = $self->_getByFieldName('rListPriceCurrency');

    # For Macmillan and RLPG, we're going to set the currency code to USD.
    # We'll also be converting the list price to USD with the conversion rate
    if ( Common::RSApp::GetClientID == 318 || Common::RSApp::GetClientID == 346 ) {
        $currency = 'USD';
    }

    return $currency;
}

sub rListPrice {
    my $self      = shift;
    my $listPrice = $self->_getByFieldName('rListPrice');
    my $currency  = $self->_getByFieldName('rListPriceCurrency');

    # For Macmillan and RLPG, we're going to convert the list price to USD with the conversion rate
    #
    if ( ( Common::RSApp::GetClientID == 318 || Common::RSApp::GetClientID == 346 ) && $currency ne 'USD' ) {
        $listPrice =~ s/,//g;
        my $conversionRate = $self->_getByFieldName('priceConversionRate');
        if ( !$conversionRate ) {
            $self->fail("Conversion Rate missing");
        }
        $listPrice *= $conversionRate;
    }

    return $listPrice;
}

sub rIsbn13 {
    my $self    = shift;
    my $rIsbn13 = $self->_getByFieldName('rIsbn13');
    my $eIsbn   = $self->_getByFieldName('eIsbn');
    $rIsbn13 =~ s/'//g;
    $eIsbn =~ s/'//g;

    if ( !$eIsbn || $eIsbn eq 'N/A' ) {
        return $rIsbn13;
    }

    return $eIsbn;
}

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

    return $sales eq "Sale" ? 1 : 0;
}

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

    return $returns eq "Refund" ? 1 : 0;
}

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

    return 1 - $discount;
}

###
1;    # Play nicely.
###
