package BookPub::Import::Importer::VitalSource;

use strict;

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

use Date::Calc qw( Add_Delta_YM );

sub _headerIdentifier { ( rTitle => 'TITLE' ) }

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

    my %fieldMap = (
        5 => {
            purchaseType => 'B',
            currencyCode => 'P',
            dateBegin    => 'D',
            dateEnd      => 'D',
            isbn         => 'F',
            rTitle       => 'E',
            rUnits       => 'J',
            rListPrice   => 'K',
            rDiscount    => 'L',
            rUnitPrice   => 'M',
            rRevenue     => 'O',
        },
        11 => {
            dateBegin        => 'F',
            dateEnd          => 'F',
            rTitle           => 'D',
            rIsbn13          => 'C',
            rInstitution     => 'A',
            serviceProductID => 'B',
            rUnits           => 'M',
            rListPrice       => 'K',
            rPurchasePrice   => 'N',
            rRevenue         => 'R',
            rFee             => 'Q',
            rDeliveryMethod  => 'I',
        },
    );

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

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub priceType                  { 'rrp' }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub _dateNormalization         { 'month' }
sub _dateFormat                { [ 'excel', 'text' ] }

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

    # If the date begin field is populated with a date use it, otherwise,
    # look at the filename (FB 9239)
    #
    return $dt if ( $dt =~ /\.(\w{3})-(\d{2})\./ );    # .MMM-YY.

    # If the date begin field is empty, look at the filename (see Case 8817)
    #
    return $self->_getDateFromFilename();
}

sub _getDateEnd {
    my $self = shift;
    my $dt   = $self->_getByFieldName('dateEnd');

    # If the date begin field is populated with a date use it, otherwise,
    # look at the filename (FB 9239)
    #
    return $dt if ( $dt =~ /\.(\w{3})-(\d{2})\./ );    # .MMM-YY.

    # If the date field is empty, look at the filename (see Case 8817)
    #
    return $self->_getDateFromFilename();
}

sub _getDateFromFilename {
    my $self     = shift;
    my $filename = $self->filename();
    my $dateStr;

    my %fullMonths = (
        january   => 'jan',
        february  => 'feb',
        march     => 'mar',
        april     => 'apr',
        may       => 'may',
        june      => 'jun',
        july      => 'jul',
        august    => 'aug',
        september => 'sep',
        october   => 'oct',
        november  => 'nov',
        december  => 'dec',
    );

    my $date = $self->filename;
    if ( $date =~ /\.(\w{3})-(\d{2})\./ )    # .MMM-YY.
    {
        # Note: this importer has 'text' as one of the allowable date formats (see _dateFormat()),
        # above), so return the date in MMM-YY format.
        $dateStr = "$1-$2";
    } elsif ( $date =~ /^(\d{4}) (\w{3,9}) / )    # YYYY MonthName FB9239
    {
        my $month = $fullMonths{ lc($2) };
        $dateStr = "$month-$1";
    } else {
        $self->fail("Unrecognized date in filename: '$date'");
    }
    return $dateStr;
}

sub countryCode {
    my $self = shift;
    if ( defined $self->_getByFieldName('countryCode') ) {
        return $self->_getByFieldName('countryCode');
    } else {
        return 'US';
    }
}

sub currencyCode {
    my $self = shift;
    if ( defined $self->_getByFieldName('currencyCode') ) {
        return $self->_getByFieldName('currencyCode');
    } else {
        return 'USD';
    }
}

sub rListPriceCurrency {
    my $self = shift;
    return $self->currencyCode;
}

sub rPurchasePriceCurrency {
    my $self = shift;
    return $self->currencyCode;
}

sub purchaseType {
    my $self = shift;

    if ( defined $self->_getByFieldName('purchaseType') ) {
        my $purchaseType = $self->_getByFieldName('purchaseType');

        if ( $purchaseType =~ /^(VST Buy Sell|VST Bulk CM|VST Bulk)$/i ) {
            return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
        } else {
            $self->fail("Could not determine purchase type from $purchaseType");
        }
    }

    return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
}

###
1;    #
###
