package BookPub::Import::Importer::SainsburysEntertainment::Version6;

use strict;

use lib '/app/tools/common/lib';
use Common::Country;

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        6 => {
            dateBegin              => 'K',
            rAuthor                => 'D',
            rTitle                 => 'B',
            rSubtitle              => 'C',
            rImprint               => 'G',
            isbn                   => 'A',
            rSales                 => 'L',
            rReturns               => 'O',
            rListPrice             => 'R',
            rListPriceCurrency     => 'P',
            rPurchasePrice         => 'T',
            rPurchasePriceCurrency => 'P',
            rRevenue               => 'Y',
            currencyCode           => 'P',
        },
        7 => {
            dateBegin              => 'K',
            rAuthor                => 'D',
            rTitle                 => 'B',
            rSubtitle              => 'C',
            rImprint               => 'G',
            isbn                   => 'A',
            rSales                 => 'L',
            rReturns               => 'O',
            rListPrice             => 'R',
            rListPriceCurrency     => 'P',
            rPurchasePrice         => 'T',
            rPurchasePriceCurrency => 'P',
            rRevenue               => 'W',
            currencyCode           => 'P',
            rTaxUnitPrice          => 'U',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        6 => {
            dateReturned => 'N',
        },
        7 => {
            dateReturned => 'N',
        },
    );

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

sub _headerIdentifier { ( rTitle => 'title' ) }

sub countryCode   { 'GB' }
sub priceType     { 'rrp' }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub _dateFormat   { ['iso'] }
sub _useIsSaleRec { 1 }

sub rAuthor {
    my $self   = shift;
    my $author = $self->_getByFieldName('rAuthor');

    # We just want the first name if there is a list.
    if ( $author =~ /^(.*?);/ ) {
        $author = $1;
    }

    return $author;
}

sub rDiscount {
    my $self = shift;
    my $discount;
    my $revenue = $self->_getByFieldName('rRevenue');
    my $list    = $self->_getByFieldName('rListPrice');

    if ( defined $list && $list != 0 ) {
        $discount = 1 - ( $revenue / $list );
    }

    return $discount;
}

sub rTaxUnitPrice {
    my $self = shift;
    my $tax;

    if ( defined $self->_getByFieldName('rTaxUnitPrice') ) {
        my $priceWithTax    = $self->_getByFieldName('rPurchasePrice');
        my $priceWithoutTax = $self->_getByFieldName('rTaxUnitPrice');
        $tax = $priceWithTax - $priceWithoutTax;
    }

    return $tax;
}

sub _getDateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateReturned');

    # client added a special date as an error code
    if ( !$date || $date eq '1-0-00' ) {
        $date = $self->SUPER::_getDateBegin() || return;
    }

    $date = substr( $date, 0, 10 );

    return $date;
}

sub _isSaleRec {
    my $self = shift;

    if (   !$self->rTitle
        && !$self->rSubtitle
        && !$self->rAuthor
        && !$self->rImprint
        && !$self->dateBegin ) {
        return 0;
    }
    return $self->BookPub::Import::Importer::_isSaleRec();
}

1;
