package com.duoku.cocosx.demo;

import org.json.JSONObject;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;
import cn.cmgame.billing.api.GameInterface;

import com.duoku.platform.single.DKPlatform;
import com.duoku.platform.single.DKPlatformSettings;
import com.duoku.platform.single.DkErrorCode;
import com.duoku.platform.single.DkProtocolKeys;
import com.duoku.platform.single.callback.IDKSDKCallBack;
import com.duoku.platform.single.item.DKCMGBData;
import com.duoku.platform.single.item.DKCMMMData;
import com.duoku.platform.single.item.DKCMMdoData;
import com.duoku.platform.single.item.GamePropsInfo;

public class JavaProxy {

	private static String TAG = JavaProxy.class.getSimpleName();
	
	/**
	 * 普通支付 
	 * mPropsId ,price需更改为 实际道具id和价格
	 */
	public static void pay(){
		final Activity activity = DemoApplication.getInstance().getMainActivity();
		
		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				String propsId = "1055";
				String price = "0.1";
				String title = "头盔";
				
				DKCMMdoData mdoData = null;
				mdoData = new DKCMMdoData();
				mdoData.setPropsId(propsId);
				mdoData.setPrice(String.valueOf(price));
				mdoData.setDest("10658077016622");
				mdoData.setChannelNum("622001");
				
				DKCMMMData mmData = new DKCMMMData("000000000000","0000000000000000");
				//mm皮肤选择，共三种。如果不设置走默认皮肤
				mmData.setSKIN(DKCMMMData.SKIN_SYSTEM_ONE);
				mmData.setPaycode("00000000000000");
				
				DKCMGBData gbData = new DKCMGBData("006");
				
				GamePropsInfo gamePropsInfo = new GamePropsInfo(propsId, price, title ,"");
				DKPlatform.getInstance().invokePayCenterActivity(activity,gamePropsInfo, mdoData, mmData, gbData, null, RechargeCallback);
				
			}
		});
	}
	
	/**
	 * 直接微信支付
	 * mPropsId ,price需更改为 实际道具id和价格
	 */
	public static void wxPay(){
		final Activity activity = DemoApplication.getInstance().getMainActivity();
		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				String propsId = "1055";
				String price = "0.1";
				String title = "头盔";
				
				GamePropsInfo gamePropsInfo = new GamePropsInfo(propsId, price , title ,"");
				DKPlatform.getInstance().invokePayCenterActivity(activity,gamePropsInfo,RechargeCallback,DKPlatformSettings.PAY_TENCENTMM);
				
			}
		});
	}
	
	/**
	 * 直接支付宝支付
	 * mPropsId ,price需更改为 实际道具id和价格
	 */
	public static void alipay(){
		final Activity activity = DemoApplication.getInstance().getMainActivity();
		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				String propsId = "1055";
				String price = "0.1";
				String title = "头盔";
				
				GamePropsInfo gamePropsInfo = new GamePropsInfo(propsId, price, title ,"");
				DKPlatform.getInstance().invokePayCenterActivity(activity,gamePropsInfo,RechargeCallback,DKPlatformSettings.PAY_ALIPAY);
				
			}
		});
	}
	
	/**
	 * 直接话费支付
	 * mPropsId ,price需更改为 实际道具id和价格
	 */
	public static void hfPay(){
		final Activity activity = DemoApplication.getInstance().getMainActivity();
		
		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				String propsId = "1055";
				String price = "0.1";
				String title = "头盔";
				
				DKCMMdoData mdoData = null;
				mdoData = new DKCMMdoData();
				mdoData.setPropsId(propsId);
				mdoData.setPrice(String.valueOf(price));
				mdoData.setDest("10658077016622");
				mdoData.setChannelNum("622001");
				
				DKCMMMData mmData = new DKCMMMData("000000000000","0000000000000000");
				//mm皮肤选择，共三种。如果不设置走默认皮肤
				mmData.setSKIN(DKCMMMData.SKIN_SYSTEM_ONE);
				mmData.setPaycode("00000000000000");
				
				mdoData.setCode("YX,253436,6,79a9,1822026");
				
				GamePropsInfo gamePropsInfo = new GamePropsInfo(propsId, price, title ,"");
				DKCMGBData gbData = new DKCMGBData("006");
				DKPlatform.getInstance().invokePayCenterActivity(activity,gamePropsInfo, mdoData, mmData, gbData,null,RechargeCallback,DKPlatformSettings.PAY_HUAFEI);
				
			}
		});
	}

	/**
	 * 支付处理过程的结果回调函数
	 * */
	static IDKSDKCallBack RechargeCallback = new IDKSDKCallBack(){
		
		Activity activity = DemoApplication.getInstance().getMainActivity();
		
		@Override
		public void onResponse(String paramString) {
			// TODO Auto-generated method stub
			Log.d("GamePropsActivity", paramString);
			try {
				JSONObject jsonObject = new JSONObject(paramString);
				// 支付状态码
				int mStatusCode = jsonObject.getInt(DkProtocolKeys.FUNCTION_STATUS_CODE);
				
				if(mStatusCode == DkErrorCode.BDG_RECHARGE_SUCCESS){
					// 返回支付成功的状态码，开发者可以在此处理相应的逻辑
					
					// 订单ID
					String mOrderId = null;
					// 订单状态
					String mOrderStatus = null;
					// 道具ID
					String mOrderProductId = null;
					// 道具实际支付的价格
					String mOrderPrice = null;
					// 支付通道
					String mOrderPayChannel = null;
					//道具原始价格，若微信、支付宝未配置打折该值为空，
					String mOrderPriceOriginal = null;
					
					if(jsonObject.has(DkProtocolKeys.BD_ORDER_ID)){						
						mOrderId = jsonObject.getString(DkProtocolKeys.BD_ORDER_ID);	
					}
					if(jsonObject.has(DkProtocolKeys.BD_ORDER_STATUS)){
						mOrderStatus = jsonObject.getString(DkProtocolKeys.BD_ORDER_STATUS);
					}
					if(jsonObject.has(DkProtocolKeys.BD_ORDER_PRODUCT_ID)){			
						mOrderProductId = jsonObject.getString(DkProtocolKeys.BD_ORDER_PRODUCT_ID);
					}
					if(jsonObject.has(DkProtocolKeys.BD_ORDER_PAY_CHANNEL)){						
						mOrderPayChannel = jsonObject.getString(DkProtocolKeys.BD_ORDER_PAY_CHANNEL);
					}
					if(jsonObject.has(DkProtocolKeys.BD_ORDER_PRICE)){						
						mOrderPrice = jsonObject.getString(DkProtocolKeys.BD_ORDER_PRICE);
					}
					if(jsonObject.has(DkProtocolKeys.BD_ORDER_PAY_ORIGINAL)){						
						mOrderPriceOriginal = jsonObject.getString(DkProtocolKeys.BD_ORDER_PAY_ORIGINAL);
					}
					
					Toast.makeText(activity, "道具购买成功!\n金额:"+mOrderPrice+"元", Toast.LENGTH_LONG).show();
					
				}else if(mStatusCode == DkErrorCode.BDG_RECHARGE_USRERDATA_ERROR){
					
					Toast.makeText(activity, "用户透传数据不合法", Toast.LENGTH_LONG).show();
					
				}else if(mStatusCode == DkErrorCode.BDG_RECHARGE_ACTIVITY_CLOSED){
					
					// 返回玩家手动关闭支付中心的状态码，开发者可以在此处理相应的逻辑
					Toast.makeText(activity, "玩家关闭支付中心", Toast.LENGTH_LONG).show();
					
				}else if(mStatusCode == DkErrorCode.BDG_RECHARGE_FAIL){ 
					if(jsonObject.has(DkProtocolKeys.BD_ORDER_ID)){			
					}
					// 返回支付失败的状态码，开发者可以在此处理相应的逻辑
					Toast.makeText(activity, "购买失败", Toast.LENGTH_LONG).show();
					
				} else if(mStatusCode == DkErrorCode.BDG_RECHARGE_EXCEPTION){ 
					
					// 返回支付出现异常的状态码，开发者可以在此处理相应的逻辑
					Toast.makeText(activity, "购买出现异常", Toast.LENGTH_LONG).show();
					
				} else if(mStatusCode == DkErrorCode.BDG_RECHARGE_CANCEL){ 
					
					// 返回取消支付的状态码，开发者可以在此处理相应的逻辑
					Toast.makeText(activity, "玩家取消支付", Toast.LENGTH_LONG).show();
					
				} else {
					Toast.makeText(activity, "未知情况", Toast.LENGTH_LONG).show();
					
				}
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	};
	
	public static void exit() {
		final Activity activity = DemoApplication.getInstance().getMainActivity();
		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					DKPlatform.getInstance().bdgameExit(activity, new IDKSDKCallBack() {
						@Override
						public void onResponse(String paramString) {
							GameInterface.exit(activity, new GameInterface.GameExitCallback() {
								@Override
								public void onConfirmExit() {
									DemoApplication.getInstance().finishAll();
									android.os.Process.killProcess(android.os.Process.myPid());
								}
								@Override
								public void onCancelExit() {
								}
							});
						}
					});
				}catch (Exception e){
					e.printStackTrace();
				}
			}
		});
	}
	
	public static int logStat() {
		return Var.bduid.equals("") ? 0:1;
	}
	
	public static void modifyPass() {
		
		final Activity activity = DemoApplication.getInstance().getMainActivity();
		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				DKPlatform.getInstance().invokeBDModifyPwd(activity, loginlistener);
			}
		});
	}
	
	public static void login() {
		
		Var.bduid = "";
		
		final Activity activity = DemoApplication.getInstance().getMainActivity();
		activity.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				DKPlatform.getInstance().invokeBDLogin(activity, loginlistener);
			}
		});
	}
	
	// 回调函数
	static IDKSDKCallBack loginlistener = new IDKSDKCallBack() {
			@Override
			public void onResponse(String paramString) {
				int mFunctionCode = 0;
				final Activity activity = DemoApplication.getInstance().getMainActivity();
				try {
					JSONObject jsonObject = new JSONObject(paramString);
					// 返回的操作状态码
					mFunctionCode = jsonObject
							.getInt(DkProtocolKeys.FUNCTION_CODE);
					// 登陆成功
					if (mFunctionCode == DkErrorCode.DK_ACCOUNT_LOGIN_SUCCESS) {
						Log.d(TAG, "login success");
						// 返回的百度uid，供cp绑定使用
						String bduid = jsonObject
								.getString(DkProtocolKeys.BD_UID);
						Log.d(TAG, "bduid =" + bduid);
						Var.bduid = bduid;
						Toast.makeText(activity, "登录成功", Toast.LENGTH_SHORT).show();
						// 登陆失败
					} else if (mFunctionCode == DkErrorCode.DK_ACCOUNT_LOGIN_FAIL) {
						// 显示登陆按钮，隐藏修改密码和切换账号按钮
						Log.d(TAG, "login failed");
						// 快速注册成功
						Var.bduid = "";
						Toast.makeText(activity, "登录失败", Toast.LENGTH_SHORT).show();
					} else if (mFunctionCode == DkErrorCode.DK_ACCOUNT_QUICK_REG_SUCCESS) {
						// 快速试玩登陆成功，都需要隐藏
						Log.d(TAG, "quick reg success");
						Var.bduid = "";
					}else{
						Log.d(TAG, "login other fail");
						Var.bduid = "";
						Toast.makeText(activity, "登录失败", Toast.LENGTH_SHORT).show();
					}
	
				} catch (Exception e) {
					e.printStackTrace();
					Var.bduid = "";
					Toast.makeText(activity, "登录失败", Toast.LENGTH_SHORT).show();
				}finally{
					JNIHelper.callback(JNIHelper.LOGIN_COMPLETE , mFunctionCode);
				}
			}
		};
	
}
