package BookPub::Import::Importer::BoksalaStudenta::v1;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            dateBegin     => 'A',
            rPurchaseType => 'M',
            rAuthor       => 'C',
            rTitle        => 'B',
            rIsbn13       => 'E',
            countryCode   => 'J',
            rUnits        => 'L',
            rListPrice    => 'G',
            rDiscount     => 'H',
            rRevenue      => 'I',
            rDuration     => 'M',
        },
    );

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

sub _headerIdentifier { ( rIsbn13 => 'e-ISBN' ) }
sub _useIsSaleRec { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub currencyCode       { shift->{_currency} }
sub rListPriceCurrency { shift->{_currency} }
sub productType        { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType       { BookPub::DB::Item::Sale::kProductTypeEBook }
sub priceType          { 'rrp' }
sub rPriceType         { 'rrp' }
sub rServiceName       {'Boksala studenta'}
sub purchaseType       { shift->rPurchaseType() }

sub rPurchaseType {
    my $self  = shift;
    my $type = $self->_getByFieldName('rPurchaseType');
    if ( $type =~ /^perpetual$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    }
    $self->fail("Can't determine purchase type from $type");
}

sub _processHeader {
    my $self = shift;

    return unless $self->_isHeader;

    my $header = $self->_getByFieldName('rListPrice') || '';
    if ( $header =~ /^(\w{2})/ ) {
        $self->{_currency} = 'USD' if $1 =~ /^US/i;
        $self->{_currency} = 'GBP' if $1 =~ /^GB/i;
    }

    return $self->SUPER::_processHeader;
}


1;
