//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008-2014, Shane Liesegang
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright 
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright 
//       notice, this list of conditions and the following disclaimer in the 
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the copyright holder nor the names of any 
//       contributors may be used to endorse or promote products derived from 
//       this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
// POSSIBILITY OF SUCH DAMAGE.
//////////////////////////////////////////////////////////////////////////////

#pragma once

extern "C"
{	
	#include "lua.h"
	#include "lauxlib.h"
	#include "lualib.h"
}

#include "../Util/StringUtil.h"
#include "../Messaging/Message.h"

///(Internal) Just sits and checks the tuning file periodically
/** 
 * This internal class periodically checks the tuning.lua file in the Config
 *  directory and reloads it if it's been modified. This allows you to change
 *  the file at runtime and see your tuning affects right away. 
 */
class ConfigUpdater : public MessageListener
{
public:
	ConfigUpdater();

	void Reload();

	virtual void ReceiveMessage(Message *message);

private:
	long _updateTime;
};


///(Internal) Handles all interactions with our Lua layer
/** 
 * This internal class is the glue between the compiled Angel simulation 
 *  engine and the scripting layer generated by SWIG. 
 * 
 * The World class handles all interactions for setup and teardown; the only
 *  bit you should care about for your game code is the #ExecuteInScript 
 *  function. 
 * 
 * For more information about Lua, visit http://www.lua.org
 */
class LuaScriptingModule
{
public:
	/**
	 * Does the very initial stages of setting up Lua so that it can be used
	 *  to load the preferences files. 
	 * 
	 * Called by the World during setup.
	 */
	static void Prep();
	
	/**
	 * Work the magic to set up Lua and load our extension module. 
	 * 
	 * Note that the start.lua file located in Scripting/EngineScripts will
	 *  get executed when this function is called. It's copied to the 
	 *  Resources/Scripts directory of all games as they're built. 
	 * 
	 * Called by the World during setup. 
	 */
	static void Initialize();
	
	/**
	 * Closes down Lua. Called by the World when it's destroyed. 
	 */
	static void Finalize();
	
	/**
	 * Execute a string in the Lua interpreter. Can be dangerous, but
	 *  can also make generating new interactions a lot easier. If you're 
	 *  using this function, just be careful about what you pass in. 
	 * 
	 * @param code The code to execute. 
	 */
	static void ExecuteInScript(const String& code);

	/**
	 * Get the internal Lua state that all our script code is using to run.
	 * 
	 * @return The a pointer to the lua_State object at the heart of things.
	 */
	static lua_State* GetLuaState();
	
	/**
	 * Print information about the current Lua stack to the system log. 
	 *  Useful for debugging any custom Lua integration.
	 */
	static void DumpStack();

private:
	static bool isInitialized;
	static lua_State *L;
	
	static ConfigUpdater* _configUpdater;
};
