package BookPub::Import::Importer::Gale::v1;

use strict;

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

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            dateEnd        => 'S2',
            rProductType   => 'C',
            rIsbn13        => 'B',
            rTitle         => 'C',
            rUnits         => 'D',
            rPurchasePrice => 'E',
            rCOGS          => 'H',
            rRevenue       => 'J',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            frequency => 'S3',
        },
    );

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

sub _headerIdentifier { ( rIsbn13 => 'PROD. NO' ) }
sub _useIsSaleRec     { 1 }
sub priceType         { 'rrp' }
sub purchaseType      { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub currencyCode      { 'USD' }
sub countryCode       { 'US' }
sub rPurchasePriceCurrency { 'USD' }

sub rUnits {

    # We need to make sure that the sign of the units matches the sign of the revenue
    # Follow the money...
    my $self    = shift;
    my $units   = $self->_getByFieldName('rUnits');
    my $revenue = $self->rRevenue;

    if ( ( $revenue < 0 && $units > 0 ) || ( $revenue > 0 && $units < 0 ) ) {
        $units *= -1;
    }

    return $units;
}

sub productType {
    my $self = shift;
    my $type = $self->rProductType;

    if ($type) {
        if ( $type =~ /^ebk/i ) {
            $self->{_productType} = BookPub::DB::Item::Sale::kProductTypeEBook;
            return $self->{_productType};
        }

        $self->fail("Could not determine product type from $type");
    }

    return $self->{_productType};
}

sub rTitle {
    my $self  = shift;
    my $title = $self->_getByFieldName('rTitle');

    if ($title) {
        if ( $title =~ /^\w{3}:\s?(.*)$/i ) {
            $self->{_title} = $1;
            return $self->{_title};
        }
        $self->fail("Could not determine title from $title");
    }
    return $self->{_title};
}

sub rIsbn13 {
    my $self = shift;
    my $isbn = $self->_getByFieldName('rIsbn13');

    if ($isbn) {
        $self->{_isbn} = $isbn;
        return $self->{_isbn};
    }
    return $self->{_isbn};
}

sub rPurchasePrice {
    my $self  = shift;
    my $price = $self->_getByFieldName('rPurchasePrice');
    my $units = $self->units();
    if ($units) {
        $price /= $units;
    }

    return $price;
}

sub _getDateBegin {
    my $self = shift;

    my $dateBegin;

    my $dateEnd   = $self->_getByFieldName('dateEnd');
    my $frequency = $self->_getByFieldName('frequency');

    if ( $dateEnd =~ /^(\d{4})-(\d{2})-\d{2}$/ ) {
        my $year  = $1;
        my $month = $2;

        if ( $frequency =~ /Quarterly/i ) {
            $month = $month - 2;
        } elsif ( $frequency =~ /Monthly/i ) {

            # Month stays the same, so we don't have to change anything here.
        } else {
            $self->fail("Unrecognized frequency: $frequency");
        }

        $dateBegin = sprintf( "%04d-%02d-%02d", $year, $month, 1 );
    }

    return $dateBegin;
}

sub _isSaleRec {
    my $self = shift;

    $self->SUPER::_isSaleRec || return 0;    # Bail out early on blank rows
    ( ( $self->rRevenue || $self->units ) && $self->rCOGS ) || return 0;    # Required fields or bail
    $self->rIsbn13 ne 'TOTAL CONTRACT' || return 0;                         # Ignore subtotal line.
    $self->rIsbn13 ne 'TOTAL'          || return 0;                         # Ignore other subtotal line.

    return 1;
}

1;
