#include "HelloWorldScene.h"
#include "GameScene.h"
#include "SimpleAudioEngine.h"
#include "Ball.h"
#include "params.h"

USING_NS_CC;

Scene* HelloWorld::createScene() {
    auto scene = Scene::createWithPhysics();
    Size visibleSize = Director::getInstance()->getVisibleSize();
    
    //choose whitch part need to draw, Joint, Shape, Contact, None or All
    //scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
    
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    
    auto material = PHYSICSBODY_MATERIAL_DEFAULT;
    material.density = 1.0f;
    material.restitution = 1.0f;
    material.friction = 0.0f;
    
    // the edge of the screen
    auto body = PhysicsBody::createEdgeBox(visibleSize, material);
    auto edgeNode = Node::create();
    edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
    body->setDynamic(false);
    edgeNode->setPhysicsBody(body);
    scene->addChild(edgeNode);
    
    return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init() {
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    // Adiona o fundo
    auto bg = cocos2d::LayerColor::create(COLOR_back);
    this->addChild(bg);
    

    /////////////////////////////
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    // auto closeItem = MenuItemImage::create(
    //                                        "CloseNormal.png",
    //                                        "CloseSelected.png",
    //                                        CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
    
    // closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
    //                             origin.y + closeItem->getContentSize().height/2));

    // // create menu, it's an autorelease object
    // auto menu = Menu::create(closeItem, NULL);
    // menu->setPosition(Vec2::ZERO);
    // this->addChild(menu, 1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    
    auto menu_item_start = MenuItemFont::create("Start", CC_CALLBACK_1(HelloWorld::Play, this));

    menu_item_start->setPosition(Point(visibleSize.width / 2, (visibleSize.height / 2)));
    
    auto *menu = Menu::create(menu_item_start, NULL);
    menu->setPosition(Point(0, 0));
    this->addChild(menu, 10);

    for (int i = 0; i < 10; i++) {
        auto ball = Ball::create();
        ball->setPosition(visibleSize.width / 2, BALL_SIZE*2);
        ball->throwBall();
        this->addChild(ball);
    }

    
    return true;
}

void HelloWorld::Play(cocos2d::Ref *pSender) {
    auto scene = GameScene::createScene();
    Director::getInstance()->replaceScene(scene);
    //Director::getInstance()->end();
}


