package BookPub::Import::Importer::Anobii::Version1;

use strict;

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

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

sub _fieldMap {
    {
        # Header
        currencyCode => 'G',

        # Detail
        countryCode => 'G',
        dateBegin   => 'A',
        isbn        => 'D',
        rTitle      => 'B',
        rAuthor     => 'E',
        rImprint    => 'F',
        rSales      => 'H',
        rReturns    => 'I',
        rListPrice  => 'L',
        rRevenue    => 'Q',

    };
}

sub _headerIdentifier { ( rTitle => 'TITLE & SUB-TITLE' ) }

sub priceType    { 'rrp' }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub currencyCode { shift->{_currencyCode} }
sub _dateFormat  { [ 'eur', 'iso' ] }

sub _processHeader { shift->_storeCurrencyCode() }

sub _storeCurrencyCode {
    my $self = shift;

    unless ( $self->{_currencyCode} ) {
        $self->{_currencyCode} = $self->_getByFieldName('currencyCode');
    }
}

sub _isValidSaleRecord {
    my $self = shift;
    if (   $self->_getByFieldName('dateBegin') eq 'TOTAL SALES'
        || $self->_getByFieldName('dateBegin') eq 'TOTAL REFUNDS'
        || $self->_getByFieldName('dateBegin') eq 'NET TOTAL' ) {
        return undef;
    }

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

sub rUnits {
    my $self    = shift;
    my $returns = $self->_getByFieldName('rReturns');
    my $sales   = $self->_getByFieldName('rSales');

    my $units = $sales - $returns;

    return $units;
}

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

    return $d;
}

1;
