/****************************************************************************
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 
 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.
 ****************************************************************************/

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "cocos/ui/CocosGUI.h"
#include "cocos2d.h" 
#include "CunDangScene.h"
#include "first.h"
using namespace cocos2d;
using namespace ui;
USING_NS_CC;

Scene* HelloWorld::createScene()
{
    return HelloWorld::create();
}

// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
    printf("Error while loading: %s\n", filename);
    printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first


    if ( !Scene::init() )
    {
        return false;
    }
	auto director = Director::getInstance();
	auto glview = director->getOpenGLView();
	glview->setFrameSize(850, 550);  //850, 550

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

	auto node8 = Sprite::create("zhujiemian.png");
	node8->setPosition(Vec2(1920, 1280));
	this->addChild(node8,-1);

	auto btn1 = Button::create("save.png", "save.png");
	btn1->setScale9Enabled(true);
	//btn->setTitleText("Jump");
	btn1->setTitleFontSize(35);
	btn1->setContentSize(Size(1000, 400));
	btn1->setPosition(Vec2(1920, 1800));
	btn1->addTouchEventListener([this, visibleSize](Ref* senderButton, ui::Widget::TouchEventType eventType) {
		auto button = static_cast<ui::Button*>(senderButton);
		switch (eventType) {
		case ui::Widget::TouchEventType::BEGAN: { CCLOG("Button Touch Began"); break; }
		case ui::Widget::TouchEventType::MOVED: { CCLOG("Button Touch Moved"); break; }
		case ui::Widget::TouchEventType::CANCELED: { CCLOG("Button Touch Cancelled"); break; }
		case ui::Widget::TouchEventType::ENDED: {
			CCLOG("Button Touch Ended");
			CCLOG("do action:");
			auto actionGrow = ScaleTo::create(.1f, 2.0f);
			auto actionShrink = ScaleTo::create(.1f, 1.0f);
			button->runAction(Sequence::createWithTwoActions(actionGrow, actionShrink));
			Scene* pScene = CunDangScene::createScene();
			Director::getInstance()->replaceScene(pScene);
			break;
		}
		}
	});
	this->addChild(btn1, 3);



	auto btn = Button::create("start.png", "start_1.png");
	btn->setScale9Enabled(true);
	//btn->setTitleText("Jump");
	btn->setTitleFontSize(35);
	btn->setContentSize(Size(1000, 400));
	btn->setPosition(Vec2(1920, 1280));
	btn->addTouchEventListener([this, visibleSize](Ref* senderButton, ui::Widget::TouchEventType eventType) {
		auto button = static_cast<ui::Button*>(senderButton);
		switch (eventType) {
		case ui::Widget::TouchEventType::BEGAN: { CCLOG("Button Touch Began"); break; }
		case ui::Widget::TouchEventType::MOVED: { CCLOG("Button Touch Moved"); break; }
		case ui::Widget::TouchEventType::CANCELED: { CCLOG("Button Touch Cancelled"); break; }
		case ui::Widget::TouchEventType::ENDED: {
			CCLOG("Button Touch Ended");
			CCLOG("do action:");
			auto actionGrow = ScaleTo::create(.1f, 2.f);
			auto actionShrink = ScaleTo::create(.1f, 1.f);
			button->runAction(Sequence::createWithTwoActions(actionGrow, actionShrink));
			//auto node7 = Sprite::create("ty.png");
			//node7->setPosition(Vec2(visibleSize.width - 960, 100));
			//this->addChild(node7);
			//auto moveTo = MoveTo::create(8, Vec2(visibleSize.width - 130, 100));
			//node7->runAction(moveTo);	
			Scene* pScene = first::createScene();
			Director::getInstance()->replaceScene(pScene);
			break;
		}
		}
	});
	this->addChild(btn, 2);
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    //Close the cocos2d-x game scene and quit the application
    Director::getInstance()->end();

    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif

    /*To navigate back to native iOS screen(if present) without quitting the application  ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/

    //EventCustom customEndEvent("game_scene_close_event");
    //_eventDispatcher->dispatchEvent(&customEndEvent);


}
