package BookPub::Import::Importer::Apple::Version4;

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';
use Date::Calc qw(Days_in_Month);

use parent 'BookPub::Import::Importer::Apple::Version2';

sub _fieldMap {
    {
        purchaseType           => 'O',
        rProductType           => 'O',
        rAuthor                => 'K',
        rTitle                 => 'L',
        rIsbn13                => 'C',
        rImprint               => 'M',
        countryCode            => 'Q',
        rClientProductID       => 'D',
        serviceProductID       => 'J',
        rUnits                 => 'E',
        rUnitPrice             => 'F',
        rListPrice             => 'F',
        rListPriceCurrency     => 'H',
        rPurchasePrice         => 'R',
        rPurchasePriceCurrency => 'S',
        rRevenue               => 'G',
        currencyCode           => 'H',
        rFormat                => 'O',
    };
}

sub _unverifiedFieldMap {
    {
        _salesOrReturn => 'I',
    };
}

sub _headerIdentifier { rTitle => 'Title' }

sub _useIsSaleRec { 1 }
sub rServiceName  { 'Apple' }
sub productType   { shift->rProductType }

sub isPreOrder { }
sub rPromoCode { }

sub dateBegin {
    my $self = shift;

    unless ( exists $self->{__dateBegin} ) {
        my ($month, $year) = $self->filename =~ /[\s_](\d{2})(\d{2})[\s_]BR/i;
        $self->fail("Can't determine dateBegin from file name.") unless $month && $year;
        $self->{__dateBegin} = sprintf "%04d-%02d-%02d", "20$year", $month, 1;
    }

    return $self->{__dateBegin};
}

sub dateEnd {
    my $self = shift;

    unless ( exists $self->{__dateEnd} ) {
        my ($month, $year) = $self->filename =~ /[\s_](\d{2})(\d{2})[\s_]BR/i;
        $self->fail("Can't determine dateEnd from file name.") unless $month && $year;
        $self->{__dateEnd} = sprintf "%04d-%02d-%02d", "20$year", $month, Days_in_Month( "20$1", $month );
    }

    return $self->{__dateEnd};
}

sub purchaseType {
    my $self = shift;

    my $type = $self->_getByFieldName('purchaseType') || '';
    if ( $type =~ /^(?:FI1|IA1|IA1-M)$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeInAppPurchase;
    } elsif ( $type =~ /^(?:IA9|IA9-M|IAC|IAC-M|IAY|IAY-M)$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL;
    }

    return BookPub::DB::Item::Sale::kPurchaseTypeDownload;
}

sub rProductType {
    my $self = shift;

    my $type = $self->_getByFieldName('rProductType') || '';
    if ( $type =~ /^(?:EB1|F1|1F|IA9)$/i || $self->SUPER::rFormat() ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }

    $self->fail("Can't determine rProductType from: '$type'.");
}

sub priceType {
    my $self = shift;

    if ( Common::RSApp::GetClientID() == 324 && $self->filename =~ /^80127007/ ) {
        return 'rrp';
    }

    return 'agency';
}

sub rTitle {
    my $self = shift;

    my $rTitle = $self->_getByFieldName('rTitle') || '';
    $rTitle =~ /^.*?(?:\d{13})?\s?(.+)$/;
    return $1 // '';
}

sub rIsbn13 {
    my $self = shift;

    my $rIsbn = $self->_getByFieldName('rIsbn13') || '';
    return $rIsbn if $rIsbn;

    my $rTitle = $self->_getByFieldName('rTitle') || '';
    $rTitle =~ /^.*?(\d{13})?.*$/;
    return $1 // '';
}

sub rUnits {
    my $self = shift;

    my $rUnits        = $self->_getByFieldName('rUnits')         || '';
    my $salesOrReturn = $self->_getByFieldName('_salesOrReturn') || '';

    return $salesOrReturn =~ /^s/i
        ? abs($rUnits)
        : -1 * abs($rUnits);
}

sub rListPrice {
    my $self = shift;

    my $rListPrice = $self->_getByFieldName('rListPrice') || 0;
    return $rListPrice * 1.4285714;
}


1;