package com.l1fan.ane.google;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.l1fan.ane.SDKContext;

public class InAppActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView tv = new TextView(this);
		tv.setLayoutParams(new LayoutParams(-1, -1));
		tv.setGravity(Gravity.CENTER);
		tv.setText("正在跳转...");
		setContentView(tv);

		try {
			JSONObject data = new JSONObject(getIntent().getExtras().getString("payData"));
			pay(data);
		} catch (JSONException e1) {
			e1.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SendIntentException e) {
			e.printStackTrace();
		}

	}

	private void pay(final JSONObject pay) throws RemoteException, SendIntentException {
		System.out.println("starting... pay");
		test(pay);
		System.out.println("end... pay");
	}

	private void test(final JSONObject pay) throws RemoteException, SendIntentException {
		String pid = pay.optString(SDKContext.PID);
		String ext = pay.optString(SDKContext.EXT);

		Bundle buyIntentBundle = InAppHelper.getHelper().billService.getBuyIntent(3, getPackageName(), pid, "inapp",
				ext);
		System.out.println("buy intent bundle:" + buyIntentBundle + ",resp code:"
				+ buyIntentBundle.getInt("RESPONSE_CODE"));
		PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
		System.out.println("pending intent" + pendingIntent);
		startIntentSenderForResult(pendingIntent.getIntentSender(), 1001, new Intent(), Integer.valueOf(0),
				Integer.valueOf(0), Integer.valueOf(0));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		System.out.println("activity result :" + resultCode);
		Bundle b = data.getExtras();
		for (String key : b.keySet()) {
			System.out.println("bundle data:" + key + ":" + b.get(key));
		}

		if (requestCode != 1001)
			return;
		SDKContext sdk = InAppHelper.getHelper().context;

		int responseCode = data.getIntExtra("RESPONSE_CODE", 0);
		String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
		String dataSignature = data.getStringExtra("INAPP_DATA_SIGNATURE");

		if (resultCode == Activity.RESULT_OK && responseCode == 0) {
			System.out.println("Successful resultcode from purchase activity.");
			System.out.println("Purchase data: " + purchaseData);
			System.out.println("Data signature: " + dataSignature);
			System.out.println("Extras: " + data.getExtras());

			if (purchaseData == null || dataSignature == null) {
				sdk.dispatchError(SDKContext.EVENT_PAY, "支付数据或签名为空");
				return;
			}

			JSONObject dispatchData = new JSONObject();

			try {
				final JSONObject p = new JSONObject(purchaseData);
				dispatchData.put("purchase", p);
				dispatchData.put("sign", dataSignature);
				new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							InAppHelper.getHelper().billService.consumePurchase(3, getPackageName(),
									p.getString("purchaseToken"));
						} catch (RemoteException e) {
							e.printStackTrace();
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
				}).start();
			} catch (JSONException e1) {
				e1.printStackTrace();
			}

			sdk.dispatchData(SDKContext.EVENT_PAY, dispatchData);
		}else if(resultCode == Activity.RESULT_OK){
			sdk.dispatchError(SDKContext.EVENT_PAY, "支付发生错误，代码:"+responseCode);
		}else if (resultCode == Activity.RESULT_CANCELED) {
			sdk.dispatchError(SDKContext.EVENT_PAY, SDKContext.CODE_ERR_CANCEL,"用户取消");
		}else {
			sdk.dispatchError(SDKContext.EVENT_PAY, "其他错误，代码:"+responseCode);
		}

		finish();
	}

}

@@ -1644,16 +1644,74 @@
 E%22));%0A%09%09
+%0A%09%09if (buyIntentBundle.getInt(%22RESPONSE_CODE%22) == 0) %7B%0A%09%09%09
 PendingI
@@ -1775,24 +1775,25 @@
 INTENT%22);%0A%09%09
+%09
 System.out.p
@@ -1834,16 +1834,17 @@
 ntent);%0A
+%09
 %09%09startI
@@ -1941,16 +1941,17 @@
 0),%0A%09%09%09%09
+%09
 Integer.
@@ -1983,16 +1983,118 @@
 Of(0));%0A
+%09%09%7Delse%7B%0A%09%09%09InAppHelper.getHelper().context.dispatchError(SDKContext.EVENT_PAY,%22%E6%94%AF%E4%BB%98%E8%AF%B7%E6%B1%82%E4%B8%8D%E6%94%AF%E6%8C%81%EF%BC%8C%E8%AF%B7%E6%A3%80%E6%9F%A5%E8%B4%A6%E5%8F%B7%22);%0A%09%09%7D%0A%0A
 %09%7D%0A%0A%09@Ov
@@ -2576,24 +2576,30 @@
 ODE%22, 0);%0A%09%09
+final 
 String purch
@@ -3222,63 +3222,8 @@
 y %7B%0A
-%09%09%09%09final JSONObject p = new JSONObject(purchaseData);%0A
 %09%09%09%09
@@ -3252,16 +3252,27 @@
 hase%22, p
+urchaseData
 );%0A%09%09%09%09d
@@ -3380,24 +3380,24 @@
 oid run() %7B%0A
-
 %09%09%09%09%09%09try %7B%0A
@@ -3392,24 +3392,82 @@
 %09%09%09%09%09%09try %7B%0A
+%09%09%09%09%09%09%09final JSONObject p = new JSONObject(purchaseData);%0A
 %09%09%09%09%09%09%09InApp

fix json序列化