package BookPub::Import::Importer::Odilo::Version11;

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';
use parent 'BookPub::Import::Importer::Odilo::Version9';

sub _fieldMap {
    {
        dateBegin                => 'A',
        rProductType             => 'C',
        priceTypeQualifierID     => 'T',
        rAuthor                  => 'F',
        rTitle                   => 'D',
        rIsbn13                  => 'B',
        rInstitution             => 'R',
        countryCode              => 'S',
        rUnits                   => 'N',
        rUnitPrice               => 'P',
        rListPrice               => 'M',
        rListPriceCurrency       => 'Q',
        rNativeListPrice         => 'K',
        rNativeListPriceCurrency => 'J',
        rPurchasePrice           => 'K',
        rPurchasePriceCurrency   => 'J',
        rDiscount                => 'O',
        rRevenue                 => 'Q',
        currencyCode             => 'Q',
        rDuration                => 'I',
        rChannel                 => 'G',
        rModel                   => 'I',
        rTransactionCategory     => 'T',
    }
}

sub _unverifiedFieldMap {
    {
        subtotalText => 'O',
    };
}

sub _headerIdentifier { rIsbn13 => 'ISBN' }

sub rServiceName  { 'Odilo' }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub rPurchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType   { $_[0]->rProductType() }
sub _useIsSaleRec { 1 }

sub dateBegin {
    my $self = shift;

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

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

sub priceTypeQualifierID {
    my $self = shift;

    my $type = $self->_getByFieldName('priceTypeQualifierID') || '';
    return BookPub::DB::Item::PriceTypeQualifier::kCorporate if  $type =~ /^(?:university|corporate|public)/i;
    return BookPub::DB::Item::PriceTypeQualifier::kSchoolLibrary if $type =~ /^school/i;
}

sub currencyCode {
    my $self = shift;

    # BBUSA
    if ( Common::RSApp::GetClientID() =~ /^(?:314|361)$/ ) {
        if ( $self->filename =~ /^RE_\w{3,9}_\d{4}_BloomsburyPublishing_(?:trade|academic|)_US_Licenses/i ) {
            return 'USD';
        }
    }
    # BBUK
    if ( Common::RSApp::GetClientID() =~ /^(?:624)$/ ) {
        if ( $self->filename =~ /^RE_\w{3,9}_\d{4}_BloomsburyPublishing_(?:trade|academic|)_GB_Licenses/i ) {
            return 'GBP';
        }
    }
    # MMUS
    elsif ( Common::RSApp::GetClientID() =~ /^(?:318)$/ ) {
        if ( $self->filename =~ /^RE_\w{3,9}_\d{4}_Macmillan_PPU/ ) {
            return 'USD';
        }
    }

    return $self->{_currency_code} if $self->{_currency_code};


    $self->fail("Unable to determine currency code.");
}

sub rListPriceCurrency {
    my $self = shift;

    return $self->currencyCode();
}

sub _processSheet {
    my ($self, $sheet, $sheetnum) = @_;

    # Don't look for the currency code for BBUSA
    if ( Common::RSApp::GetClientID() !~ /^(?:314|361)$/ ) {
        # get currency code from the footer in col. Q
        for (my $i = @$sheet - 1; $i >= 0; $i--) {
            my $cell = $sheet->[$i][16] // next;
            if ( $cell =~ /^[-+]?\d+(?:[\.,]\d+)?\s+(\w{3})$/ ) {
                $self->{_currency_code} = $1;
                last;
            }
        }
    }

    return $self->SUPER::_processSheet( $sheet, $sheetnum );
}

1;
