package BookPub::Import::Importer::OReilly::Version1;

use strict;

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

sub _headerIdentifier { ( rTitle => 'Title, ISBN, Format' ) }

sub _fieldMap {
    {
        isbn       => 'G',
        rTitle     => 'F',
        rListPrice => 'L',
        rSales     => 'M',
        rReturns   => 'N',
        dateBegin  => 'C',
        rRevenue   => 'L',
    };
}

sub priceType    { 'agency' }
sub rServiceName { q(O'Reilly) }
sub currencyCode { 'USD' }
sub countryCode  { 'US' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

sub revenue {
    my $self = shift;
    my $rRev = $self->_getByFieldName('rRevenue');
    my $rDis = rDiscount();

    return $rRev * ( 1 - $rDis );
}

# The title is composed of title, ISBN, and format
# like so...
#       Professional Linux Programming, 1E (9781457107368.EBOOK)
#
sub rTitle {
    my $self  = shift;
    my $title = $self->_getByFieldName('rTitle');

    $title =~ s/\ \(.*\.EBOOK\)$//;

    return $title;
}

sub rUnits {
    my $self    = shift;
    my $returns = $self->_getByFieldName('rReturns');
    my $sales   = $self->_getByFieldName('rSales');

    my $units = $sales - $returns;

    return $units;
}

sub rReturns {
    my $self    = shift;
    my $returns = $self->_getByFieldName('rReturns');

    return $returns < 0 ? abs($returns) : 0;
}

sub rDiscount {
    my $self     = shift;
    my $clientID = Common::RSApp::GetClientID();

    if ( $clientID == 324 ) {    # Wiley
        return 0.30;
    } elsif ( $clientID == 335 ) {    # BKPub
        return 0.25;
    } else {
        $self->fail("Discount not set for this client");
        return undef;
    }
}

1;
