package BookPub::Import::Importer::RMBooks;

use strict;

use lib '/app/tools/common/lib';
use Common::Util qw(trimleadingzeros);

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

use Date::Calc qw( Add_Delta_YM );

my %priceTypeMap = (
    1  => 'rrp',
    2  => 'rrpplustax',
    41 => 'agency',
    42 => 'agencyplustax',
);

sub _headerIdentifier { ( rIsbn13 => 'Main Product ID' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            rServiceName         => 'BA',
            dateBegin            => 'L',
            dateEnd              => 'L',
            purchaseType         => 'W',
            rFormat              => 'V',
            priceType            => 'AC',
            rAuthor              => 'R',
            rTitle               => 'Q',
            rIsbn13              => 'P',
            rImprint             => 'U',
            rPostalCode          => 'AD',
            countryCode          => 'AE',
            rReturns             => 'AA',
            rSales               => 'Z',
            rUnits               => 'AB',
            rUnitPrice           => 'AF',
            rListPriceCurrency   => 'AH',
            rDiscount            => 'AN',
            rRevenue             => 'AP',
            currencyCode         => 'AH',
            rDuration            => 'X',
            rModel               => 'W',
            rTransactionCategory => 'Y',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            netValue => 'AL',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _dateFormat                { [ 'eur', 'iso' ] }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _isSaleRec {
    my $self = shift;

    # we'll only look at fields through transaction category as
    # they sometimes include totals starting with the units column
    my $offset = $self->_getOffset('rTransactionCategory');
    foreach my $cell ( @{ $self->line }[ 0 .. $offset ] ) {
        if ( $cell ne '' ) {
            return 1;
        }
    }

    return 0;
}

sub priceType {
    my $self = shift;
    my $type = $self->_getByFieldName('priceType') || return;
    $type = trimleadingzeros($type);

    $self->fail("Unknown price type '$type'") unless exists $priceTypeMap{$type};

    return $priceTypeMap{$type};
}

sub rFormat {
    my $self   = shift;
    my $format = $self->_getByFieldName('rFormat');

    if ( lc($format) eq 'e101' ) {
        return BookPub::DB::Item::Sale::kFormatTypeEPub;
    } elsif ( lc($format) eq 'e107' ) {
        return BookPub::DB::Item::Sale::kFormatTypePDF;
    }

    return $format;
}

sub purchaseType {
    my $self         = shift;
    my $purchaseType = $self->_getByFieldName('purchaseType');
    if ( $purchaseType =~ /rental/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } elsif ( $purchaseType =~ /purchase/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } else {
        $self->fail("Unrecognized purchase type: $purchaseType");
    }
}

sub discount {
    my $self = shift;
    my $fee  = $self->_getByFieldName('rDiscount');
    $fee =~ s/£//;
    my $netValue = $self->_getByFieldName('netValue');
    $netValue =~ s/£//;

    my $discount;

    if ( $netValue && $netValue != 0 ) {
        $discount = $fee / $netValue;
    }

    # Just going to leave discount blank if we end up with what would be an invalid figure for it.
    if ( $discount > 1 || $discount < 0 ) {
        return;
    }

    return $discount;
}

sub rRevenue {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('rRevenue');
    $revenue =~ s/£//;
    return $revenue;
}

sub rUnitPrice {
    my $self  = shift;
    my $price = $self->_getByFieldName('rUnitPrice');
    $price =~ s/£//;
    return $price;
}

1;
