/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011      Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
 
http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 ****************************************************************************/
package com.duoku.cocosx.demo;

import org.cocos2dx.cpp.AppActivity;
import org.json.JSONObject;

import com.duoku.platform.single.DKPlatform;
import com.duoku.platform.single.DkErrorCode;
import com.duoku.platform.single.DkProtocolKeys;
import com.duoku.platform.single.callback.IDKSDKCallBack;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * Due to the Cocos2dxActivity's draw scene can not be scheduled
 * 
 * I choose to do login and splash in launcher.
 * 
 * LauncherActivity should not be finished until app end
 * 
 * use to receive bd pass broadcast
 * 
 * @author Administrator
 */
public class LauncherActivity extends Activity {

	private IDKSDKCallBack loginlistener;
	
	private String TAG = getClass().getSimpleName();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		initLogin();
		
		DemoApplication.getInstance().in(this);
	}
	
	// 登陆初始化
	private void initLogin() {
		// 回调函数
		loginlistener = new IDKSDKCallBack() {
			@Override
			public void onResponse(String paramString) {
				try {
					JSONObject jsonObject = new JSONObject(paramString);
					// 返回的操作状态码
					int 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;
						
						// 登陆失败
					} else if (mFunctionCode == DkErrorCode.DK_ACCOUNT_LOGIN_FAIL) {
						// 显示登陆按钮，隐藏修改密码和切换账号按钮
						Log.d(TAG, "login failed");
						Var.bduid = "";
						// 快速注册成功
					} else if (mFunctionCode == DkErrorCode.DK_ACCOUNT_QUICK_REG_SUCCESS) {
						// 快速试玩登陆成功，都需要隐藏
						Log.d(TAG, "quick reg success");
						Var.bduid = "";
					}

				} catch (Exception e) {
					e.printStackTrace();
					Var.bduid = "";
				}finally{
					enterGame();
				}
			}
		};
		DKPlatform.getInstance().invokeBDInit(this, loginlistener);
	}

	protected void enterGame() {
		startActivity(new Intent(this , AppActivity.class));
	}
	
	@Override
    protected void onResume(){
        super.onResume();
        DKPlatform.getInstance().resumeBaiduMobileStatistic(this);
    }

    @Override
    protected void onPause(){
        super.onPause();
        
        DKPlatform.getInstance().pauseBaiduMobileStatistic(this);
    }
}
