package BookPub::Import::Importer::Readwell;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( rIsbn13 => 'main_product_id' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            currencyCode       => 'Q',
            conversionRate     => 'AC',
            countryCode        => 'C',
            rState             => 'D',
            rPostalCode        => 'H',
            dateBegin          => 'I',
            rIsbn13            => 'K',
            rTitle             => 'M',
            rAuthor            => 'N',
            rImprint           => 'A',
            rUnits             => 'O',
            rListPrice         => 'AD',
            rListPriceCurrency => 'Q',
            rTaxAmount         => 'AB',
            rUnitPrice         => 'P',
        },
        2 => {
            currencyCode       => 'Q',
            conversionRate     => 'AI',
            countryCode        => 'C',
            rState             => 'D',
            rPostalCode        => 'H',
            dateBegin          => 'I',
            rIsbn13            => 'K',
            rTitle             => 'M',
            rAuthor            => 'N',
            rImprint           => 'A',
            rUnits             => 'O',
            rListPrice         => 'AJ',
            rListPriceCurrency => 'Q',
            rTaxAmount         => 'AH',
            rUnitPrice         => 'P',
        },
        3 => {
            dateBegin          => 'E',
            dateEnd            => 'F',
            rFormat            => 'AA',
            priceType          => 'BN',
            rAuthor            => 'V',
            rTitle             => 'U',
            rIsbn13            => 'S',
            rImprint           => 'Z',
            countryCode        => 'AH',
            rClientProductID   => 'X',
            serviceProductID   => 'T',
            rReturns           => 'AD',
            rSales             => 'AC',
            rUnits             => 'AE',
            rUnitPrice         => 'AI',
            rListPrice         => 'BM',
            rListPriceCurrency => 'AK',
            rDiscount          => 'AL',
            rRevenue           => 'AY',
            currencyCode       => 'I',
        },
        4 => {
            dateBegin          => 'D',
            dateEnd            => 'E',
            rFormat            => 'Z',
            priceType          => 'BM',
            rAuthor            => 'U',
            rTitle             => 'T',
            rIsbn13            => 'R',
            rImprint           => 'Y',
            countryCode        => 'AH',
            rClientProductID   => 'W',
            serviceProductID   => 'S',
            rReturns           => 'AC',
            rSales             => 'AB',
            rUnits             => 'AD',
            rUnitPrice         => 'AI',
            rListPrice         => 'BL',
            rListPriceCurrency => 'AK',
            rDiscount          => 'AL',
            rRevenue           => 'AY',
            currencyCode       => 'H',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType    { 'agency' }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

sub rUnitPrice {
    my $self  = shift;
    my $price = $self->_getByFieldName('rUnitPrice');

    return abs($price);
}

sub rRevenue {
    my $self = shift;

    my $units   = $self->units();
    my $price   = $self->rUnitPrice();
    my $revenue = $units * $price * .7;

    return $revenue;
}

1;
