package BookPub::Import::Importer::OLF::Base;

use strict;

use Common::Country;
use BookPub::Tracker::Service;

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

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

sub priceType    { 'rrp' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

sub currencyCode {
    my $self = shift;
    my $code = $self->SUPER::currencyCode() || return;

    # The currency code listed is LS, we aren't sure what that is. LS is the symbol
    # for Latvian Lats, but we don't think that's right.  According to the vendor
    # All sales are GBP, but we will blow up if we see something other than LS in this column.
    # Case 15139
    $self->fail("Unknown currency code '$code'") unless ( lc($code) eq 'ls' );

    return 'GBP';
}

sub _isValidSaleRecord {
    my $self = shift;

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

sub countryCode {
    my $self = shift;
    my $code = $self->SUPER::countryCode();

    my $countryObj = Common::Country->Get($code) || $self->fail("Unknown country code '$code'");

    return $countryObj->alpha2();
}

sub _processHeader {
    my $self = shift;
    $self->_storeDate();

    if ( $self->_isHeader() && !$self->dateEnd() ) {
        $self->fail("Date not found in header.");
    }
}

sub _storeDate {
    my $self = shift;

    unless ( $self->_getDateEnd() ) {
        my $input = $self->_getByFieldName('dateEnd');

        $self->{_dateEnd} = $input if ( $input && length($input) && $input !~ /[a-zA-Z]/ );
    }
}

sub _dateFormat { [ 'us', 'iso' ] }
sub _dateNormalization { 'month' }
sub _getDateBegin      { shift->_getDateEnd() }
sub _getDateEnd        { shift->{_dateEnd} }

1;
