package BookPub::Import::Importer::BarnesNoble::Version7;

use strict;
use Data::Dumper;

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

use lib '/app/tools/sale_import/lib/';
use Import::ValidationError;

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

sub _headerIdentifier { ( rTitle => 'Title' ) }

sub _fieldMap {
    {
        currencyCode       => 'Y',
        countryCode        => 'L',
        dateBegin          => 'I',
        rIsbn13            => 'B',
        rTitle             => 'C',
        rAuthor            => 'D',
        rSales             => 'T',
        rReturns           => 'U',
        rListPrice         => 'P',
        rListPriceCurrency => 'AB',
        rPurchasePrice     => 'Q',
        rDiscount          => 'V',
        rUnitPrice         => 'Z',
        rRevenue           => 'AA',
    };
}

sub _unverifiedFieldMap {
    {
        eGiftDateBegin => 'J',
        publisher      => 'E',
    };
}

sub priceType {
    my $self = shift;

    my $publisher = $self->_getByFieldName('publisher');
    if ( lc($publisher) eq "guinness world records" ) {
        return 'rrp';
    }
    return 'agency';
}

sub productType { BookPub::DB::Item::Sale::kProductTypeEBook }

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

sub _getDateBegin {
    my $self = shift;

    # We'll use the eGift Date if that column is populated.
    my $date = $self->_getByFieldName('eGiftDateBegin');

    if ( !$date ) {
        $date = $self->_getByFieldName('dateBegin');
    }

    return $date;
}

sub rDiscount {
    my $self = shift;

    my $discount = $self->_getByFieldName('rDiscount');
    $discount =~ s/%//;
    $discount = ( 100 - $discount ) / 100;

    return $discount;
}

sub countryCode {
    my $self = shift;

    # 1 = US
    my $countryCode = $self->_getByFieldName('countryCode');
    if ( $countryCode == 1 ) {
        $countryCode = 'US';
    }

    return $countryCode;
}

sub rRevenue {
    my $self = shift;

    my $revenue = $self->SUPER::rRevenue();

    $revenue =~ s/,//g;

    my $units = $self->units || 0;

    if ( $revenue > 0 && $units < 0 ) {
        $self->fail("Positive revenue found with negative units");
        return undef;
    }

    if ( $revenue < 0 && $units > 0 ) {
        $self->fail("Negative revenue found with positive units");
        return undef;
    }

    return $revenue;
}

sub rReturns {
    my $self = shift;

    my $returns = $self->SUPER::rReturns();

    $returns =~ s/,//g;

    $returns *= -1;

    return $returns;
}

sub units {
    my $self = shift;

    my $sales = $self->rSales || 0;

    my $returns = $self->rReturns || 0;

    return $sales - $returns;
}

sub rTitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $title;
}

sub rSubtitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $subtitle;
}

1;
