package BookPub::Import::Importer::Skoobe;

use strict;

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

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

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

    my %fieldMap = (
        2 => {
            currencyCode           => 'I',
            countryCode            => 'E',
            rIsbn13                => 'B',
            rTitle                 => 'C',
            rUnits                 => 'D',
            rListPrice             => 'H',
            rListPriceCurrency     => 'F',
            rPurchasePrice         => 'G',
            rPurchasePriceCurrency => 'F',
            rRevenue               => 'J',
        },
        3 => {
            currencyCode           => 'I',
            countryCode            => 'E',
            rIsbn13                => 'B',
            rTitle                 => 'C',
            rUnits                 => 'D',
            rListPrice             => 'H',
            rListPriceCurrency     => 'F',
            rPurchasePrice         => 'J',
            rPurchasePriceCurrency => 'I',
            rRevenue               => 'K',
        },
        4 => {
            currencyCode           => 'J',
            countryCode            => 'E',
            rIsbn13                => 'B',
            rTitle                 => 'C',
            rUnits                 => 'D',
            rListPrice             => 'H',
            rListPriceCurrency     => 'F',
            rPurchasePrice         => 'I',
            rPurchasePriceCurrency => 'I1',
            rRevenue               => 'K',
        },
    );

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

sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub priceType                  { 'rrp' }
sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub _getDateBegin {
    my $self = shift;

    if ( $self->filename =~ /^(\d{4})(\d{2})/ ) {
        return "$1-$2-01";
    } else {
        $self->fail( "Unable to determine date from filename: " . $self->filename );
    }
}

sub _getDateEnd {
    my $self = shift;
    my ( $dateStr, $date );

    $dateStr = $self->_getDateBegin();
    $date = new Common::Date( date => $dateStr, type => $self->_dateFormat() );

    return $date->monthEnd();
}

sub rPurchasePriceCurrency {
    my $self = shift;

    my $currency = $self->_getByFieldName('rPurchasePriceCurrency');

    if ( $currency =~ /^(\w{3})$/ ) {
        return $1;
    } elsif ( $currency =~ /\((\w{3})\)$/ ) {
        return $1;
    }

    $self->fail( "Unable to find purchase price currency in: " . $currency );
}

1;
