package BookPub::Import::Importer::Copia::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',
        rProductType       => 'AX',
        rIsbn13            => 'AF',
        rTitle             => 'AI',
        rAuthor            => 'AJ',
        rImprint           => 'AN',
        rUnits             => 'AO',
        rListPrice         => 'AQ',
        rListPriceCurrency => 'AV',
        rPurchasePrice     => 'AP',
        rDiscount          => 'AR',
        rRevenue           => 'AS',
    };
}

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

sub productType {
    my $self = shift;

    my $productType = $self->_getByFieldName('rProductType');

    if ( lc($productType) eq 'ebook' ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } else {
        $self->fail("Unknown product type: $productType");
        return undef;
    }
}

sub isFree {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue');

    if ( $revenue == 0 ) {
        return 'Y';
    }

    return;
}

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.
###
