package BookPub::Import::Importer::XigXag::v2;

use strict;
use warnings;

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

use Date::Calc qw(Days_in_Month);

sub _fieldMap {
    my $self = shift;

    my $map = {
        'audiobook sales' => {
            rTitle      => 'B',
            rIsbn13     => 'A',
            countryCode => 'C',
            rUnits      => 'D',
            rUnitPrice  => 'G',
            rListPrice  => 'E',
            rRevenue    => 'H',
            rTaxAmount  => 'I',
        },
        'ebook sales' => {
            rTitle      => 'B',
            rIsbn13     => 'A',
            countryCode => 'C',
            rUnits      => 'D',
            rUnitPrice  => 'F',
            rListPrice  => 'F',
            rRevenue    => 'G',
            rComments   => 'H',
            isFree      => 'E',
        },
    };

    my $sheet = $self->sheetname || "";
    foreach (keys %$map) {
        return $map->{$_} if $sheet =~ /$_/i;
    }
    return undef;
}

sub _headerIdentifier { rTitle => 'Title' }

sub rServiceName       { 'XigXag' }
sub _useIsSaleRec      { 1 }
sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rPurchaseType      { $_[0]->purchaseType() }
sub rProductType       { $_[0]->productType() }
sub rPriceType         { 'rrp' }
sub priceType          { $_[0]->rPriceType() }
sub currencyCode       { 'GBP' }
sub rListPriceCurrency { 'GBP' }


my %months = (
    'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6,
    'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12,
);

sub dateBegin {
    my $self = shift;

    if ( my ($month, $year) = $self->sheetname =~ /^(\w{3}) (\d{4})/i ) {
        return sprintf("%04d-%02d-%02d", $year, $months{ lc $month }, 1);
    }

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

sub productType {
    my $self = shift;

    return BookPub::DB::Item::Sale::kProductTypeAudioBook if ($self->sheetname =~ /audiobook sales/i);
    return BookPub::DB::Item::Sale::kProductTypeEBook     if ($self->sheetname =~ /ebook sales/i);

    $self->fail("Unbale to determine product type");
}

sub rUnits {
    my $self = shift;

    my $units = $self->_getByFieldName('rUnits') || 0;

    $units =~ s/N\/A/0/ig;

    if ($self->sheetname =~ /ebook sales/){
        $self->fail("EBook sales should not contain more than 1 unit") if ($units > 1);
    }

    return $units;
}

sub rUnitPrice {
    my $self = shift;

    my $price = $self->_getByFieldName('rUnitPrice') || 0;
    $price =~ s/N\/A/0/ig;
    return $price;
}

sub rListPrice {
    my $self = shift;

    my $price = $self->_getByFieldName('rListPrice') || 0;
    $price =~ s/N\/A/0/ig;
    return $price;
}

sub rRevenue {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue') || 0;
    $revenue =~ s/N\/A/0/ig;
    return $revenue;
}

sub isFree {
    my $self = shift;

    if ($self->sheetname =~ /ebook sales/){
        my $free  = $self->_getByFieldName('isFree') || 0;
        $free =~ s/\%//g;
        return 'Y' if ($free < 0.1);
    }

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

sub _isSaleRec {
    my $self = shift;

    return if ($self->rUnitPrice =~ /total/i || !($self->rTitle && $self->rIsbn13));

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

1;
