package BookPub::POS::Import::Importer::Apple::Version1;

use strict;

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

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

sub _fieldMap {
    {
        serviceProductID   => 'M',
        currencyCode       => 'E',
        countryCode        => 'H',
        dateBegin          => 'V',
        rClientProductID   => 'N',
        isbn               => 'L',
        rTitle             => 'A',
        rAuthor            => 'B',
        rImprint           => 'Q',
        rUnits             => 'C',
        rListPrice         => 'F',
        rListPriceCurrency => 'G',
        rRevenue           => 'D',
        rFormat            => 'I',
        rProductType       => 'I',
    };
}

sub priceType    { 'agency' }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

sub _isValidSaleRecord {
    my $self = shift;

    my $format = $self->SUPER::rFormat() || return;
    my $revenue = $self->revenue();

    if ( $format && $format =~ /^7[TF]?$/i && !$revenue ) {
        Log->warn("Ignoring sale with 'Update' format type");
        return;
    }

    return $self->rTitle && $self->dateBegin;
}

sub productType {
    my $self = shift;
    my $type = $self->rProductType() || return;

    my $typeCode = $self->_parseProductType($type);
    return $typeCode;
}

sub _parseProductType {
    my $self = shift;
    my $type = shift;

    if ( $type && $type =~ /^eb1$/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ( $self->rFormat ) {

        # the product type field is reporting a format type, not a product type
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } else {
        return $self->SUPER::_parseProductType($type);
    }

    return;
}

sub rFormat {
    my $self    = shift;
    my $format  = $self->SUPER::rFormat() || return;
    my $revenue = $self->revenue();

    # One apple format actually reports this information in the product type field.
    # See case #13326

    # Known format types:
    #   1 - Free or Paid Apps (iPhone/iPod)
    #   7 - Updates (iPhone/iPod)
    #   IA1 - In-App Purchase
    #   IA9 - In-App Subscription
    #   1F - Free or Paid Apps (All)
    #   7F - Updates (All)
    #   1T - Free or Paid Apps (iPad)
    #   7T - Updates (iPad)

    if ( $format =~ /^1[TF]?$/i ) {
        return $format;
    } elsif ( $format =~ /^7[TF]?$/i ) {
        $self->fail("application upgrade sales item containing unexpected revenue")
          if ($revenue);
    } elsif ( $format =~ /^IA[19]$/i ) {
        $self->fail("in-app purchase or subscription seen");
    } else {

        # This isn't a format type we recognize.  Let product type actually handle any error cases
        return;
    }

}

sub formatType {
    my $self = shift;
    my $type = $self->SUPER::rFormat || return;

    if ( $type =~ /^1[TF]?$/i ) {
        return BookPub::DB::Item::Sale::kFormatTypeAppleMultimedia;
    } else {
        return;
    }
}

sub listPrice {
    return abs( shift->SUPER::listPrice() );
}

sub isPreOrder {
    my $preorder = shift->SUPER::isPreOrder() || return;

    return lc($preorder) eq 'p' ? 'Y' : undef;
}

1;
