package BookPub::Import::Importer::Kortext::Version3;

use strict;
use warnings;
use POSIX qw/ceil floor/;

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

sub _fieldMap {
    my $self = shift;

    my %map = (
        3 => {
            dateBegin          => 'A',
            dateEnd            => 'A',
            rFormat            => 'E',
            rAuthor            => 'F',
            rTitle             => 'D',
            rIsbn13            => 'C',
            rInstitution       => 'B',
            rUnits             => 'I',
            rUnitPrice         => 'O',
            rListPrice         => 'M',
            rDiscount          => 'N',
            rRevenue           => 'P',
            rChannel           => 'J',
            rModel             => 'K',
            rPurchaseType      => 'K',
            rListPriceCurrency => "L",
            currencyCode       => "L"
        },
        4 => {
            dateBegin          => 'A',
            dateEnd            => 'A',
            rPurchaseType      => 'K',
            rFormat            => 'E',
            priceType          => 'N',
            rAuthor            => 'F',
            rTitle             => 'D',
            rIsbn13            => 'C',
            rInstitution       => 'B',
            rUnits             => 'I',
            rUnitPrice         => 'P',
            rListPrice         => 'N',
            rListPriceCurrency => 'M',
            rDiscount          => 'O',
            rRevenue           => 'Q',
            currencyCode       => 'M',
            rChannel           => 'J',
        },
        5 => {
            dateBegin          => 'A',
            dateEnd            => 'A',
            rPurchaseType      => 'K',
            rFormat            => 'E',
            rAuthor            => 'F',
            rTitle             => 'D',
            rIsbn13            => 'C',
            rInstitution       => 'B',
            countryCode        => 'J',
            rUnits             => 'L',
            rListPrice         => 'N',
            rListPriceCurrency => 'I',
            rDiscount          => 'AC',
            rRevenue           => 'AE',
            currencyCode       => 'I',
            rComments          => 'AF',
        },
    );

    return $map{ $self->_version };
}

sub _unverifiedFieldMap {
    my $self = shift;
    my %map = (
        3 => {},
        4 => {},
        5 => {
            _altUnits => 'M',
        },
    );

    return $map{ $self->_version };
}

sub currencyCode       { shift->_getByFieldName("currencyCode") }
sub rListPriceCurrency { shift->_getByFieldName("rListPriceCurrency") }

sub dateBegin {
    my $self = shift;

    my $date =  $self->_getByFieldName('dateBegin') || '';
    if ( $date =~ /^\d+\.?\d*$/ && $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
    }

    return $date;
}

sub dateEnd {
    my $self = shift;

    my $date =  $self->_getByFieldName('dateEnd') || '';
    if ( $date =~ /^\d+\.?\d*$/ && $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
    }

    return $date;
}

sub priceType {
    my $self = shift;

    if ( $self->_version == 4 ) {
        return 'rrp' if $self->_getByFieldName('priceType') =~ /^Ex Tax RRP$/i;
    }

    return 'rrp' if $self->_version == 5;

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

sub rRevenue {
    my $self = shift;

    my $rRevenue = $self->_getByFieldName('rRevenue') || 0;
    $rRevenue =~ s/[^\d\.-]//g;

    return $rRevenue;
}

sub rUnitPrice {
    my $self = shift;

    my $rUnitPrice = $self->_getByFieldName('rUnitPrice') || 0;
    $rUnitPrice =~ s/[^\d\.]//g;

    return $rUnitPrice;
}

sub rListPrice {
    my $self = shift;

    my $rListPrice = $self->_getByFieldName('rListPrice') || 0;
    if ($self->_version == 5) {
        # Get first non-empty value from columns O through AB
        (my $rListPriceAlt) = grep {$_} @{ $self->{line} }[14..27];
        $rListPrice = $rListPriceAlt if $rListPriceAlt;
    }

    $rListPrice =~ s/[^\d\.]//g;

    return $rListPrice;
}

sub rUnits {
    my $self = shift;

    my $rUnits = $self->_getByFieldName('rUnits') || $self->_getByFieldName('_altUnits') || 0;
    $self->fail("Error the file: No units, but revenue reported.") if !$rUnits && $self->rRevenue;
    return $rUnits < 0 ? floor( $rUnits ) : ceil( $rUnits );
}

sub rPurchaseType {
    my $self = shift;

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

    if ( $rPurchaseType =~ /^Special/i ) {
        my $rIsbn13 = $self->_getByFieldName('rIsbn13') || '';
        my $rFormat = $self->_getByFieldName('rFormat') || '';
        my $rAuthor = $self->_getByFieldName('rAuthor') || '';

        $self->fail("Missing ISBN") unless ( $rIsbn13 && $rFormat && $rAuthor );
    }

    return $rPurchaseType;
}

sub isFree {
    my $self = shift;

    return (((!$self->rRevenue || $self->rRevenue =~ /^0.0+/) && $self->rUnits) ? 'Y' : 'N');
}

sub _isSaleRec {
    my $self = shift;

    my $units     = $self->rUnits();
    my $unitPrice = $self->_getByFieldName('rUnitPrice');
    my $title     = $self->_getByFieldName('rTitle');

    return unless ($units || $unitPrice);

    $self->fail("rTitle value can't be blank") unless $title;
}

1;
