package BookPub::Import::Importer::Inktera;

use strict;

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            dateBegin          => 'O',
            rProductType       => 'AD',
            rPriceType         => 'BR',
            rAuthor            => 'Y',
            rTitle             => 'X',
            rIsbn10            => 'U',
            rIsbn13            => 'W',
            rImprint           => 'AC',
            countryCode        => 'AL',
            rReturns           => 'AF',
            rSales             => 'AG',
            rUnits             => 'AH',
            rUnitPrice         => 'AM',
            rListPrice         => 'BQ',
            rListPriceCurrency => 'AO',
            rDiscount          => 'AP',
            rRevenue           => 'BC',
            currencyCode       => 'I',
            isFree             => 'AI',
            rChannel           => 'BO',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            alt_rPriceType => 'AN',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _useIsSaleRec { 1 }

sub _dateFormat  { ['iso'] }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

sub _headerIdentifier { ( rTitle => 'Product title' ) }

sub isFree {
    my $self = shift;
    my $free = $self->_getByFieldName('isFree');

    if ( $free == 0 ) {
        return 'N';
    } elsif ( $free == 1 ) {
        return 'Y';
    } else {
        $self->fail("Unexpected value '$free' for isFree");
    }
}

sub productType {
    my $self = shift;
    my $type = $self->_getByFieldName('rProductType');

    if ( $type == 29 ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }

    return $type;
}

sub rListPrice {
    my $self      = shift;
    my $price     = $self->_getByFieldName('rListPrice');
    my $unitPrice = $self->_getByFieldName('rUnitPrice');

    if ( !$price ) {
        return $unitPrice;
    }

    return $price;
}

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin') || return;

    # format the date
    if ( $date =~ /(\d{4})(\d{2})(\d{2})/ ) {
        $date = $1 . "-" . $2 . "-" . $3;
    }

    return $date;
}

sub priceType {
    my $self         = shift;
    my $rPriceType   = $self->_getByFieldName('rPriceType');
    my $altPriceType = $self->_getByFieldName('alt_rPriceType');

    my $type = defined $rPriceType || $rPriceType eq '' ? $altPriceType : $rPriceType;

    if ( $type eq '43' ) {
        $type = 'agency';
    }

    return $type;
}

sub rReturns {
    my $self = shift;
    my $returns = $self->SUPER::rReturns() || return;

    return $returns < 0 ? abs($returns) : 0;
}

sub rSales {
    my $self = shift;
    my $sales = $self->SUPER::rSales() || return;

    return $sales >= 0 ? $sales : 0;
}

###
1;    # Play nicely.
###
