#include "BeginScene.h"
#include "GameScene.h"
#include "SimpleAudioEngine.h"
#include "GameScene/Ball.h"
#include "params.h"

USING_NS_CC;

Scene* BeginScene::createScene() {
    auto scene = Scene::createWithPhysics();
    Size visibleSize = Director::getInstance()->getVisibleSize();
    
#if DEBUG
    scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
#endif
    
    auto layer = BeginScene::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;
}

bool BeginScene::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);
    
    auto menu_item_start = MenuItemFont::create(MSG_START, CC_CALLBACK_1(BeginScene::Play, this));
    menu_item_start->setFontNameObj(FONT);
    
    menu_item_start->setPosition(Point(visibleSize.width / 2, (visibleSize.height*0.25)));
    
    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);
    }
    
    auto core = DrawNode::create();
    core->drawDot(Vec2(0, 0), CORE_RADIUS*4, Color4F(COLOR_red));
    core->setPosition(Point(visibleSize.width / 2, (visibleSize.height*0.75)));
    this->addChild(core);
    
    auto physicsBody = PhysicsBody::createCircle(CORE_RADIUS*4, PhysicsMaterial(0.1f, 0.9f, 0.0f));
    physicsBody->setDynamic(false);
    core->addComponent(physicsBody);
    
    
    auto text = Label::createWithTTF(MSG_TITLE, FONT, 100);
    text->setPosition(core->getPosition());
    this->addChild(text);
    
    double last, diff;
    int level;
    
    UserDefault *userdata = UserDefault::getInstance();
    level = userdata->getIntegerForKey("level");
    last = userdata->getDoubleForKey("time");
    unsigned long int sec = time(NULL);
    diff = 3600 * LOSE_IS + last - sec;
    
    while (diff < 0 && level > 0) {
        last += 3600.0* ((double) LOSE_IS);
        level--;
        diff = 3600 * LOSE_IS + last - sec;
    }
    
    userdata->setDoubleForKey("time", last);
    userdata->setIntegerForKey("level", level);
    
    this->levels = level;
    
    char level_text[256];
    if (level != 0) {
        sprintf(level_text,MSG_LEVEL_BACK, level, diff/3600.0);
        
        auto textlevel = Label::createWithTTF(level_text, FONT, 20);
        textlevel->setPosition(menu_item_start->getPositionX(), menu_item_start->getPositionY()-60);
        this->addChild(textlevel);
    }
    
    return true;
}

void BeginScene::Play(cocos2d::Ref *pSender) {
    auto scene = GameScene::createScene(this->levels);
    Director::getInstance()->replaceScene(scene);
}


