package BookPub::Import::Importer::Bookwire::v1;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin                => 'A',
        dateEnd                  => 'B',
        rPurchaseType            => 'P',
        purchaseType             => 'P',
        rProductType             => 'N',
        rAuthor                  => 'L',
        rTitle                   => 'K',
        rIsbn13                  => 'J',
        countryCode              => 'I',
        rReturns                 => 'Y',
        rSales                   => 'Y',
        rUnits                   => 'Y',
        rNativeListPrice         => 'Q',
        rNativeListPriceCurrency => 'S',
        rCOGS                    => 'X',
        rRevenue                 => 'Z',
        currencyCode             => 'U',
        rDistributor             => 'G',
        rChannel                 => 'H',
        rTransactionCategory     => 'O',
    };
}


sub _headerIdentifier { rTitle => 'Title' }
sub priceType         { 'rrp' }
sub _useIsSaleRec     { 1 }

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

sub dateBegin {
    my $self = shift;

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

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

sub dateEnd {
    my $self = shift;

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

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

sub purchaseType {
    my $self = shift;

    my $type = $self->_getByFieldName('purchaseType') || '';
    if ( $type =~ /^download$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } elsif ( $type =~ /^library$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } elsif ( $type =~ /^(?:flat-)?subscription$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    }

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

sub rProductType {
    my $self = shift;

    my $type = $self->_getByFieldName('rProductType') || '';
    if ( $type =~ /^(?:EPUB|FixedLayout|iBooksAuthor)$/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ( $type =~ /^(?:Audiobook|Audiotrack)$/i ) {
        return BookPub::DB::Item::Sale::kProductTypeAudioBook;
    }

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

sub productType { shift->rProductType }


sub rReturns {
    my $self = shift;

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

    return $self->rRevenue < 0 ? abs $rUnits : 0;
}

sub rSales {
    my $self = shift;

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

    return $self->rRevenue > 0 ? abs $rUnits : 0;
}

sub units {
    my $self = shift;

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

    return $rUnits;
}

sub rRevenue {
    my $self = shift;

    my $rRevenue = $self->_getByFieldName('rRevenue') || 0;
    $rRevenue =~ s/,/./g;

    return $rRevenue;
}

sub rNativeListPrice {
    my $self = shift;

    my $rNativeListPrice = $self->_getByFieldName('rNativeListPrice') || 0;
    $rNativeListPrice =~ s/,/./g;

    return $rNativeListPrice;
}

sub rCOGS {
    my $self = shift;

    my $rCOGS = $self->_getByFieldName('rCOGS') || 0;
    $rCOGS =~ s/,/./g;

    return $rCOGS > 1 ? $rCOGS * 0.01 : $rCOGS;
}

1;
