package BookPub::Import::Importer::Shelfie;

use strict;

use Data::Dumper;
use Date::Simple;

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

sub _fieldMap {
    my $self     = shift;
    my %fieldMap = (
        1 => {
            dateBegin  => 'K',
            dateEnd    => 'J',
            rFormat    => 'A1',
            rAuthor    => 'F',
            rTitle     => 'E',
            rIsbn13    => 'A',
            rListPrice => 'L',
            rRevenue   => 'O',
            rFee       => 'M',
        },
    );

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

sub _headerIdentifier { ( rIsbn13 => 'EPUB' ) }
sub _useIsSaleRec { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _dateFormat { [ 'iso', 'us' ] }

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

sub countryCode        { 'US' }
sub rListPriceCurrency { 'USD' }
sub currencyCode       { 'USD' }

sub _getDateBegin {
    my $self = shift;
    my $date = substr( $self->_getByFieldName('dateBegin'), 0, 10 );

    return $date;
}

sub _getDateEnd {
    my $self = shift;
    my $date = substr( $self->_getByFieldName('dateEnd'), 0, 10 );

    return $date;
}

sub rFormat {
    my $self   = shift;
    my $format = $self->_getByFieldName('rFormat');

    if ( $format =~ /EPUB/i ) {
        return BookPub::DB::Item::Sale::kFormatTypeEPub;
    } else {
        $self->fail("Unexpected format type: $format");
    }
}

sub rUnits {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('rRevenue');

    $revenue < 0 ? return -1 : return 1;

}

1;
