/*
 * Copyright 2012-2015 One Platform Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onepf.opfiab.listener;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.onepf.opfiab.OPFIab;
import org.onepf.opfiab.api.IabHelper;
import org.onepf.opfiab.model.billing.Purchase;
import org.onepf.opfiab.model.billing.SkuType;
import org.onepf.opfiab.model.event.billing.InventoryResponse;
import org.onepf.opfiab.model.event.billing.PurchaseResponse;
import org.onepf.opfiab.verification.VerificationResult;

import java.util.Map;

public class DefaultBillingListener extends SimpleBillingListener {

    @Nullable
    private IabHelper iabHelper;

    @NonNull
    protected IabHelper getHelper() {
        if (iabHelper == null) {
            iabHelper = OPFIab.getAdvancedHelper();
        }
        return iabHelper;
    }

    @Override
    public void onPurchase(@NonNull final PurchaseResponse purchaseResponse) {
        super.onPurchase(purchaseResponse);
        if (purchaseResponse.isSuccessful()) {
            getHelper().inventory(true);
        }
    }

    @SuppressWarnings("PMD.AvoidDeeplyNestedIfStmts")
    @Override
    public void onInventory(@NonNull final InventoryResponse inventoryResponse) {
        super.onInventory(inventoryResponse);
        if (inventoryResponse.isSuccessful()) {
            // Inventory request was successful
            final Map<Purchase, VerificationResult> inventory = inventoryResponse.getInventory();
            if (inventory != null) {
                // Inventory is not empty
                for (final Map.Entry<Purchase, VerificationResult> entry : inventory.entrySet()) {
                    final Purchase purchase = entry.getKey();
                    if (purchase.getType() == SkuType.CONSUMABLE
                            && entry.getValue() == VerificationResult.SUCCESS) {
                        // Purchase is consumable and it was successfully verified
                        consume(purchase);
                    }
                }
            }
            if (inventoryResponse.hasMore()) {
                getHelper().inventory(false);
            }
        }
    }

    protected void consume(final Purchase purchase) {
        getHelper().consume(purchase);
    }
}

@@ -1406,225 +1406,777 @@
     
-@Override%0A    public void onPurchase(@NonNull final PurchaseResponse purchaseResponse) %7B%0A        super.onPurchase(purchaseResponse);%0A        if (purchaseResponse.isSuccessful()) %7B%0A            getHelper().inventory(tru
+protected boolean canConsume(@Nullable final Purchase purchase,%0A                                 @Nullable final VerificationResult verificationResult) %7B%0A        return purchase != null && verificationResult != null%0A                && purchase.getType() == SkuType.CONSUMABLE%0A                && verificationResult == VerificationResult.SUCCESS;%0A    %7D%0A%0A    @Override%0A    public void onPurchase(@NonNull final PurchaseResponse purchaseResponse) %7B%0A        super.onPurchase(purchaseResponse);%0A        final Purchase purchase = purchaseResponse.getPurchase();%0A        if (purchaseResponse.isSuccessful()%0A                && canConsume(purchase, purchaseResponse.getVerificationResult())) %7B%0A            //noinspection ConstantConditions%0A            getHelper().consume(purchas
 e);%0A
@@ -2850,237 +2850,86 @@
 if (
-purchase.getType() == SkuType.CONSUMABLE%0A                            && entry.getValue() == VerificationResult.SUCCESS) %7B%0A                        // Purchase is consumable and it was successfully verified%0A                        
+canConsume(purchase, entry.getValue())) %7B%0A                        getHelper().
 cons
@@ -2989,32 +2989,79 @@
 %7D%0A            %7D%0A
+            // Load next batch if there's more%0A
             if (
@@ -3170,107 +3170,7 @@
    %7D
-%0A%0A    protected void consume(final Purchase purchase) %7B%0A        getHelper().consume(purchase);%0A    %7D
 %0A%7D%0A

Change DefaultBillingListener behavior.