package BookPub::Import::Importer::CNPeReading::Version1;

use strict;

use lib '/app/tools/common/lib';
use Common::Country;

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            dateBegin          => 'B',
            rTitle             => 'D',
            rIsbn13            => 'C',
            rImprint           => 'K',
            rInstitution       => 'A',
            rUnits             => 'E',
            rListPrice         => 'H',
            rListPriceCurrency => 'G',
            rDiscount          => 'I',
            rRevenue           => 'J',
            currencyCode       => 'G',
            rModel             => 'F',
        },
    );

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

sub _headerIdentifier { ( rIsbn13 => 'eISBN' ) }
sub _useIsSaleRec     { 1 }
sub priceType         { 'rrp' }
sub countryCode       { 'CN' }
sub productType       { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType      { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType      { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceTypeQualifierID       { BookPub::DB::Item::PriceTypeQualifier::kCorporate }    # Library
sub _autoParseTitleAndSubtitle { 1 }

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName(
        service_id => shift->serviceID
    );
}

sub discount {
    my $self = shift;
    my $discount = $self->_getByFieldName('rDiscount') || return;
    $discount = 1 - $discount;
    return $discount;
}

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

    $date = substr( $date, 0, 4 ) . "-" . substr( $date, 4, 2 ) . "-" . substr( $date, 6, 2 );

    return $date;
}

sub _isSaleRec {
    my $self = shift;

    $self->SUPER::_isSaleRec || return 0;    # Bail out early on blank rows
    $self->rTitle ne 'Total' || return 0;    # Ignore subtotal line.

    return 1;
}

1;
