#ifndef GlobalDevice_h_INCLUDED
#define GlobalDevice_h_INCLUDED

//---------------------------------------------------------------------

#include <irrlicht.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

//---------------------------------------------------------------------
//The event manager allows you to get keyboard and mouse states

class EventManager
{
	friend class GlobalDevice;
public:
	bool keys[irr::KEY_KEY_CODES_COUNT];
	bool mouseLeft;
	bool mouseRight;
	bool mouseMiddle;
	f32 mouseWheel;
	s32 mouseX;
	s32 mouseY;
	s32 mouseDX;
	s32 mouseDY;
private:
	//Please use the GlobalDevice to get the EventManager
	EventManager();
};

//---------------------------------------------------------------------
// The GlobalDevice is the main object to retreive irrlicht device and other things.
// There shall be only one GlobalDevice instance.

class GlobalDevice
{
public:
	static dimension2d<s32> getDesktopResolution();

	//Constructor
	GlobalDevice(const dimension2d<s32>& windowSize, bool isFullscreen, irr::video::E_DRIVER_TYPE driverType);

	//----------------------
	// all functions below require the GlobalDevice instance to be created
	
	//Irrlicht main classes accessors
	inline static IrrlichtDevice* getDevice() {return device;}
	inline static ISceneManager* getSceneManager() {return device->getSceneManager();}
	inline static IVideoDriver* getVideoDriver() {return device->getVideoDriver();}
	inline static IGUIEnvironment* getGUIEnvironment() {return device->getGUIEnvironment();}
	
	inline static EventManager* getEventManager() {return eventManager;}

	//Utilities to load meshs
	static IBillboardSceneNode* loadBillboardNodeFromTexture(const char* filename, float ratioPixelTo3D, dimension2d<s32>& outTextureOriginalSize, bool hasAlphaChannel = true);
	static IMeshSceneNode* loadNodeFromMeshFile(const char* filename, bool hasAlphaChannel = false);
	static IAnimatedMeshSceneNode* loadAnimatedNodeFromMeshFile(const char* filename, bool hasAlphaChannel = false);
	
	//Duration of one frame in seconds (i.e. 1/FPS).
	//Use this instead of irrlicht's getFPS() which does not work well
	static float getFrameDuration();
	
	static const position2d<s32>& getMainWindowSize();
	
	//Print something on console
	static void println(const char* text);

	//Update frame duration value. You MUST call it once per loop
	static void updateFrameDuration();

	static void draw2DImageTranspChannel(ITexture* tex, position2d<s32> pos);

	static float getRandomNumber(float min, float max);

	//Destructor
	~GlobalDevice();
private:
	static IrrlichtDevice* device;
	static EventManager* eventManager;
};

//---------------------------------------------------------------------

#endif //GlobalDevice_h_INCLUDED
