package BookPub::Import::Importer::EBook::Version13;

use strict;

use Data::Dumper;

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

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

sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub _autoParseTitleAndSubtitle { 1 }

sub _fieldMap {
    {
        countryCode        => 'O',
        dateBegin          => 'L',
        rFormat            => 'I',
        rIsbn13            => 'G',
        rTitle             => 'D',
        rAuthor            => 'E',
        rImprint           => 'C',
        rComments          => 'T',
        rListPrice         => 'P',
        rListPriceCurrency => 'Q',
        rDiscount          => 'R',
        rRevenue           => 'S',
        currencyCode       => 'Q',
    };
}

sub _unverifiedFieldMap {
    { fIsbn13 => 'H', };
}

sub currencyCode {
    my $self = shift;
    my $code = $self->_getByFieldName('currencyCode');

    return substr( $code, 0, 3 );
}

sub rListPriceCurrency {
    my $self = shift;
    my $code = $self->_getByFieldName('rListPriceCurrency');

    return substr( $code, 0, 3 );
}

sub isFree {
    my $self  = shift;
    my $price = $self->_getByFieldName('rListPrice');

    return $price eq 0 ? 'Y' : 'N';
}

sub rIsbn13 {
    my $self = shift;
    my $isbn = $self->_getByFieldName('rIsbn13');

    my $fisbn = $self->_getByFieldName('fIsbn13');

    if ( defined($isbn) && $isbn ne '' ) {
        return $isbn;
    } else {
        return $fisbn;
    }
}

sub _isValidSaleRecord {
    my $self    = shift;
    my $fIsbn13 = $self->_getByFieldName('fIsbn13');

    return $self->rTitle && ( $self->rIsbn10 || $self->rIsbn13 || $fIsbn13 || $self->rRevenue ) && $self->dateBegin;
}

sub units {
    my $revenue = shift->rRevenue || return 1;
    return $revenue < 0 ? -1 : 1;
}

1;
