package BookPub::Import::Importer::Libri;

use strict;

use lib '/app/tools/common/lib';
use Common::Date;
use Common::Log;

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 = (
        2 => {
            currencyCode       => 'J',
            dateBegin          => 'T',
            rProductType       => 'R',
            rIsbn13            => 'E',
            rTitle             => 'G',
            rAuthor            => 'H',
            rUnits             => 'M',
            rListPrice         => 'N',
            rListPriceCurrency => 'O',
            rDiscount          => 'K',
            rRevenue           => 'I',
        },
    );

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

sub _headerIdentifier { ( rIsbn13 => 'ISBN13' ) }
sub _useIsSaleRec { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _dateNormalization         { 'month' }
sub priceType                  { 'rrp' }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub countryCode                { 'DE' }

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('rProductType');

    if ( lc($type) eq 'epb' ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }

    $self->fail("Unknown product type: $type");
}

sub _getDateBegin {
    my $self      = shift;
    my $dateBegin = $self->_getByFieldName('dateBegin');

    if ( $dateBegin =~ /(\d{2})\/(\d{2})\/(\d{4})/ ) {
        $dateBegin = $3 . "-" . $2 . "-" . $1;
    }

    return $dateBegin;
}

1;
