package com.billing.google;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.android.vending.billing.IabHelper;
import com.billing.BillingService;
import com.eggheadgames.inapppayments.BuildConfig;

import java.util.List;

public class GoogleBillingService extends BillingService {

    private IabHelper iap;
    public List<String> iapkeys;
    private GoogleBillingListener googleBillingListener;
    private String lastRequestedSku;
    private boolean productPurchaseRequested;

    public GoogleBillingService(Context context, List<String> iapkeys) {
        super();
        this.context = context;
        this.iapkeys = iapkeys;
    }

    public void purchaseCallback(int requestCode, int resultCode, Intent data) {
        try {
            iap.handleActivityResult(requestCode, resultCode, data);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @SuppressWarnings("ConstantConditions")
    @Override
    public void init(String key) {
        //We rebuild the public key at run-time.
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < key.length(); i++) {
            char c = key.charAt(i);
            if ((c >= 'A' && c <= 'M') || (c >= 'a' && c <= 'm'))
                c += 13;
            else if ((c >= 'N' && c <= 'Z') || (c >= 'n' && c <= 'z'))
                c -= 13;
            stringBuilder.append(c);
        }
        try {
            iap = new IabHelper(context, stringBuilder.toString());
            if (iap != null) {
                googleBillingListener = new GoogleBillingListener(iap, this);
                iap.startSetup(googleBillingListener);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void buy(Activity activity, String sku, int id) {
        try {
            if (iap != null) {
                lastRequestedSku = sku;
                productPurchaseRequested = true;
                iap.launchPurchaseFlow(activity, sku, id, googleBillingListener);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void subscribe(Activity activity, String sku, int id) {
        try {
            if (iap != null) {
                lastRequestedSku = sku;
                productPurchaseRequested = false;
                iap.launchSubscriptionPurchaseFlow(activity, sku, id, googleBillingListener);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void unsubscribe(Activity activity, String sku, int id) {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("https://play.google.com/store/account"));
            activity.startActivity(intent);
            activity.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void enableDebugLogging(boolean enable) {
        if (iap != null) {
            iap.enableDebugLogging(enable);
        }
    }

    @Override
    public void close() {
        iap.dispose();
        super.close();
    }

    public String getLastRequestedSku() {
        return lastRequestedSku;
    }

    public boolean isProductPurchaseRequested() {
        return productPurchaseRequested;
    }
}

@@ -223,59 +223,8 @@
 ice;
-%0Aimport com.eggheadgames.inapppayments.BuildConfig;
 %0A%0Aim
@@ -2765,16 +2765,17 @@
 _VIEW);%0A
+%0A
         
@@ -2782,39 +2782,38 @@
     
-intent.setData(Uri.parse(
+String subscriptionUrl = 
 %22http
-s
 ://p
@@ -2840,17 +2840,184 @@
 /account
-%22
+/subscriptions%22%0A                    + %22?package=%22 + activity.getPackageName()%0A                    + %22&sku=%22 + sku;%0A%0A            intent.setData(Uri.parse(subscriptionUrl
 ));%0A    

Update Google subscription link (Close #15)