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

use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( isbn => 'ISBN' ) }

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            countryCode  => 'L',
            dateBegin    => 'A',
            isbn         => 'B',
            rTitle       => 'C',
            rAuthor      => 'D',
            rComments    => 'E',
            rUnits       => 'G',
            rListPrice   => 'F',
            rDiscount    => 'H',
            rRevenue     => 'J',
            rInstitution => 'K',
            rChannel     => 'E',
        },
        2 => {
            dateBegin          => 'A',
            rPurchaseType      => 'E',
            rAuthor            => 'D',
            rTitle             => 'C',
            isbn               => 'B',
            rInstitution       => 'K',
            countryCode        => 'L',
            rUnits             => 'G',
            rUnitPrice         => 'I',
            rListPrice         => 'F',
            rListPriceCurrency => 'F2',
            rComments          => 'E',
            rDiscount          => 'H',
            rRevenue           => 'J',
            currencyCode       => 'I2',
            rChannel           => 'E',
        },
        3 => {
            dateBegin            => 'A',
            rAuthor              => 'D',
            rTitle               => 'C',
            isbn                 => 'B',
            rInstitution         => 'J',
            countryCode          => 'K',
            rUnits               => 'F',
            rListPrice           => 'E',
            rListPriceCurrency   => 'E2',
            rComments            => 'E',
            rDiscount            => 'G',
            rRevenue             => 'I',
            currencyCode         => 'I2',
            priceTypeQualifierID => 'J2',
        },
        4 => {
            rAuthor              => 'C',
            rTitle               => 'B',
            isbn                 => 'A',
            rInstitution         => 'I',
            countryCode          => 'J',
            rUnits               => 'E',
            rListPrice           => 'D',
            rDiscount            => 'F',
            rRevenue             => 'H',
            currencyCode         => 'H2',
            priceTypeQualifierID => 'I2',
        },
    );

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

sub _unverifiedFieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            subtotalText => 'I',
        },
        2 => {
            subtotalText => 'I',
        },
        3 => {
            subtotalText => 'H',
        },
        4 => {
            subtotalText => 'G',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType               { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

sub currencyCode {
    my $self = shift;
    if ($self->version() == 4) {
        return $self->_getByFieldName('currencyCode');
    }
    return "USD";
}

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

    $date =~ s/[A-z].+$//g;
    $date =~ s/[^0-9]//g;

    # Date format: 20170926 (YYYMMDD)
    # Let's check the first couple of characters for a clue on the format.
    if ( $date =~ /^20/ && $date =~ /^(\d{4})(\d{2})(\d{2})$/ ) {
        $date = $1 . "-" . $2 . "-" . $3;
    }

    # Date format: 01062015 (MMDDYYY)
    #
    elsif ( $date =~ /^(\d{2})(\d{2})(\d{4})$/ ) {
        $date = $3 . "-" . $1 . "-" . $2;
    }

    return $date;
}

sub priceType {
    my $self     = shift;
    my $clientID = Common::RSApp::GetClientID;

    if (
        $clientID == 346 || $clientID == 324    # Rowman Littlefield and Wiley
        || $clientID == 335                     # Berret-Koehler
        || $clientID == 310
      ) {                                       # DTMVTEST
        return 'rrp';
    } elsif ( $clientID == 318 ) {              #Macmillan US
        return 'agency';
    }

    $self->fail("Price Type not set for this client");
}

sub _isSaleRec {
    my $self = shift;

    my $subtotalText = lc( $self->_getByFieldName('subtotalText') ) || "";
    my $revenue = $self->_getByFieldName('rRevenue');

    # We're going to skip empty and subtotal lines.
    if ( $subtotalText =~ /total/i || $subtotalText eq "sold units" ) {
        return 0;
    }
    return 0 unless $revenue;

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

1;
