package BookPub::Import::Importer::Zinio::Version1;

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';

sub _headerIdentifier { ( countryCode => 'User Franchisee Country Name' ) }

sub _fieldMap {
    {
        currencyCode       => 'Y',
        countryCode        => 'P',
        dateBegin          => 'AE',
        rProductType       => 'C',
        rIsbn13            => 'E',
        rTitle             => 'B',
        rSales             => 'Q',
        rReturns           => 'Q',
        rListPrice         => 'U',
        rListPriceCurrency => 'T',
        rRevenue           => 'AD',
        conversionRate     => 'Z',
    };
}

sub _unverifiedFieldMap {
    {
        netSaleAmount => 'W',    # List price column is not populated for refunds, so we'll use this for those instead.
    };
}

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType    { 'rrp' }

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('rProductType');

    if ( $type eq 'Book' ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }

    return;
}

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

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

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

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

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

    return $isbn;
}

sub rListPrice {
    my $self = shift;

    my $sheet     = lc( $self->sheetname );
    my $listPrice = $self->_getByFieldName('rListPrice');
    if ( !$listPrice || $listPrice == 0 ) {
        $listPrice = $self->_getByFieldName('netSaleAmount');
    }
    $listPrice = abs($listPrice);

    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency');
    my $currencyCode      = $self->_getByFieldName('currencyCode');

    # We want to convert all list prices to the same currency as the revenue for RLPG
    if ( $currencyCode ne $listPriceCurrency && Common::RSApp::GetClientID == 346 ) {

        my $conversionRate = $self->_getByFieldName('conversionRate');
        $listPrice *= $conversionRate;
    }

    return $listPrice;
}

sub rListPriceCurrency {
    my $self = shift;

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

    # We're converting the list prices for RLPG, so we need to set this to the revenue's currency code.
    if ( Common::RSApp::GetClientID == 346 ) {
        $listPriceCurrency = $self->currencyCode;
    }

    return $listPriceCurrency;
}

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