package BookPub::Import::Importer::Alexi;

use strict;

use lib '/app/tools/bookpub/lib';

use base 'BookPub::Import::Importer';

sub _fieldMap {
    {
        dateBegin   => 'A1',
        rAuthor     => 'D',
        rTitle      => 'C',
        rIsbn13     => 'B',
        rImprint    => 'F',
        countryCode => 'A',
        rReturns    => 'L',
        rSales      => 'K',
        rUnits      => 'M',
        rDiscount   => 'N',
        rRevenue    => 'P',
    };
}

sub _unverifiedFieldMap {
    {
        listGBP      => 'G',
        listUSD      => 'H',
        listAUD      => 'I',
        listINR      => 'J',
        localRevenue => 'O',
    };
}

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

sub _useIsSaleRec { 1 }

sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL }
sub priceType    { 'rrp' }
sub currencyCode { 'GBP' }

sub _isSaleRec {
    return shift->rIsbn13 =~ /^\d{13}$/;
}

sub _dateNormalization { 'month' }

sub _getDateBegin {
    my $self = shift;

    my ( $day, $month, $year ) = $self->_getByFieldName('dateBegin') =~ /for (\d{2})\/(\d{2})\/(\d{4})/;

    return $year . '-' . $month . '-' . $day;
}

sub rListPriceCurrency {
    my $self = shift;

    my $currencyCode;
    if ( $self->_getByFieldName('listGBP') != 0 ) {
        $currencyCode = 'GBP';
    } elsif ( $self->_getByFieldName('listUSD') != 0 ) {
        $currencyCode = 'USD';
    } elsif ( $self->_getByFieldName('listAUD') != 0 ) {
        $currencyCode = 'AUD';
    } elsif ( $self->_getByFieldName('listINR') != 0 ) {
        $currencyCode = 'INR';
    }

    return $currencyCode;
}

sub rListPrice {
    my $self = shift;

    my $listPrice;
    if ( $self->_getByFieldName('listGBP') != 0 ) {
        $listPrice = $self->_getByFieldName('listGBP');
    } elsif ( $self->_getByFieldName('listUSD') != 0 ) {
        $listPrice = $self->_getByFieldName('listUSD');
    } elsif ( $self->_getByFieldName('listAUD') != 0 ) {
        $listPrice = $self->_getByFieldName('listAUD');
    } elsif ( $self->_getByFieldName('listINR') != 0 ) {
        $listPrice = $self->_getByFieldName('listINR');
    }

    return $listPrice;
}

sub listPrice {
    my $self = shift;

    my $listPrice;
    my $localRevenue = $self->_getByFieldName('localRevenue');
    if ($localRevenue) {
        $listPrice = Common::RSMath::round( ( $self->revenue / $localRevenue ) * $self->rListPrice, 2 );
    }

    return $listPrice;
}

sub discount {
    my $self = shift;

    if ( $self->rListPrice && $self->units ) {
        return Common::RSMath::round( 1 - ( ( $self->_getByFieldName('localRevenue') / $self->units ) / $self->rListPrice ), 2 );
    }
}

1;

