package BookPub::Import::Importer::DieselEbooks::Version1;

use strict;
use Date::Calc qw(Add_Delta_Days);

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

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

sub _fieldMap {
    {
        currencyCode       => 'G',
        countryCode        => 'I',
        rState             => 'J',
        rPostalCode        => 'N',
        dateBegin          => 'AA',
        rIsbn13            => 'AF',
        rTitle             => 'AI',
        rAuthor            => 'AJ',
        rImprint           => 'AN',
        rUnits             => 'AO',
        rListPrice         => 'AP',
        rListPriceCurrency => 'AT',
        rPurchasePrice     => 'AP',
        rDiscount          => 'AQ',
    };
}

sub _headerIdentifier { ( rTitle => 'Product title' ) }
sub purchaseType      { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType       { BookPub::DB::Item::Sale::kProductTypeEBook }
sub priceType         { 'agency' }

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin');
    if ( $date =~ /^(\d{1,2})\/(\d{1,2})\/(\d{4})/ ) {
        $date = $3 . "-" . $1 . "-" . $2;
    }

    return $date;
}

sub rRevenue {
    my $self      = shift;
    my $listPrice = $self->rListPrice || return;
    my $units     = $self->rUnits || return;
    my $discount  = $self->_getByFieldName('rDiscount') || return;

    my $revenue = ( $listPrice * $units ) - $discount;

    return $revenue;
}

sub rDiscount {
    my $self      = shift;
    my $listPrice = $self->rListPrice || return;
    my $units     = $self->rUnits || return;
    my $discount  = $self->_getByFieldName('rDiscount') || return;

    my $discountRate = ( $discount / $units ) / $listPrice;

    return $discountRate;
}

sub rTitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $title;
}

sub rSubtitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    my $subtitle;
    ( $title, $subtitle ) = BookPub::Import::Importer::ParseTitleAndSubtitle($title);

    return $subtitle;
}

###
1;    # Play nicely.
###
