package BookPub::Import::Importer::INscribeDigital::Version4;

use strict;
use Data::Dumper;

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

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

# We have to do some odd things with the field map here because this importer has
# different column definitions for separate tabs.  The map is defined by tab name below.
# The summary tab is ignored here (returns and empty field set).
sub _fieldMap {
    my $self = shift;

    my $map = {
        'details' => {
            countryCode => 'I',
            dateBegin   => 'B',
            dateEnd     => 'D',
            isbn        => 'H',
            rTitle      => 'G',
            rAuthor     => 'F',
            rUnits      => 'J',
            rListPrice  => 'K',
            rRevenue    => 'N',
        },
    };

    # ignore all sheets except 'details'
    my $sheet = lc( $self->sheetname ) || return {};
    $sheet =~ /details/ ? return $map->{$sheet} : return {};
}

sub _headerIdentifier { ( rTitle => 'TITLE' ) }

sub priceType     { 'rrp' }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub currencyCode  { 'USD' }
sub _useIsSaleRec { 1 }

# If this grows much larger, we should encapsulate this data in the database SB
# !!! Note that we have a list like this in Common::CurrencyFormat already. !!!
# Some of these are mapped to different values though, so we'll check this list first
# and use the one in Common as a backup.
my $currencyMap = {
    AR => 'ARS',    # Argentine Peso
    AT => 'ATS',    # Austrian Schilling
    AU => 'AUD',    # Australian Dollar
    BE => 'BEF',    # Belgian Franc
    BG => 'BGN',    # Bulgarian Lev
    BR => 'BRL',    # Brazilian Real
    CA => 'CAD',    # Canadian Dollar
    CH => 'CHF',    # Swiss Franc
    CL => 'CLP',    # Chilean Peso
    CO => 'COP',    # Columbian Peso
    CR => 'CRC',    # Costa Rican Colon
    CY => 'EUR',    # Cyprus, Euro
    CZ => 'CZK',    # Czech Republic Koruna
    DE => 'EUR',    # Germany, Euro
    DO => 'DOP',    # Dominican Republic Peso
    DK => 'DKK',    # Danish Krone
    EE => 'EUR',    # Estonia, Euro
    EC => 'USD',    # Ecuador, US Dollar
    ES => 'EUR',    # Spain, Euro
    FI => 'EUR',    # Finland, Euro
    FR => 'EUR',    # France, Euro
    GB => 'GBP',    # Great Britain, Pound Sterling
    GR => 'EUR',    # Greece, Euro
    GT => 'GTQ',    # Guatemalan Quetzal
    HN => 'HNL',    # Honduran Lempira
    HU => 'HUF',    # Hungarian Forint
    IE => 'EUR',    # Ireland, Euro
    IT => 'EUR',    # Italy, Euro
    JP => 'JPY',    # Japan, Yen
    LU => 'EUR',    # Luxembourg, Euro
    LT => 'LTL',    # Lithuanian litas
    LV => 'LVL',    # Latvian Lats
    MT => 'EUR',    # Malta, Euro
    MX => 'MXN',    # Mexican Peso
    NI => 'NIO',    # Nicaraguan Cordoba
    NL => 'EUR',    # Netherlands, Euro
    NZ => 'NZD',    # New Zealand Dollar
    PA => 'PAB',    # Panamanian Balboa
    PE => 'PEN',    # Peruvian nuevo sol
    PL => 'PLN',    # Polish Zloty
    PT => 'EUR',    # Portugal, Euro
    PY => 'PYG',    # Paraguay Guarani
    RO => 'RON',    # Romanian Leu
    SE => 'SEK',    # Swiss Krona
    SI => 'EUR',    # Slovenia, Euro
    SK => 'EUR',    # Slovakia, Euro
    SV => 'USD',    # El Salvador, US Dollar
    US => 'USD',    # US Dollar
    VE => 'VEF',    # Venezuelan Fuerte
};

sub rListPriceCurrency {
    my $self    = shift;
    my $country = uc( $self->_getByFieldName('countryCode') );
    my $code;

    if ($country) {
        my $countryObj = Common::Country->Get($country)
          || $self->fail("Unknown country code '$country'");
        $code = $countryObj->alpha2();
    }

    if ( $currencyMap->{$code} ) {
        return $currencyMap->{$code};
    } else {
        my $currencyFormat = Common::CurrencyFormat->new( countryCode => $code );
        if ($currencyFormat) {
            return $currencyFormat->currencyCode();
        }
        $self->fail("Country code not found in currencyMap: $code");
    }
}

sub _isSaleRec {
    my $self    = shift;
    my $country = uc( $self->_getByFieldName('countryCode') );

    # We're going to end processing when we've reached the TOTAL line...
    if ( $country eq "TOTAL" ) {
        return 0;
    }

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

1;
