package BookPub::Import::Importer::Odilo::Version9;

use strict;
use warnings;

use parent 'BookPub::Import::Importer';

sub _fieldMap {
    my $self = shift;

    my %map = (
        9 => {
            dateBegin                => 'A',
            rProductType             => 'C',
            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',
        },
        10 => {
            dateBegin                => 'F',
            rProductType             => 'H',
            rAuthor                  => 'K',
            rTitle                   => 'I',
            rIsbn13                  => 'G',
            rInstitution             => 'W',
            countryCode              => 'X',
            rUnits                   => 'S',
            rUnitPrice               => 'U',
            rListPrice               => 'R',
            rListPriceCurrency       => 'V',
            rNativeListPrice         => 'P',
            rNativeListPriceCurrency => 'O',
            rPurchasePrice           => 'P',
            rPurchasePriceCurrency   => 'O',
            rDiscount                => 'T',
            rRevenue                 => 'V',
            currencyCode             => 'V',
            rDuration                => 'N',
            rChannel                 => 'L',
            rModel                   => 'N',
        }
    );

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

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 rProductType {
    my $self = shift;

    my $rProductType = $self->_getByFieldName('rProductType') || '';
    if ( $rProductType =~ /^EBOOK$/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }

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

sub priceType {
    my $self = shift;

    my $clientID = Common::RSApp::GetClientID;
    return $clientID == 348 ? 'rrp+tax': 'rrp'; # MMAU(348)
}

sub priceTypeQualifierID {
    my $self = shift;

    my $clientID = Common::RSApp::GetClientID;
    # MMUS
    if ( $clientID == 318 && $self->filename =~ /Macmillan.Academic/i ) {
        return BookPub::DB::Item::PriceTypeQualifier::kSchoolLibrary;
    }

    return BookPub::DB::Item::PriceTypeQualifier::kCorporate;
}

sub isFree {
    my $self = shift;

    return $self->rUnits && !$self->rRevenue ? 'Y' : 'N';
}

sub _isSaleRec {
    my $self = shift;

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

    return if $subtotalText =~ /total/i || $subtotalText =~ /sold units/i;
    return 1 if ($dateBegin && $self->rIsbn13);

}

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->{_currencu_code} if $self->{_currencu_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. V
        for (my $i = @$sheet - 1; $i >= 0; $i--) {
            my $cell = $sheet->[$i][21] // next;
            if ( $cell =~ /^[-+]?\d+(?:[\.,]\d+)?\s+(\w{3})$/ ) {
                $self->{_currencu_code} = $1;
                last;
            }
        }
    }

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


1;
