package BookPub::Import::Importer::LSI::Version2;

use strict;
use warnings;

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

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


sub _fieldMap {
    {
        dateBegin                => 'AO',
        dateEnd                  => 'AO',
        rFormat                  => 'I',
        rAuthor                  => 'G',
        rTitle                   => 'F',
        rIsbn10                  => 'C',
        rIsbn13                  => 'BO',
        rImprint                 => 'BI',
        countryCode              => 'BX',
        rClientProductID         => 'A',
        serviceProductID         => 'D',
        rReturns                 => 'AQ',
        rSales                   => 'M',
        rUnits                   => 'AY',
        rUnitPrice               => 'R',
        rListPrice               => 'K',
        rListPriceCurrency       => 'AN',
        rNativeListPrice         => 'K',
        rNativeListPriceCurrency => 'AN',
        rDiscount                => 'L',
        rRevenue                 => 'AZ',
        currencyCode             => 'AN',
        rFee                     => 'U',
        rDistributor             => 'B',
        rComments                => 'BN',
    };
}

sub _headerIdentifier { rTitle => 'title' }

sub rServiceName { 'Ingram Digital LSI' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypePrintOnDemand }
sub productType  { BookPub::DB::Item::Sale::kProductTypePhysicalBook }
sub priceType    { 'rrp' }

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName("dateBegin") || '';
    my ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $date );
    return $dateBegin if $dateBegin;

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

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName("dateBegin") || '';
    my ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $date );
    return $dateEnd if $dateEnd;

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

sub countryCode {
    my $self = shift;

    my $code = $self->_getByFieldName("countryCode") || '';
    return 'US' if $code =~ /US\s?-\s?Ingram-GAP/i;

    if ( $code =~ /LS\s?-\s?Euro/i ) {
        return 'GB' if Common::RSApp::GetClientID == 318;    # MMUS
        $self->fail("Unable to determine countryCode from '$code' for this client");
    }

    if ( $code =~ /^(LS|Global)\s*-\s*(.+)$/i ) {
        my $oCountry = Common::Country->Get($2);
        return $oCountry->alpha2() if $oCountry;
    }

    $self->fail("Unable to determine countryCode from '$code'");
}

sub formatType {
    my $self = shift;

    my $rFormat = $self->_getByFieldName("rFormat") || '';
    return $self->_parseBinding($rFormat) || $rFormat;
}

sub rSales {
    my $self = shift;

    my $rSales = $self->_getByFieldName("rSales") || 0;
    return abs($rSales);
}

sub rReturns {
    my $self = shift;

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

sub rUnits {
    my $self = shift;

    my $rRevenue = $self->_getByFieldName("rRevenue") || 0;
    my $rUnits   = $self->_getByFieldName("rUnits")   || 0;

    if ( $rRevenue >= 0 ) {
        $rUnits = abs($rUnits);
    } else {
        $rUnits = abs($rUnits) * -1;
    }

    return $rUnits;
}

sub discount {
    my $self = shift;

    return ($self->listPrice * 1)
        ? 1 - ( $self->unitPrice / $self->listPrice )
        : 0;
}

sub isFree {
    my $self = shift;

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


1;