package BookPub::Import::Importer::ACX::Version3;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin            => 'L1',
        rAuthor              => 'D',
        rTitle               => 'C',
        countryCode          => 'E',
        serviceProductID     => 'B',
        rTransactionCategory => 'F',
    }
}

sub _unverifiedFieldMap {
    {
        rUnitsALC      => 'I',
        rUnitsAL       => 'L',
        rUnitsALOP     => 'O',
        rRevenueALC    => 'K',
        rRevenueAL     => 'N',
        rRevenueALOP   => 'Q',
        rListPriceALC  => 'J',
        rListPriceAL   => 'M',
        rListPriceALOP => 'P',
    }
}

sub _headerIdentifier { rTitle => 'Title' }

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _dateFormat                { 'text' }
sub _dateNormalization         { 'month' }
sub priceType                  { 'rrp' }
sub currencyCode               { 'USD' }
sub rListPriceCurrency         { 'USD' }
sub rServiceName               { 'ACX' }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType                { BookPub::DB::Item::Sale::kProductTypeAudioBook }

sub _getDateEnd { shift->_getDateBegin() }

sub rUnits {
    my $self = shift;

    my $saleType = $self->{_saleType};
    my $rUnits = $self->_getByFieldName('rUnits' . $saleType) || 0;

    return $rUnits;
}

sub rRevenue {
    my $self = shift;

    my $saleType = $self->{_saleType};
    my $rRevenue = $self->_getByFieldName('rRevenue' . $saleType) || 0;

    return $rRevenue;
}

sub rListPrice {
    my $self = shift;

    my $saleType = $self->{_saleType};
    my $rListPrice = $self->_getByFieldName('rListPrice' . $saleType) || 0;

    return $rListPrice / $self->rUnits;
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}

sub _processLine {
    my $self = shift;

    unless ( $self->_isSaleRec() ) {
        Common::Log::Print( " -- Ignore line sheet: " . $self->sheetname . " line: " . $self->linenum );
        $self->{_linesIgnored}++;
        return;
    }

    my $aSaleTypes = $self->_getSaleTypes();
    for my $saleType ( @$aSaleTypes ) {
        $self->{_saleType} = $saleType;
        $self->_saveLine();
        $self->{_linesImported}++;
    }

}

sub _getSaleTypes {
    my $self = shift;

    $self->{_saleTypes} = {
        AL   => 0,
        ALC  => 0,
        ALOP => 0,
    };

    foreach my $saleType ( keys %{ $self->{_saleTypes} } ) {
        my $units   = $self->_getByFieldName('rUnits' . $saleType) || 0;
        my $revenue = $self->_getByFieldName('rRevenue' . $saleType) || 0;
        $self->{_saleTypes}{$saleType}++ if $units || $revenue;
    }

    return [ grep { $self->{_saleTypes}{$_} } keys %{ $self->{_saleTypes} } ];
}

sub _isSaleRec {
    my $self = shift;

    return 0 if !$self->rTitle && !$self->rAuthor && !$self->countryCode;

    return 1;
}


1;
