package org.cocos2dx.cpp;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.json.JSONObject;

import android.os.Bundle;
import android.util.Log;

import com.duoku.cocosx.demo.DemoApplication;
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;

public class AppActivity extends Cocos2dxActivity{
	
	private String TAG = getClass().getSimpleName();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Log.d(TAG, "AppActivity ------------ onCreate");
		
		// init sdk ，品宣初始化，微信支付插件初始化等
		initSDK();
		
		DemoApplication.getInstance().setMainActivity(this);
	}
	
	private void initSDK() {
		IDKSDKCallBack initcompletelistener = new IDKSDKCallBack() {
			@Override
			public void onResponse(String paramString) {
				Log.d(TAG, "SDK INIT RESULT:"+paramString);
				try {
					JSONObject jsonObject = new JSONObject(paramString);
					// 返回的操作状态码
					int mFunctionCode = jsonObject
							.getInt(DkProtocolKeys.FUNCTION_CODE);

					// 初始化完成
					if (mFunctionCode == DkErrorCode.BDG_CROSSRECOMMEND_INIT_FINSIH) {
						initAds();
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};

		// 参数为测试数据，接入时请填入你真实数据
		DKCMMMData mmData = new DKCMMMData("000000000000", "0000000000000000");

		DKCMGBData gbData = new DKCMGBData();

		// 初始化函数
		DKPlatform.getInstance().init(this, false,
				DKPlatformSettings.SdkMode.SDK_PAY, mmData, gbData,
				initcompletelistener);
	}
	
	
	
	private void initAds() {
		DKPlatform.getInstance().bdgameInit(this, new IDKSDKCallBack() {
			@Override
			public void onResponse(String paramString) {
				Log.d(TAG, "bggameInit success");
			}
		});
	}
	
	@Override
    protected void onResume(){
        super.onResume();
        Log.d(TAG, "AppActivity ------------ onResume");
        
        DKPlatform.getInstance().resumeBaiduMobileStatistic(this);
    }

    @Override
    protected void onPause(){
        super.onPause();
        
        Log.d(TAG, "AppActivity ------------ onPause");
        
        DKPlatform.getInstance().pauseBaiduMobileStatistic(this);
    }
    
    @Override
    protected void onStop() {
    	// TODO Auto-generated method stub
    	super.onStop();
    	
    	Log.d(TAG, "AppActivity ------------ onStop");
    }
    
    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	super.onDestroy();
    	
    	Log.d(TAG, "AppActivity ------------ onDestroy");
    }
    
    @Override
    public void setVisible(boolean visible) {
    	// TODO Auto-generated method stub
    	super.setVisible(visible);
    	
    	Log.d(TAG, "AppActivity ------------ setVisible");
    }
}
