package BookPub::Import::Importer::Storytel::Version3;

use strict;
use warnings;

use Date::Calc qw(Days_in_Month);

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


sub _fieldMap {
    my $self = shift;

    my $sheet = lc( $self->sheetname || '' );
    return {} unless $sheet && $sheet =~ /A\-books/i;

    my %fieldMap = (
        3 => {
            productType        => 'B',
            rAuthor            => 'A',
            rTitle             => 'B',
            rIsbn13            => 'C',
            rImprint           => 'N',
            countryCode        => 'D',
            rUnits             => 'F',
            rUnitPrice         => 'K',
            rListPrice         => 'K',
            rListPriceCurrency => 'K1',
            rDiscount          => 'E',
            rRevenue           => 'L',
            currencyCode       => 'L1',
        },
    );

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

sub productType  { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub rProductType { BookPub::DB::Item::Sale::kProductTypeAudioBook }

sub countryCode {
    my $self = shift;

    my $code = $self->_getByFieldName('countryCode');

    my $oCountry = Common::Country->Get($code) if $code;
    return $oCountry->alpha2() if $oCountry;

    $self->fail("Unknown country code '$code'");
}

sub rDiscount {
    my $self = shift;

    my $discount = $self->_getByFieldName('rDiscount');
    $discount =~ /(\d{1,2})/;
    return $1 / 100;
}

sub rUnits {
    my $self = shift;

    my $units = $self->_getByFieldName('rUnits');
    return sprintf "%.0f", $units;
}

sub rListPriceCurrency {
    my $self = shift;

    my ( $currencyCode ) = $self->_getByFieldName('rListPriceCurrency') =~ /\((\w+)\)/;
    return $currencyCode;
}

sub currencyCode {
    my $self  = shift;

    my ( $currencyCode ) = $self->_getByFieldName('currencyCode') =~ /\((\w+)\)/;
    return $currencyCode;
}

1;
