package BookPub::Import::Importer::HooplaDigital::v1;

use strict;
use warnings;
use POSIX;

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

use constant RE_YEAR_MONTH_DAY => qr/^(\d{4})[-.](\d{1,2})[-.](\d{1,2}).+$/;

sub _fieldMap {
    {
        dateBegin          => 'B',
        dateEnd            => 'B',
        rProductType       => 'F',
        rAuthor            => 'E',
        rTitle             => 'C',
        rIsbn13            => 'G',
        rInstitution       => 'K',
        rState             => 'L',
        rPostalCode        => 'M',
        countryCode        => 'L',
        rClientProductID   => 'I',
        rUnitPrice         => 'R',
        rListPrice         => 'N',
        rListPriceCurrency => 'O',
        rRevenue           => 'T',
        currencyCode       => 'U',
        rCOGS              => 'S',
    };
}

sub _headerIdentifier    { rIsbn13 => 'isbn' }
sub _useIsSaleRec        { 1 }
sub priceType            { 'rrp' }
sub purchaseType         { BookPub::DB::Item::Sale::kPurchaseTypeLoan }
sub priceTypeQualifierID { BookPub::DB::Item::PriceTypeQualifier::kCorporate }
sub isFree               { return ( shift->rRevenue == 0 ) ? 'Y' : 'N' }

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

sub dateBegin {
    my $self = shift;

    my $dateBegin = $self->_getByFieldName('dateBegin') || '';
    if ( my ($year, $month, $day) = $dateBegin =~ RE_YEAR_MONTH_DAY ) {
        return sprintf "%04d-%02d-%02d", $year, $month, $day;
    }

    $self->fail("Can't determine date: $dateBegin");
}

sub dateEnd { shift->dateBegin }

sub countryCode {
    my $self = shift;

    my $state = $self->SUPER::rState();
    if ( Common::Postal::US->new->getName($state) ) {
        return 'US';
    } elsif ( Common::Postal::CA->new->getName($state) ) {
        return 'CA';
    }

    $self->fail("'$state' is not a US state or CA province");
}

sub rRevenue {
    my $self = shift;

    my $rRevenue = $self->_getByFieldName('rRevenue') || 0;
    $rRevenue =~ s/[^\d\.-]//g;

    return $rRevenue;
}

sub rListPrice {
    my $self = shift;

    my $rListPrice = $self->_getByFieldName('rListPrice') || 0;
    $rListPrice =~ s/[^\d\.]//g;

    return $rListPrice;
}

sub rUnitPrice {
    my $self = shift;

    my $rUnitPrice = $self->_getByFieldName('rUnitPrice') || 0;
    $rUnitPrice =~ s/[^\d\.]//g;

    return $rUnitPrice;
}

sub rUnits {
    my $self = shift;

    my $cogs = $self->_getByFieldName('rCOGS') || "";

    ( ($self->rRevenue) > 0 ? return 1 : return -1 ) if $cogs;

    return 1 if $self->rRevenue == 0 || !$self->rUnitPrice == 0; # RSD-7436
    return ceil( $self->rRevenue / $self->rUnitPrice );
}

sub rProductType {
    my $self = shift;

    my $rProductType = $self->_getByFieldName('rProductType') || '';
    return BookPub::DB::Item::Sale::kProductTypeAudioBook if $rProductType =~ /^AUDIOBOOK$/i;
    return BookPub::DB::Item::Sale::kProductTypeEBook     if $rProductType =~ /^EBOOK/i;

    $self->fail("Couldn't determine product_type from: '$rProductType'");
}

sub productType { shift->rProductType }

sub _isSaleRec {
    my $self = shift;

    return if $self->{_endOfData};

    unless ( $self->SUPER::_isSaleRec ) {
        $self->{_endOfData} = 1;
        return;
    }

    return 1;
}


1;
