package BookPub::Import::Importer::Slicebooks;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( rIsbn13 => 'Parent eBook ISBN13' ) }

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            rPriceType       => 'O',
            dateBegin        => 'AD',
            rClientProductID => 'L',
            rIsbn10          => 'E',
            rIsbn13          => 'F',
            rTitle           => 'Q',
            rAuthor          => 'R',
            rImprint         => 'W',
            rUnits           => 'X',
            rListPrice       => 'Z',
            rPurchasePrice   => 'AL',
            rDiscount        => 'AM',
            rRevenue         => 'AN',
        },
        2 => {
            rPriceType       => 'N',
            dateBegin        => 'AF',
            rProductType     => 'O',
            rClientProductID => 'K',
            rIsbn10          => 'D',
            rIsbn13          => 'E',
            rTitle           => 'P',
            rAuthor          => 'Q',
            rImprint         => 'V',
            rUnits           => 'W',
            rListPrice       => 'Y',
            rPurchasePrice   => 'AQ',
            rDiscount        => 'AR',
            rRevenue         => 'AS',
        },
        3 => {
            rPriceType       => 'N',
            dateBegin        => 'AG',
            rProductType     => 'O',
            rClientProductID => 'K',
            rIsbn10          => 'D',
            rIsbn13          => 'E',
            rTitle           => 'P',
            rAuthor          => 'Q',
            rImprint         => 'V',
            rUnits           => 'X',
            rListPrice       => 'Z',
            rPurchasePrice   => 'AR',
            rDiscount        => 'AS',
            rRevenue         => 'AU',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            secondHeaderCheck => 'A',
        },
        2 => {
            secondHeaderCheck => 'A',
        },
        3 => {
            secondHeaderCheck => 'A',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub currencyCode               { 'USD' }
sub countryCode                { 'US' }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

sub productType {
    my $self = shift;

    my $type = lc( $self->_getByFieldName('rProductType') );
    if ( $type eq 'ebook' || !$type ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }
    elsif ( $type =~ /slice|issue/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEChapter;
    }
    else {
        $self->fail("Unknown productType: '$type'");
    }
}

sub rPriceType {
    my $self = shift;

    my $priceType = $self->_getByFieldName('rPriceType');

    if ( $priceType =~ /^Non-Agency$/i ) {
        $priceType = 'rrp';
    }

    return $priceType;
}

sub _getDateBegin {
    my $self      = shift;
    my $dateBegin = $self->_getByFieldName('dateBegin');

    return substr( $dateBegin, 0, 10 );
}

sub _processLine {
    my $self = shift;

    # We're going to check for the second header
    my $secondHeaderCheck = $self->_getByFieldName('secondHeaderCheck');
    if ( $secondHeaderCheck =~ /eBook Sales Distributed by Ingram/i ) {
        $self->{_endOfData} = 1;
    }

    return $self->SUPER::_processLine(@_);
}

sub _isSaleRec {
    my $self = shift;

    # If we've set the end of data flag in processLine, we are done.
    return if ( $self->{_endOfData} );

    # Ignore the subtotal line.
    my $discount = $self->_getByFieldName('rDiscount');
    return if ( $discount =~ /^Total Amount Due$/i );

    # we require a title, ISBN, and author for a valid record
    if (   !$self->rTitle
        && !$self->rIsbn13
        && !$self->rAuthor
        && !$self->rUnits ) {
        return 0;
    }

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

1;
