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

use strict;

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

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

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        3 => {
            currencyCode           => 'P',
            dateBegin              => 'A',
            isbn                   => 'D',
            rTitle                 => 'B',
            rSubtitle              => 'C',
            rAuthor                => 'E',
            rImprint               => 'G',
            rSales                 => 'H',
            rReturns               => 'I',
            rListPrice             => 'N',
            rListPriceCurrency     => 'P',
            rPurchasePrice         => 'J',
            rPurchasePriceCurrency => 'P',
            rTaxAmount             => 'L',
            rRevenue               => 'O',
        },
        5 => {
            currencyCode           => 'R',
            countryCode            => 'K',
            dateBegin              => 'H',
            isbn                   => 'A',
            rTitle                 => 'B',
            rSubtitle              => 'C',
            rAuthor                => 'D',
            rImprint               => 'F',
            rSales                 => 'I',
            rReturns               => 'J',
            rListPrice             => 'M',
            rListPriceCurrency     => 'R',
            rPurchasePrice         => 'N',
            rPurchasePriceCurrency => 'R',
            rTaxAmount             => 'O',
            rRevenue               => 'Q',
        },
    );

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

sub _headerIdentifier { ( rTitle => 'title' ) }
sub priceType         { 'rrp' }
sub productType       { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType      { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub _dateFormat { [ 'eur', 'iso' ] }

sub _useIsSaleRec { 1 }

sub countryCode {
    my $self = shift;

    if ( defined $self->_getByFieldName('countryCode') ) {
        my $country = $self->_getByFieldName('countryCode');
        $country = "GB" if ( $country =~ /^gbp$/i );    # Map currency code to country code
        return $country;
    } else {
        return 'GB';
    }
}

sub _isSaleRec {
    my $self = shift;

    # If we've set the end of data flag in processLine, we are done.
    return if ( $self->{_endOfData} );

    return $self->SUPER::_isSaleRec();
}

sub _processLine {
    my $self = shift;

    my $foundData = 0;

    foreach my $cell ( @{ $self->line } ) {
        if ( $cell ne '' ) {
            $foundData = 1;
        }
    }

    if ( !$foundData ) {
        $self->{_endOfData} = 1;
    }

    return $self->SUPER::_processLine(@_);
}

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 $unitPrice = $self->unitPrice();
    my $listPrice = $self->listPrice();

    if ($listPrice) {
        $discount = 1 - ( $unitPrice / $listPrice );
    }

    return $discount;
}

sub _getDateBegin {
    my $date = shift->SUPER::_getDateBegin() || return;
    my $d = substr( $date, 0, 10 );

    return $d;
}

1;
