package BookPub::Import::Importer::GooglePlay;

use strict;
use Data::Dumper;

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

sub _headerIdentifier { ( rIsbn13 => 'Primary ISBN' ) }

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        4 => {
            purchaseType           => 'C',
            isPreOrder             => 'E',
            currencyCode           => 'S',
            conversionRate         => 'U',
            countryCode            => 'P',
            dateBegin              => 'A',
            rIsbn13                => 'G',
            rTitle                 => 'I',
            rAuthor                => 'J',
            rImprint               => 'H',
            rUnits                 => 'F',
            rListPrice             => 'L',
            rListPriceCurrency     => 'K',
            rPurchasePrice         => 'N',
            rPurchasePriceCurrency => 'M',
            rDiscount              => 'Q',
            rRevenue               => 'R',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        4 => {
            transactionType       => 'D',
            listPriceTaxInclusive => 'N',
            listPriceTaxExclusive => 'O',
            preorderStatus        => 'E',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }

sub currencyCode {
    my $self              = shift;
    my $currencyCode      = $self->_getByFieldName('currencyCode');          # column S
    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency');    # column K
    my $revenue           = $self->rRevenue();                               # column R

    #    print STDERR "currencyCode: " . $currencyCode . " listPriceCurrency: " . $listPriceCurrency . " revenue: " . $revenue . "\n";

    if ( ( !defined $currencyCode || $currencyCode eq '' ) && $revenue == 0 ) {
        return $listPriceCurrency;
    }
    return $currencyCode;
}

sub conversionRate {
    my $self = shift;
    my $rate = $self->_getByFieldName('conversionRate');

    if ( !defined $rate || $rate eq '' ) {
        return 1;
    }
    return $rate;
}

sub priceType {
    my $self = shift;

    # For Macmillan UK, we're going to set the price type to RRP.
    if ( Common::RSApp::GetClientID == 359 ) {
        return 'rrp';
    }
    return 'agency';
}

sub purchaseType {
    my $self         = shift;
    my $purchaseType = $self->_getByFieldName('purchaseType');
    if ( $purchaseType =~ /Single Purchase/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } elsif ( $purchaseType =~ /Rental 90-Day/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } elsif ( $purchaseType =~ /Rental 90 Days/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } elsif ( $purchaseType =~ /Rental DAY_90/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } elsif ( $purchaseType =~ /Rental DAY_180/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } elsif ( $purchaseType =~ /Rental 180-Day/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } elsif ( $purchaseType =~ /Rental 180 Days/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } elsif ( $purchaseType =~ /school purchase/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } else {
        $self->fail("Unrecognized purchase type: $purchaseType");
    }
}

sub rDiscount {
    my $self     = shift;
    my $discount = $self->_getByFieldName('rDiscount');
    $discount =~ s/%//;
    $discount = ( 100 - $discount ) / 100;

    return $discount;
}

sub rSales {
    my $self            = shift;
    my $units           = $self->_getByFieldName('rUnits');
    my $transactionType = $self->_getByFieldName('transactionType');

    if ( $transactionType =~ /sale/i ) {
        return abs($units);
    }
}

sub rReturns {
    my $self            = shift;
    my $units           = $self->_getByFieldName('rUnits');
    my $transactionType = $self->_getByFieldName('transactionType');

    if ( $transactionType =~ /refund/i ) {
        return abs($units);
    }
}

sub rRevenue {
    my $self    = shift;
    my $rate    = $self->conversionRate();
    my $revenue = $self->_getByFieldName('rRevenue');
    $revenue =~ s/,//g;

    $revenue *= $rate;
    return $revenue;
}

sub rPurchasePrice {
    my $self  = shift;
    my $price = $self->_getByFieldName('rPurchasePrice');
    $price =~ s/,//g;

    return $price;
}

sub rTaxUnitPrice {
    my $self      = shift;
    my $listPrice = $self->_getByFieldName('rPurchasePrice');    # list price tax inclusive
    $listPrice =~ s/,//g;
    my $listPriceTaxExclusive = $self->_getByFieldName('listPriceTaxExclusive');
    $listPriceTaxExclusive =~ s/,//g;

    my $tax = $listPrice - $listPriceTaxExclusive;

    return $tax;
}

sub _isSaleRec {
    my $self = shift;

    # Ignore preorders that have not yet been fulfilled.
    my $preorder = $self->_getByFieldName('preorderStatus');
    return if ( $preorder =~ /^Authorized$/i );
    return if ( $preorder =~ /^Cancelled$/i );

    return $self->SUPER::_isSaleRec();
}

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