package BookPub::Import::Importer::LearnOutLoud::Version1;

use strict;

use Common::Country;
use Common::Date;

use Date::Calc qw( Add_Delta_YMD  Add_Delta_YM );

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

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

sub _fieldMap {
    {
        countryCode => 'I',
        isbn        => 'H',
        rTitle      => 'B',
        rAuthor     => 'C',
        rUnits      => 'A',
        rListPrice  => 'D',
        rDiscount   => 'F',
        rRevenue    => 'G',
    };
}

sub priceType    { 'rrp' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub currencyCode { 'USD' }

sub _getDateBegin {
    my $self = shift;

    my $filename = $self->filename();

    $self->fail("Could not pull report date from filename: $filename")
      unless ( $filename =~ /(\d{2})(\d{2})(\d{2})/ );

    my ( $year, $month, $day ) = Add_Delta_YMD( "20$3", $1, $2, 0, -1, 0 );

    my $tmp = sprintf( "%04d%02d%02d", $year, $month, $day );
    return $tmp;
}

sub _dateFormat { [ 'numerical_year_first', 'iso' ] }

sub _dateNormalization { 'month' }

sub countryCode {
    my $self    = shift;
    my $country = $self->_getByFieldName('countryCode');

    my $obj = Common::Country->Get($country);
    return $obj->alpha2;
}
1;
