package BookPub::Import::Importer::Readbooks::Version4;

use strict;
use Date::Calc qw(Days_in_Month);

use lib '/app/tools/common/lib';
use Common::Country;

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

sub _headerIdentifier { ( rIsbn13 => 'ISBN' ) }

sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub priceType                  { 'agency+tax' }
sub _useIsSaleRec              { 1 }
sub _dateNormalization         { 'month' }
sub _autoParseTitleAndSubtitle { 1 }

sub dateBegin {
    my $self    = shift;
    my $dateStr = $self->filename;
    my $date;

    # date is in format YYYY-MM
    if ( $dateStr =~ /(\d{4})-(\d{1,2})/ ) {
        $date = sprintf( "%04d-%02d-%02d", $1, $2, 1 );
    } else {
        $date = undef;
    }

    return $date;
}

sub dateEnd {
    my $self    = shift;
    my $dateStr = $self->filename;
    my $date;

    # date is in format YYYY-MM
    if ( $dateStr =~ /(\d{4})-(\d{1,2})/ ) {
        $date = sprintf( "%04d-%02d-%02d", $1, $2, Days_in_Month( $1, $2 ) );
    } else {
        $date = undef;
    }

    return $date;
}

sub _isSaleRec {
    my $self = shift;

    if (   !$self->rTitle
        && !$self->rIsbn13
        && !$self->countryCode
        && !$self->rUnits
        && !$self->currencyCode ) {
        return 0;
    }
    return $self->BookPub::Import::Importer::_isSaleRec();
}

1;
