#include "AppDelegate.h"
#include "Config/Config.hpp"
#include "Game/GameScene/Scene.hpp"
#include "LoadingScreen.hpp"

USING_NS_CC;

AppDelegate::AppDelegate() {}

AppDelegate::~AppDelegate() {}

void AppDelegate::initGLContextAttrs() {
    
    GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
    
    GLView::setGLContextAttrs(glContextAttrs);
}

static int register_all_packages() {return 0;}

bool AppDelegate::applicationDidFinishLaunching() {
    
	CConfig* config = CConfig::getInstance();

    auto director = Director::getInstance();
    director->setProjection(cocos2d::Director::Projection::_2D);
    auto glview = director->getOpenGLView();
    if (!glview) {
        
		if (CConfig::m_Windowed == 1)
		{
			glview = GLViewImpl::createWithRect("Game", Rect(0, 0,
															 CConfig::m_ResoliutionX,
															 CConfig::m_ResoliutionY),
												(float)CConfig::m_WindowedY / (float)CConfig::m_ResoliutionY);
		}
		else
		{
			glview = GLViewImpl::createWithFullScreen("Game");
		}
        //glview = GLViewImpl::create("Game");
        
        director->setOpenGLView(glview);
    }
    
    director->setDisplayStats(true);
    director->setAnimationInterval(1.0f / 60.0);
    
    auto frameSize = glview->getFrameSize();
    register_all_packages();

    
    //auto scene = Game::CScene::createScene(1, 1);
    //director->runWithScene(scene);
    
    auto scene = CLoadingScreen::createScene();
    director->runWithScene(scene);
    
    return true;
}

// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
void AppDelegate::applicationDidEnterBackground() {
    Director::getInstance()->stopAnimation();
    
    // if you use SimpleAudioEngine, it must be paused
    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
    Director::getInstance()->startAnimation();
    
    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
