#include "Application.h"

namespace BlueCore
{

static weak_ptr<Application> gApplication;

static SQInteger _quit(HSQUIRRELVM v)
{
	if (gApplication.valid())
		gApplication->quit();
	return 0;
}

static SQInteger _toggle_pause(HSQUIRRELVM v)
{
	if (gApplication.valid())
		gApplication->togglePause();
	return 0;
}

static SQInteger _setscenegraph(HSQUIRRELVM v)
{
	SceneGraph *scenegraph = 0;

	sq_getinstanceup(v, -1, ( void ** ) &scenegraph, 0);

	gApplication->setSceneGraph(scenegraph);

	return 0;
}

void setupScriptSystem_Application(ScriptSystem* scriptsystem,
		Application* application)
{
	if (application == 0)
		return;

	gApplication = application;

	HSQUIRRELVM vm = scriptsystem->getVM();
	sq_pushroottable(vm);

	sq_pushstring(vm, "quit", -1);
	sq_newclosure(vm, _quit, 0);
	sq_newslot(vm, -3, false);

	sq_pushstring(vm, "togglePause", -1);
	sq_newclosure(vm, _toggle_pause, 0);
	sq_newslot(vm, -3, false);

	sq_pushstring(vm, "setSceneGraph", -1);
	sq_newclosure(vm, _setscenegraph, 0);
	sq_newslot(vm, -3, false);

	sq_pop(vm, 1);
}

}
