package BookPub::Import::Importer::Saxo::v1;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';

use base 'BookPub::Import::Importer';


sub _fieldMap {
    {
        dateBegin          => 'A',
        dateEnd            => 'A',
        rProductType       => 'E',
        rAuthor            => 'D',
        rTitle             => 'C',
        rIsbn13            => 'B',
        rUnits             => 'F',
        rListPriceCurrency => 'G',
        rRevenue           => 'H',
        currencyCode       => 'G',
    }
}

sub _headerIdentifier { rTitle => 'Title' }

sub rServiceName       { 'Saxo' }
sub priceType          { 'agency+tax' }
sub countryCode        { 'DK' }
sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType       { shift->rProductType }


sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';
    if ( $date =~ /(\d{4}).(\d{2}).(\d{2})/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    }

    $self->fail("Unable to determine dateBegin from '$date'");
}

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd') || '';
    if ( $date =~ /(\d{4}).(\d{2}).(\d{2})/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, $3;
    }

    $self->fail("Unable to determine dateEnd from '$date'");
}

sub rProductType {
    my $self = shift;

    my $type = $self->_getByFieldName('rProductType') || '';
    if ( $type =~ /^Audiobook$/i ) {
        return BookPub::DB::Item::Sale::kProductTypeAudioBook;
    }

    $self->fail("Unable to determine rProductType from '$type'");
}

sub rUnits {
    my $self = shift;

    my $units = $self->_getByFieldName('rUnits') || 0;
    return $units / 5;
}


1;
