package BookPub::Import::Importer::SoomoLearing::v1;

use strict;
use warnings;

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

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

sub _fieldMap {
    {
        rServiceName => 'A1',
        rAuthor      => 'C',
        rTitle       => 'C',
        rIsbn13      => 'C',
        rInstitution => 'A3',
        rState       => 'A3',
        rPostalCode  => 'A3',
        rUnits       => 'D',
        rUnitPrice   => 'E',
        rListPrice   => 'E',
        rRevenue     => 'F',
        isFree       => 'F D',
    };
}

sub _unverifiedFieldMap {
    {
        stmtMonth => 'A',
        stmtYear  => 'A2'
    }
}

sub _headerIdentifier { rUnits => 'Provisioning' }

sub _useIsSaleRec { 1 }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rPurchaseType { shift->purchaseType }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType  { shift->productType }
sub priceType     { 'rrp' }
sub countryCode   { 'US' }
sub currencyCode  { 'USD' }
sub rListPriceCurrency { 'USD' }


my %months = (
    'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6,
    'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12
);

sub rServiceName {
    my $self = shift;

    my $service = $self->_getByFieldName('rServiceName') || '';
    if ( $service =~ /Soomo Learning LLC/i ) {
        return 'Soomo Learning';
    }

    $self->fail("Unable to determine rServiceName from '$service'");
}

sub dateBegin {
    my $self = shift;

    my $yearStr = $self->_getByFieldName('stmtYear')  || '';
    unless ( $self->{_year} ) {
        ($self->{_year}) = $yearStr =~ /.*(\d{4})$/;
    }

    my $monthStr = $self->_getByFieldName('stmtMonth') || '';
    my $month = $months{ lc( substr($monthStr, 0, 3) ) };

    unless ( $self->{_year} && $month ) {
        $self->fail("Unable to determine dateBegin from '$yearStr' and '$monthStr'");
    }

    return sprintf "%04d-%02d-%02d", $self->{_year}, $month, 1;
}

sub rAuthor {
    my $self = shift;

    my $str = $self->_getByFieldName('rAuthor') || '';
    my ($rAuthor) = $str =~ /]\s*(.+)$/;

    return $rAuthor;

}

sub rTitle {
    my $self = shift;

    my $str = $self->_getByFieldName('rTitle') || '';
    my ($rTitle) = $str =~ /^(.*?)\s*,/;

    $self->fail("Unable to determine rTitle from '$str'") unless $rTitle;

    return $rTitle;
}

sub rIsbn13 {
    my $self = shift;

    my $str = $self->_getByFieldName('rTitle') || '';
    my ($rIsbn13) = $str =~ /\[ISBN(.*?)\]/;
    $rIsbn13 =~ s/-//g;

    return $rIsbn13;
}

sub rInstitution {
    my $self = shift;

    return $self->{_r_institution} if $self->{_r_institution};

    my $str = $self->_getByFieldName('rInstitution') || '';
    my ($rInstitution) = $str =~ /^for (.*?),/;

    return $self->{_r_institution} = $rInstitution;
}

sub rState {
    my $self = shift;

    return $self->{_r_state} if $self->{_r_state};

    my $str = $self->_getByFieldName('rState') || '';
    my ($rState) = $str =~ /, ([^,]+) \d+$/;

    return $self->{_r_state} = $rState;
}

sub rPostalCode {
    my $self = shift;

    return $self->{_r_postal_code} if $self->{_r_postal_code};

    my $str = $self->_getByFieldName('rPostalCode') || '';
    my ($rPostalCode) = $str =~ /, [^,]+ (\d+)$/;

    return $self->{_r_postal_code} = $rPostalCode;
}

sub rRevenue {
    my $self = shift;

    return $self->rUnits * $self->rListPrice;
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}

sub _isSaleRec {
    my $self = shift;

    $self->SUPER::_isSaleRec();

    my $rAuthor = $self->_getByFieldName('rAuthor') || '';
    return 0 if $rAuthor =~ /Total Due/i;

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


1;
