#include "HelloWorldScene.h"
#include "FuncellGameSdkProxy.h"
USING_NS_CC;

CCSize visibleSize;
CCPoint origin;

FuncellSession *mSession;


class InitCallBackImpl : public IFuncellInitCallBack{
public:
	virtual void onInitSuccess(){

	}
	virtual void onInitFailure(const char *msg){

	}
};

class SessionCallBackImpl : public IFuncellSessionCallBack{
public:
	virtual void onLoginSuccess(FuncellSession *session){
		mSession = session;
	}
	virtual void onLoginCancel(){

	}
	virtual void onLoginFailed(char *paramString){

	}
	virtual void onLogout(){

	}
};

class PayCallBackImpl : public IFuncellPayCallBack{
	virtual void onSuccess(char *cpOrder, char *sdkOrder,
			char *extrasParams){

	}
	virtual void onFail(char *paramString){

	}
	virtual void onCancel(char *paramString){

	}
};


CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
	if ( !CCLayer::init() )
	    {
	        return false;
	    }
	FuncellGameSdkProxy::Init(new InitCallBackImpl(),new SessionCallBackImpl(),new PayCallBackImpl());
	//可视区域大小
	    visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	    //可视区域的起点坐标
	    origin = CCDirector::sharedDirector()->getVisibleOrigin();


	    /***************************设置背景图片*****************************/
	    CCSprite* pSprite = CCSprite::create("funcell_demo_background.jpg");

	    CCSize size = CCDirector::sharedDirector()->getWinSize();

	    float winw = size.width; //获取屏幕宽度
	    float winh = size.height;//获取屏幕高度


	    float spx = pSprite->getTextureRect().getMaxX();
	    float spy = pSprite->getTextureRect().getMaxY();

	    pSprite->setScaleX(winw/spx); //设置精灵宽度缩放比例
	    pSprite->setScaleY(winh/spy);


	    // position the sprite on the center of the screen
	    pSprite->setPosition(ccp(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));

	    // add the sprite as a child to this layer
	    this->addChild(pSprite, 0);

	    CCMenuItemImage *loginButton = CCMenuItemImage::create("funcell_demo_login_button_nomal.png", "funcell_demo_login_button_click.png", this, menu_selector(HelloWorld::loginCallback));

	    CCMenuItemImage *logoutButton = CCMenuItemImage::create("funcell_demo_logout_button_nomal.png", "funcell_demo_logout_button_click.png", this, menu_selector(HelloWorld::logoutCallback));

	    CCMenuItemImage *payButton = CCMenuItemImage::create("funcell_demo_pay_button_nomal.png", "funcell_demo_pay_button_click.png", this, menu_selector(HelloWorld::payCallback));

	    CCMenuItemImage *exitButton = CCMenuItemImage::create("funcell_demo_exit_button_nomal.png", "funcell_demo_exit_button_click.png", this, menu_selector(HelloWorld::exitCallback));

	    CCMenuItemImage *loginevent = CCMenuItemImage::create("loginevent.png", "loginevent1.png", this, menu_selector(HelloWorld::setLoginEventCallback));
	    CCMenuItemImage *CreatRoleEvent = CCMenuItemImage::create("creatrole.png", "creatrole1.png", this, menu_selector(HelloWorld::setCreatRoleEventCallback));
	    CCMenuItemImage *RoleLevelUpEvent = CCMenuItemImage::create("rolelevelup.png", "rolelevelup1.png", this, menu_selector(HelloWorld::setRoleLevelUpEventCallback));
	    CCMenuItemImage *Server_RoleEvent = CCMenuItemImage::create("role_server.png", "role_server1.png", this, menu_selector(HelloWorld::setServer_RoleEventCallback));


	    loginButton->setPosition(ccp(origin.x + loginButton->getContentSize().width - 40, origin.y + visibleSize.height - 50));
	    logoutButton->setPosition(ccp(origin.x + logoutButton->getContentSize().width - 40  ,origin.y + visibleSize.height - loginButton->getContentSize().height - 50 -30));
	    exitButton->setPosition(ccp(origin.x + exitButton->getContentSize().width - 40,origin.y + visibleSize.height - exitButton->getContentSize().height*4 - 50 -30*4));
	    payButton->setPosition(ccp(origin.x + payButton->getContentSize().width - 40, origin.y + visibleSize.height - payButton->getContentSize().height*3 - 50 -30*3));
	    loginevent->setPosition(ccp(origin.x + loginevent->getContentSize().width - 40, origin.y + visibleSize.height - loginevent->getContentSize().height*5 - 50 -30*5));
	    CreatRoleEvent->setPosition(ccp(origin.x + CreatRoleEvent->getContentSize().width - 40, origin.y + visibleSize.height - CreatRoleEvent->getContentSize().height*6 - 50 -30*6));
	    RoleLevelUpEvent->setPosition(ccp(origin.x + RoleLevelUpEvent->getContentSize().width - 40, origin.y + visibleSize.height - RoleLevelUpEvent->getContentSize().height*7 - 50 -30*7));
	    Server_RoleEvent->setPosition(ccp(origin.x + Server_RoleEvent->getContentSize().width - 40, origin.y + visibleSize.height - Server_RoleEvent->getContentSize().height*8 - 50 -30*8));


	    CCMenu* pMenu = CCMenu::create(loginButton,logoutButton,exitButton,payButton,loginevent,CreatRoleEvent,RoleLevelUpEvent,Server_RoleEvent,NULL);
	    pMenu->setPosition(CCPointZero);
	    this->addChild(pMenu,1,2);
    
    return true;
}

