package BookPub::Import::Importer::Copia;

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 = (
        6 => {
            rServiceName           => 'BO',
            dateBegin              => 'E',
            dateEnd                => 'F',
            rPurchaseType          => 'BW',
            rPriceType             => 'AN',
            currencyCode           => 'I',
            countryCode            => 'K',
            rState                 => 'BT',
            rPostalCode            => 'BV',
            rIsbn13                => 'U',
            rTitle                 => 'X',
            rAuthor                => 'Y',
            rImprint               => 'AC',
            rInstitution           => 'BX',
            rUnits                 => 'AH',
            rSales                 => 'AF',
            rReturns               => 'AG',
            rListPrice             => 'BQ',
            rListPriceCurrency     => 'I',
            rPurchasePrice         => 'AM',
            rPurchasePriceCurrency => 'AO',
            rDiscount              => 'AP',
            rRevenue               => 'BC',
            rDuration              => 'BW',
            rChannel               => 'M',
            rModel                 => 'BW',
        },
    );

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

sub _headerIdentifier { ( rIsbn13 => 'Main product ID#' ) }
sub _useIsSaleRec { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }

sub purchaseType {
    my $self = shift;
    my $type = lc( $self->rPurchaseType );

    if ( $type =~ /rent/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } elsif ( $type =~ /perpetual/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
    }

    return;
}

sub priceType {
    my $self = shift;
    my $type = lc( $self->rPriceType );

    if ( $type == 1 ) {
        $type = 'rrp';
    } elsif ( $type == 2 ) {
        $type = 'rrpplustax';
    } elsif ( $type == 41 ) {
        $type = 'agency';
    } elsif ( $type == 42 ) {
        $type = 'agencyplustax';
    }

    return $type;
}

1;
