package BookPub::Import::Importer::Booktopia::Version3;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        3 => {
            serviceProductID   => 'A',
            currencyCode       => 'K',
            countryCode        => 'J',
            rIsbn13            => 'B',
            rTitle             => 'C',
            rAuthor            => 'D',
            rUnits             => 'E',
            rListPrice         => 'F',
            rListPriceCurrency => 'K',
            rTaxAmount         => 'H',    # actually price including tax
            rDiscount          => 'G',
            rRevenue           => 'I',
        },
        5 => {
            rAuthor            => 'D',
            rTitle             => 'C',
            rIsbn13            => 'B',
            countryCode        => 'K',
            serviceProductID   => 'A',
            rUnits             => 'E',
            rListPrice         => 'F',
            rListPriceCurrency => 'L',
            rDiscount          => 'H',
            rRevenue           => 'J',
            currencyCode       => 'L',
            rTaxAmount         => 'I',
        },
    );

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

sub rListPriceCurrency {
    my $self = shift;

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

    if ( Common::RSApp::GetClientID == 318 ) {    # always AUD for Macmillian US (MMUS)
        return 'AUD';
    }

    return $currency;
}

sub isFree {
    my $self = shift;

    my $discount = $self->_getByFieldName('rDiscount');
    $discount =~ s/%//g;

    return lc($discount) eq 'null' ? 'Y' : 'N';
}

sub rDiscount {
    my $self = shift;

    my $discount = $self->_getByFieldName('rDiscount');
    $discount =~ s/%//g;

    if ( $discount eq 'NULL' ) { $discount = 0 }

    return $discount && $discount > 1 ? $discount / 100 : $discount;
}

sub rRevenue {
    my $self = shift;

    return $self->_getByFieldName('rRevenue');
}

sub rTaxAmount {
    my $self = shift;

    my $pubShare = $self->_getByFieldName('rTaxAmount');    # actually price including tax
    my $revenue  = $self->_getByFieldName('rRevenue');

    return $pubShare - $revenue;
}

my %months = (
    january   => 1,
    february  => 2,
    march     => 3,
    april     => 4,
    may       => 5,
    june      => 6,
    july      => 7,
    august    => 8,
    september => 9,
    october   => 10,
    november  => 11,
    december  => 12,
);

sub dateBegin {
    my $self = shift;
    my $file = $self->filename;

    $file =~ /(\w{3,9}) -?\s?(\d{4})\./;

    $self->fail("Failed to determine start date from filename ($file)") unless ($1);

    my $startDate = sprintf( "%04d-%02d-%02d", $2, $months{ lc($1) }, 1 );
    my $date = new Common::Date( date => $startDate, type => 'iso' );

    return $date->monthBegin();
}

sub dateEnd {
    my $self = shift;
    my $file = $self->filename;

    $file =~ /(\w{3,9}) -?\s?(\d{4})\./;

    $self->fail("Failed to determine end date from filename ($file)") unless ($1);

    my $endDate = sprintf( "%04d-%02d-%02d", $2, $months{ lc($1) }, 1 );
    my $date = new Common::Date( date => $endDate, type => 'iso' );

    return $date->monthEnd();
}

1;