class ExitCallBackImpl : public IFuncellExitCallBack{
public:
	virtual void onChannelExit(){

	}
	virtual void onGameExit(){

	}
};


void HelloWorld::loginCallback(CCObject* pSender)
{
    printf("set Logincallback\n");
    FuncellGameSdkProxy::Login();
}

void HelloWorld::logoutCallback(CCObject* pSender){
	printf("set logoutCallback\n");
	FuncellGameSdkProxy::Logout();
}

void HelloWorld::exitCallback(CCObject* pSender){
	printf("set exitCallback\n");
	FuncellGameSdkProxy::Exit(new ExitCallBackImpl());
}

void HelloWorld::payCallback(CCObject* pSender)
{
    printf("set payCallback\n");

	string RoleName = "test1";
	string RoleId = "123";
	string RoleLevel = "2";
	string ServerName = "test";
	string ServerId = "998";
	string VipLevel = "3";
	string GameGoldBalance = "888";
	string GameUnionName = "游戏工会";
	RoleInfo *roleInfo = new RoleInfo();
	roleInfo->setRoleName((char*) RoleName.c_str());
	roleInfo->setRoleId((char*) RoleId.c_str());
	roleInfo->setRoleLevel((char*) RoleLevel.c_str());
	roleInfo->setServerName((char*) ServerName.c_str());
	roleInfo->setServerId((char*) ServerId.c_str());
	roleInfo->setVipLevel((char*) VipLevel.c_str());
	roleInfo->setGameGoldBalance((char*) GameGoldBalance.c_str());
	roleInfo->setGameUnionName((char*) GameUnionName.c_str());

    FuncellPayParams *PayParams = new FuncellPayParams();
    PayParams->setItemName("10元宝");
    PayParams->setItemId("100002");
    PayParams->setItemCount(10);
    PayParams->setItemDescription("商品描述");
    PayParams->setItemAmount(10);
    PayParams->setExtrasParams("test");
    PayParams->setItemType("元宝");
    PayParams->setOrderId("游戏订单号");
    PayParams->setRoleInfo(roleInfo);

    FuncellGameSdkProxy::Pay(PayParams);
}

void HelloWorld::setLoginEventCallback(CCObject* pSender){
	if (mSession == NULL) {
		return;
	}
	string serverno = "998";
	const char *userId = mSession->getUserId();
	char  json[128] = {0};
	sprintf(json,"{\"usermark\":\"%s\",\"serverno\":\"%s\"}",userId,serverno.c_str());//拼装json数据
	FuncellGameSdkProxy::SetDatas(DATA_LOGIN,json);
}
void HelloWorld::setRoleLevelUpEventCallback(CCObject* pSender) {
	if (mSession == NULL) {
		return;
	}
	const char *userId = mSession->getUserId();
	string serverno = "998";
	string level = "999";
	string role_id = "123";
	string role_name = "test";
	string server_name = "天下";
	char  json[256] = {0};
	sprintf(json,
			"{\"usermark\":\"%s\",\"serverno\":\"%s\",\"level\":\"%s\",\"role_id\":\"%s\",\"role_name\":\"%s\",\"server_name\":\"%s\"}",
			userId, serverno.c_str(), level.c_str(), role_id.c_str(),role_name.c_str(),server_name.c_str()); //拼装json数据
	FuncellGameSdkProxy::SetDatas(DATA_ROLE_LEVELUP,json);
}
void HelloWorld::setCreatRoleEventCallback(CCObject* pSender) {
	if (mSession == NULL) {
		return;
	}
	const char *userId = mSession->getUserId();
	string serverno = "998";
	string role_id = "123";
	string role_name = "test";
	string server_name = "天下";
	char json[256] = { 0 };
	sprintf(json,
			"{\"usermark\":\"%s\",\"serverno\":\"%s\",\"role_id\":\"%s\",\"role_name\":\"%s\",\"server_name\":\"%s\"}",
			userId, serverno.c_str(), role_id.c_str(),role_name.c_str(),server_name.c_str()); //拼装json数据
	FuncellGameSdkProxy::SetDatas(DATA_CREATE_ROLE,json);
}
void HelloWorld::setServer_RoleEventCallback(CCObject* pSender) {
	if (mSession == NULL) {
		return;
	}

	const char *userId = mSession->getUserId();
	string serverno = "998";
	string role_level = "999";
	string role_id = "123";
	string role_name = "test";
	string server_name = "天下";
	string role_vip_level = "VIP等级";
	string role_gameunion_name = "工会";
	char json[256] = { 0 };

	sprintf(json,
			"{\"usermark\":\"%s\",\"serverno\":\"%s\",\"role_id\":\"%s\",\"role_name\":\"%s\",\"server_name\":\"%s\",\"role_level\":\"%s\",\"role_vip_level\":\"%s\",\"role_gameunion_name\":\"%s\"}",
			userId, serverno.c_str(), role_id.c_str(), role_name.c_str(),
			server_name.c_str(), role_level.c_str(), role_vip_level.c_str(),
			role_gameunion_name.c_str()); //拼装json数据
	FuncellGameSdkProxy::SetDatas(DATA_SERVER_ROLE_INFO,json);

}
void HelloWorld::getExitUI(CCObject* pSender) {
	if (mSession == NULL) {
		return;
	}
	FuncellGameSdkProxy::GetExitUI();
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}
