package BookPub::Import::Importer::Zebralution::v1;

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';
use Date::Calc qw(Days_in_Month);
use POSIX;

use parent 'BookPub::Import::Importer';

sub _fieldMap {
    {
        dateBegin          => 'B',
        rPurchaseType      => 'N',
        rProductType       => 'R',
        rAuthor            => 'H',
        rTitle             => 'I',
        rIsbn13            => 'K',
        rImprint           => 'F',
        countryCode        => 'O',
        rClientProductID   => 'K',
        rUnits             => 'T',
        rRevenue           => 'Q',
        rDistributor       => 'L',
        rComments          => 'C',
    };
}

sub _unverifiedFieldMap {
    {
        currency => "Q"
    }
}
sub _headerIdentifier   { rAuthor => 'Author' }
sub _useIsSaleRec       { 1 }

sub rUnits        { shift->units() }
sub purchaseType  { shift->rPurchaseType() }
sub rProductType  { shift->_getByFieldName("rProductType") }
sub unitPrice     { shift->rUnitPrice() }
sub productType   { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub priceType     { 'rrp' }
sub rServiceName  { 'Zebralution' }

sub _processHeader {
    my $self = shift;

    my $currency = $self->_getByFieldName("currency") || "";
    if ($currency =~ /^Revenue-(\w+)$/) {
        $self->{_currency} = $1;
    }
}

sub rListPriceCurrency { $_[0]->{_currency} if $_[0]->{_currency} }
sub currencyCode { $_[0]->{_currency} if $_[0]->{_currency} }

sub rPurchaseType  {
    my $self = shift;

    my $type = $self->_getByFieldName('rPurchaseType') || "";
    if ( $type =~ /^(?:Subscription|Cloud)$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeStream ;
    }
    elsif ( $type =~ /^Download$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    }

    $self->fail("Unable to determine purchase type from: '$type'");
}

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';
    if ( $date =~ /^(\d{4})-(\d{2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, 1;
    }

    $self->fail("Can't determine dateBegin from: '$date'.");
}

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';
    if ( $date =~ /^(\d{4})-(\d{2})$/ ) {
        return sprintf "%04d-%02d-%02d", $1, $2, Days_in_Month( $1, $2 );
    }

    $self->fail("Can't determine dateEnd from: '$date'.");
}

sub units {
    my $self = shift;

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

    $units =~ s/,/\./g;

    return 1 if $units == 0 && $revenue;
    return ceil($units);
}

sub rRevenue {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue') || 0;

    $revenue =~ s/,/\./g;

    return $revenue;
}

sub rUnitPrice {
    my $self = shift;

    my $revenue = $self->rRevenue();
    my $units = $self->units();

    $revenue =~ s/,/\./g;

    return ($revenue / $units) if ($revenue && $units);
}

sub rComments {
    my $self = shift;

    # MMUK only
    return unless Common::RSApp::GetClientID() == 359;

    return $self->_getByFieldName('rComments');
}

sub _isSaleRec {
    my $self = shift;

    my $isbn   = $self->_getByFieldName('rIsbn13');
    my $title  = $self->_getByFieldName('rTitle');
    my $author = $self->_getByFieldName('rAuthor');

    return 0 unless $title && $author;

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

sub isFree {
    my $self = shift;

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

    return 'Y' if (!$revenue && $units);
    return 'N';
}

1;
