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

use strict;

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

sub _fieldMap {
    {
        dateBegin            => 'C',
        dateEnd              => 'C',
        purchaseType         => 'I',
        rAuthor              => 'J',
        rTitle               => 'E',
        rIsbn13              => 'G',
        countryCode          => 'L',
        serviceProductID     => 'F',
        rUnits               => 'K',
        rListPrice           => 'O',
        rListPriceCurrency   => 'O1',
        rDiscount            => 'P',
        rRevenue             => 'Q',
        currencyCode         => 'R',
        rDuration            => 'I',
        rTransactionCategory => 'D',
    };
}

sub _headerIdentifier { ( rAuthor => 'AUTHOR' ) }

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

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin');
    # convert date from format (days since 1900-01-01) to 'yyyy-mm-dd'
    if ( $date && $date =~ /^\d{5}$/ ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
    }

    return $date;
}

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd');
    # convert date from format (days since 1900-01-01) to 'yyyy-mm-dd'
    if ( $date && $date =~ /^\d{5}$/ ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
    }

    return $date;
}

sub purchaseType {
    my $self = shift;
    my $type = $self->_getByFieldName('purchaseType');

    if ( !$type ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    } else {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    }
}

sub countryCode {
    my $self        = shift;
    my $countryCode = $self->_getByFieldName('countryCode');

    if ( $countryCode =~ m/^AUD$/i ) {
        return "AU";
    } elsif ( $countryCode =~ m/^GBP$/i ) {
        return "GB";
    } else {
        $self->fail("Unable to determine country code for '$countryCode'");
    }
}

sub rListPriceCurrency {
    my $self              = shift;
    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency');

    if ( $listPriceCurrency =~ m/^list price ([A-Z]{3})$/i ) {
        return $1;
    } else {
        $self->fail("Unable to find list price currency in '$listPriceCurrency'");
    }
}

1;

