package BookPub::Import::Importer::24Symbols;

use strict;

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            serviceProductID => 'A',
            currencyCode     => 'K',
            dateBegin        => 'A1',
            dateEnd          => 'A2',
            rIsbn13          => 'D',
            rTitle           => 'B',
            rAuthor          => 'C',
            rImprint         => 'E',
            rUnits           => 'G',
            rListPrice       => 'H',
            rDiscount        => 'I',
            rRevenue         => 'J',
        },
        2 => {
            serviceProductID   => 'A',
            currencyCode       => 'O',
            countryCode        => 'G',
            dateBegin          => 'A1',
            dateEnd            => 'A2',
            rIsbn13            => 'D',
            rTitle             => 'B',
            rAuthor            => 'C',
            rImprint           => 'E',
            rUnits             => 'L',
            rListPrice         => 'J',
            rListPriceCurrency => 'I',
            rDiscount          => 'K',
            rRevenue           => 'P',
        },
    );

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

sub _headerIdentifier { ( rIsbn13 => 'isbn' ) }
sub _useIsSaleRec { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub countryCode                { 'ES' }
sub priceType                  { 'rrp' }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

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

    $isbn =~ s/-//g;

    return $isbn;
}

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

    if ( $date =~ /Start date: (\d{4}-\d{2}-\d{2})/ ) {
        $date = $1;
    }

    return $date;
}

sub _getDateEnd {
    my $self = shift;
    my $date = $self->_getByFieldName('dateEnd') || return;

    if ( $date =~ /End date: (\d{4}-\d{2}-\d{2})/ ) {
        $date = $1;
    }

    return $date;
}

###
1;    # Play nicely.
###
