package BookPub::Import::Importer::Booksio::v1;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';

use base 'BookPub::Import::Importer';


sub _fieldMap {
    {
        rServiceName        => 'BO',
        dateBegin           => 'O',
        dateEnd             => 'O',
        rProductType        => 'AD',
        rPriceType          => 'AN',
        rAuthor             => 'Y',
        rTitle              => 'X',
        rIsbn13             => 'U',
        rImprint            => 'AC',
        countryCode         => 'AL',
        rClientProductID    => 'AA',
        serviceProductID    => 'W',
        rReturns            => 'AG',
        rSales              => 'AF',
        rUnits              => 'AH',
        rUnitPrice          => 'AM',
        rListPrice          => 'BQ',
        rListPriceCurrency  => 'AO',
        rDiscount           => 'AP',
        rRevenue            => 'BC',
        currencyCode        => 'F',
        rFee                => 'AU',
        isFree              => 'AM',
        rDistributor        => 'N',
    }
}

sub _unverifiedFieldMap {
    {
        rFee1       => 'AX',
        rFee2       => 'BA',
        dateBeginAlt => 'G',
        dateEndAlt   => 'H',
    }
}

sub _headerIdentifier { rTitle => 'Product Title' }

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

sub rServiceName { shift->_getByFieldName('rServiceName') || 'BOOKSIO' }

sub productType {
    my $self = shift;

    my $productType = $self->_getByFieldName('rProductType') || '';

    return BookPub::DB::Item::Sale::kProductTypeEBook if $productType =~ /^E101$/i;
    return BookPub::DB::Item::Sale::kProductTypeAudioBook if $productType =~ /^audio$/i;

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

sub priceType {
    my $self = shift;

    my $priceType = $self->_getByFieldName('rPriceType') || '';

    return 'rrp'    if $priceType =~ /^01$/;
    return 'agency' if $priceType =~ /^4[13]$/;

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

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || $self->_getByFieldName('dateBeginAlt') || '';

    if ($date =~ /^(\d{4})(\d{2})(\d{2})$/) {
        return (sprintf "%04d-%02d-%02d", $1, $2, $3);
    }


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

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd') || $self->_getByFieldName('dateEndAlt') || '';

    if ($date =~ /^(\d{4})(\d{2})(\d{2})$/) {
        return (sprintf "%04d-%02d-%02d", $1, $2, $3);
    }
    $self->fail("Unable to determine date end from: $date");
}

sub rListPrice {
    my $self = shift;

    my $rListPrice = $self->_getByFieldName('rListPrice') || 0;
    my $rUnitPrice = $self->_getByFieldName('rUnitPrice') || 0;

    return $rUnitPrice if ($rUnitPrice && !$rListPrice);
}

sub rFee {
    my $self = shift;

    my $rFee = $self->_getByFieldName('rFee') || 0;
    my $rFee1 = $self->_getByFieldName('rFee1') || 0;
    my $rFee2 = $self->_getByFieldName('rFee2') || 0;

    return ($rFee + $rFee1 + $rFee2);
}

sub isFree {
    my $self = shift;

    return !$self->rListPrice ? 'Y' : 'N';
}

1;
