package BookPub::Import::Importer::LSI::Version4;

use strict;

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

sub _dateFormat { [ 'us', 'iso' ] }
sub _isSaleRec  { 1 }

sub _fieldMap {
    {
        dateBegin          => 'O',
        dateEnd            => 'O',
        rPriceType         => 'J',
        rAuthor            => 'Y',
        rTitle             => 'X',
        rIsbn13            => 'U',
        rImprint           => 'AC',
        countryCode        => 'AL',
        serviceProductID   => 'W',
        rReturns           => 'AG',
        rSales             => 'AF',
        rUnits             => 'AH',
        rUnitPrice         => 'AS',
        rListPrice         => 'AM',
        rListPriceCurrency => 'AO',
        rDiscount          => 'AP',
        rRevenue           => 'BC',
        currencyCode       => 'I',
        rFee               => 'AU',
        rChannel           => 'N',
        rProductType       => 'AD',
    };
}

sub productType {
	my $self = shift;
	my $type = $self->_getByFieldName("rProductType");

	return BookPub::DB::Item::Sale::kProductTypeEBook unless $type;
	return BookPub::DB::Item::Sale::kProductTypeEBook if $type =~ /^ebook$/i;
	return BookPub::DB::Item::Sale::kProductTypeAudioBook if $type =~ /^eaudio$/i;

    $self->fail("Unknown product type '$type'");
}

sub priceType {
    my $self = shift;
    my $type = lc( $self->rPriceType ) || return;

    $type = 'rrp' if ( $type =~ /^wholesale$/i );
    $type = 'agency'  if ($type =~ /^agency$/i);

    return $type;
}

sub rProductType { shift->productType() }

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

sub _getDateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';
    # strip off the time stamp
    $date =~ s/ \d{1,2}:\d{1,2}(?::\d{1,2})$//;

    # MM/DD/YY
    if ( $date =~ /^(\d{1,2})\/(\d{1,2})\/(\d{1,2})$/ ) {
        return sprintf( "20%02d-%02d-%02d", $3, $1, $2 );
    }
    # YYYY-MM-DD 2019-12-21
    elsif ( $date =~ /^(\d{4})[\/.-](\d{1,2})[\/.-](\d{1,2})$/ ) {
        return sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    }

    $self->fail("Unable to determine date_begin from: '$date'.");
}

sub _getDateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd') || '';
    # strip off the time stamp
    $date =~ s/ \d{1,2}:\d{1,2}(?::\d{1,2})$//;
    
    # MM/DD/YY
    if ( $date =~ /^(\d{1,2})\/(\d{1,2})\/(\d{1,2})$/ ) {
        return sprintf( "20%02d-%02d-%02d", $3, $1, $2 );
    }
    # YYYY-MM-DD 2019-12-21
    elsif ( $date =~ /^(\d{4})[\/.-](\d{1,2})[\/.-](\d{1,2})$/ ) {
        return sprintf( "%04d-%02d-%02d", $1, $2, $3 );
    }

    $self->fail("Unable to determine date_end from: '$date'.");
}

sub _isSaleRec {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin');
    return 0 unless $date;

    return $self->SUPER::_isSaleRec();
}

sub rReturns {
    my $self = shift;

    my $returns = $self->_getByFieldName('rReturns') || 0;
    return abs $returns;
}

###
1;    # Play nicely.
###
