#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

std::string menus [] ={
"普通支付",
"微信支付",
"支付宝支付",
"短信支付"
};

MenuItemFont *loginMenu;
MenuItemFont *exchangeMenu;
MenuItemFont *modifyMenu;
MenuItemFont *exitMenu;

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    
    auto layer = HelloWorld::create();

    scene->addChild(layer);

    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }

    auto visibleSize = Director::getInstance()->getVisibleSize();

    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    auto closeItem = MenuItemImage::create("img/CloseNormal.png",
    										"img/CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

    closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 - 20 ,
                                origin.y + closeItem->getContentSize().height/2));

    //bg
    auto sprite = Sprite::create("img/demoBg.jpg");
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(sprite, 0);

    //pay menu
    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);

    Point beginPos = Point(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - 50);
	float step = 60.0f;
	int menuCount = sizeof(menus) / sizeof(std::string);

	for (int i = 0; i < menuCount; ++i) {
		std::string caseName = menus[i];

		log("in the condition %d" , i);

		MenuItemFont *pItem;
		pItem = MenuItemFont::create(caseName.c_str(), CC_CALLBACK_1(HelloWorld::menuCallback, this));

		pItem->setTag(i);
		pItem->setPosition(Point(beginPos.x, beginPos.y - i * step));
		menu->addChild(pItem);
	}

	//login menu
	auto menuLogin = Menu::create(closeItem, NULL);
	menuLogin->setPosition(Vec2::ZERO);
	this->addChild(menuLogin, 1);

	int flag = 0;
	loginMenu = MenuItemFont::create("账号登录", CC_CALLBACK_1(HelloWorld::menuCallback, this));
	loginMenu->setTag(flag++ + menuCount);
	loginMenu->setPosition(Point(origin.x + 80, origin.y + visibleSize.height - 550));
	menuLogin->addChild(loginMenu);
	exchangeMenu = MenuItemFont::create("切换账号", CC_CALLBACK_1(HelloWorld::menuCallback, this));
	exchangeMenu->setTag(flag++ + menuCount);
	exchangeMenu->setPosition(Point(origin.x + visibleSize.width - 80, origin.y + visibleSize.height - 550));
	menuLogin->addChild(exchangeMenu);
	modifyMenu = MenuItemFont::create("修改密码", CC_CALLBACK_1(HelloWorld::menuCallback, this));
	modifyMenu->setTag(flag++ + menuCount);
	modifyMenu->setPosition(Point(origin.x + visibleSize.width - 80, origin.y + visibleSize.height - 670));
	menuLogin->addChild(modifyMenu);
	exitMenu = MenuItemFont::create("退出游戏", CC_CALLBACK_1(HelloWorld::menuCallback, this));
	exitMenu->setTag(flag++ + menuCount);
	exitMenu->setPosition(Point(origin.x + 80, origin.y + visibleSize.height - 670));
	menuLogin->addChild(exitMenu);

	this->updateMenuState();

	//reg event listener
	auto listener = EventListenerCustom::create("logincomplete", [=](EventCustom* event){
	        log("logincomplete received.........");
	        this->updateMenuState();
	    });

	auto dispatcher = Director::getInstance()->getEventDispatcher();
	dispatcher->addEventListenerWithFixedPriority(listener ,1);

    return true;
}

void HelloWorld::menuCallback(Ref* pSender)
{
    MenuItemFont *pItem = (MenuItemFont*) pSender;
    Scene* newScene = NULL;

    auto className = "com.duoku.cocosx.demo.JavaProxy";
    auto methodName = "";
    auto params = "()V";

    switch (pItem->getTag()) {
    case 0:
    	methodName = "pay";
    	break;
    case 1:
    	methodName = "wxPay";
		break;
    case 2:
    	methodName = "alipay";
		break;
    case 3:
		methodName = "hfPay";
		break;
    case 4:
		methodName = "login";
		break;
    case 5:
		methodName = "login";
		break;
    case 6:
		methodName = "modifyPass";
		break;
    case 7:
		methodName = "exit";
		break;
    default:
        break;
    }

    JniMethodInfo t;
	bool hasMethod = JniHelper::getStaticMethodInfo(t , className , methodName , params);
	if(!hasMethod){
		log("JNI ERROR !!!!!!!!!! method invalid , %s , %s , %s" , className , methodName , params);
	}
	else{
		log("find method ,ready to call ---> , %s , %s , %s" , className , methodName , params);
		t.env->CallStaticVoidMethod(t.classID , t.methodID , params);
	}
}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
	auto className = "com.duoku.cocosx.demo.JavaProxy";
	auto methodName = "exit";
	auto params = "()V";

	JniMethodInfo t;
	bool hasMethod = JniHelper::getStaticMethodInfo(t , className , methodName , params);
	if(!hasMethod){
		log("JNI ERROR !!!!!!!!!! method invalid , %s , %s , %s" , className , methodName , params);
	}
	else{
		log("find method ,ready to call ---> , %s , %s , %s" , className , methodName , params);
		t.env->CallStaticVoidMethod(t.classID , t.methodID , params);
	}
}

void HelloWorld::updateMenuState()
{
	int loginState = this->getLoginState();
	loginMenu->setVisible(loginState == 0);
	exchangeMenu->setVisible(loginState != 0);
	modifyMenu->setVisible(loginState != 0);
}

int HelloWorld::getLoginState()
{
	auto className = "com.duoku.cocosx.demo.JavaProxy";
	auto methodName = "logStat";
	auto params = "()I";

	int state = 0;

	JniMethodInfo t;
	bool hasMethod = JniHelper::getStaticMethodInfo(t , className , methodName , params);
	if(!hasMethod){
		log("JNI ERROR !!!!!!!!!! method invalid , %s , %s , %s" , className , methodName , params);
	}
	else{
		log("find method ,ready to call ---> , %s , %s , %s" , className , methodName , params);
		state = t.env->CallStaticIntMethod(t.classID , t.methodID , params);

		log("log state ---> , %d" , state);
	}
	return state;
}

