package BookPub::Import::Importer::Scribd::Version8;

use strict;

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

sub _fieldMap {
    {
        purchaseType       => 'Q',
        priceType          => 'G',
        currencyCode       => 'J',
        countryCode        => 'V',
        dateBegin          => 'R',
        dateEnd            => 'S',
        rIsbn13            => 'K',
        rTitle             => 'L',
        rAuthor            => 'M',
        rImprint           => 'N',
        rUnits             => 'O',
        rListPrice         => 'C',
        rListPriceCurrency => 'D',
        rDiscount          => 'H',
        rRevenue           => 'I',
    };
}

sub _unverifiedFieldMap {
    {
        altListPrice         => 'F',
        altListPriceCurrency => 'J',
    };
}

sub _autoParseTitleAndSubtitle { 1 }
sub rServiceName { 'Scribd' }
sub rProductType { BookPub::DB::Item::Sale::kProductTypeEBook }

sub dateBegin {
    my $self = shift;

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

    if ( $date =~ /^\d{5}$/) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( 'yyyy-mm-dd', $date );
        return $date;
    }

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

sub dateEnd {
    my $self = shift;

    my $date = $self->_getByFieldName('dateEnd') || '';

    if ( $date =~ /^\d{5}$/) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( 'yyyy-mm-dd', $date );
        return $date;
    }

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

sub priceType {
    my $self = shift;

    my $type = $self->_getByFieldName('priceType') || '';
    return $type =~ /^list$/i ? 'rrp' : 'agency';
}

sub rListPriceCurrency {
    my $self = shift;
    my $code;
    if ( $self->listPriceRequired ) {
        $code = $self->_getByFieldName('altListPriceCurrency');
    } else {
        $code = $self->_getByFieldName('rListPriceCurrency');
    }

    return uc($code);
}

sub rListPrice {
    my $self = shift;
    my $price;
    if ( $self->listPriceRequired ) {
        $price = $self->_getByFieldName('altListPrice');
    } else {
        $price = $self->_getByFieldName('rListPrice');
    }

    return $price;
}

sub currencyCode {
    my $self = shift;
    my $code;

    $code = $self->_getByFieldName('currencyCode');

    return uc($code);
}

sub rUnits {
    my $self  = shift;
    my $units = $self->_getByFieldName('rUnits') || 0;

    return int $units;
}

sub rDiscount {
    my $self     = shift;
    my $discount = $self->_getByFieldName('rDiscount');
    $discount = 1 - ( $discount / 100 );

    return $discount;
}

1;
