package BookPub::Import::Importer::Yoto::v1;

use strict;
use warnings;

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

use Date::Calc qw(Days_in_Month);

sub _fieldMap {
    {
        dateBegin                => 'A7',
        rAuthor                  => 'C',
        rTitle                   => 'A',
        rIsbn13                  => 'B',
        rUnits                   => 'D',
        rListPrice               => 'E',
        rListPriceCurrency       => 'E',
        rNativeListPrice         => 'E',
        rNativeListPriceCurrency => 'E',
        rRevenue                 => 'I',
        currencyCode             => 'I10',
    };
}

sub _headerIdentifier { rTitle => 'Title' }

sub rServiceName  { 'Yoto' }
sub _useIsSaleRec { 1 }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType   { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub rPurchaseType { $_[0]->purchaseType() }
sub rProductType  { $_[0]->productType() }
sub rPriceType    { 'rrp' }
sub countryCode   { 'US' }

my %months = (
    january   => 1,
    february  => 2,
    march     => 3,
    april     => 4,
    may       => 5,
    june      => 6,
    july      => 7,
    august    => 8,
    september => 9,
    october   => 10,
    november  => 11,
    december  => 12,
);

sub _processHeader {
    my $self = shift;

    unless ($self->{_currency}) {
        my $currency = $self->_getByFieldName("currencyCode");
        $self->{_currency} = "USD" if substr($currency, -1)  =~ /\$/;
    }

    unless ($self->{_dateBegin}) {
        my $date = $self->_getByFieldName("dateBegin") || "";
        $date =~ /Royalty Statement (\w+) (\d{4})$/i;
        $self->{_dateBegin} = sprintf("%04d-%02d-%02d", $2, $months{lc($1)}, 1);
    }

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

sub dateBegin    { $_[0]->{_dateBegin} ? $_[0]->{_dateBegin} : $_[0]->fail("Cannot determine dates") }
sub currencyCode { $_[0]->{_currency} if $_[0]->{_currency} }
sub rListPrice   { shift->rUnitPrice }
sub rListPriceCurrency { shift->currencyCode }
sub rNativeListPriceCurrency { $_[0]->detectCurrency() }
sub rNativeListPrice   { $_[0]->_clean($_[0]->_getByFieldName("rNativeListPrice")) }
sub rRevenue { $_[0]->_clean($_[0]->_getByFieldName("rRevenue")) }

sub _clean {
    my ($self, $arg) = @_;

    $arg =~ s/^[^0-9]*//g;
    $arg =~ s/[,.](?=\d+[,.])//g;
    $arg =~ s/,/./g;

    return $arg;
}

sub rUnitPrice {
    my $self = shift;

    my $revenue = $self->rRevenue|| 0;
    my $units = $self->_getByFieldName('rUnits') || 0;

    return $revenue / $units;
}

sub detectCurrency {
    my $self = shift;

    my $cur = $self->_getByFieldName("rNativeListPriceCurrency") || "";

    return "EUR" if ($cur =~ /\x{20ac}/);
    return "USD" if ($cur =~ /\$/);
    return "GBP" if ($cur =~ /\x{a3}/);

    $self->fail("Cannot detect NativeListPrice Currency from '$cur'");

}

sub _isSaleRec {
    my $self = shift;

    return unless ( $self->rRevenue =~ /[^0-9]+/ && $self->rTitle);

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

1;
