/* * * * * * * * * * * * * * * * 
 * 	 ______     __  __     ______     __  __     ______     ______     __  __    
 *	/\  __ \   /\ \/\ \   /\  == \   /\ \_\ \   /\  __ \   /\  ___\   /\ \/ /    
 *	\ \ \/\ \  \ \ \_\ \  \ \  __<   \ \  __ \  \ \  __ \  \ \ \____  \ \  _"-.  
 *	 \ \_____\  \ \_____\  \ \_____\  \ \_\ \_\  \ \_\ \_\  \ \_____\  \ \_\ \_\ 
 *	  \/_____/   \/_____/   \/_____/   \/_/\/_/   \/_/\/_/   \/_____/   \/_/\/_/ 
 *
 *  - Version : 3L
 *  - By Oubliette
 * * * * * * * * * * * * * * * */

#include "oubhack.h"

/* ===============================
		Name : CallConsoleCommand
   =============================== */

LUA_FUNCTION( CallConsoleCommand ){

	oubhack->GetLuaInterface( LUA_STATE_MENU )->CheckType( 1, GLua::TYPE_STRING );
	oubhack->GetLuaInterface( LUA_STATE_MENU )->CheckType( 2, GLua::TYPE_STRING );
	
	const char* ccName = oubhack->GetLuaInterface( LUA_STATE_MENU )->GetString( 1 );
	const char* ccArgs = oubhack->GetLuaInterface( LUA_STATE_MENU )->GetString( 2 );

	concom_res res = oubhack->m_pConsoleCommands->ExecuteConsoleCommand( ccName, ccArgs );

	if ( res.bSuccess ){

		oubhack->GetLuaInterface( LUA_STATE_MENU )->PushBool( true );
		return 1;

	}
	else {

		oubhack->GetLuaInterface( LUA_STATE_MENU )->PushBool( false );
		oubhack->GetLuaInterface( LUA_STATE_MENU )->PushString( res.ccErr );
		return 2;

	}

}

/* ===============================
		Name : SetCallback
   =============================== */

LUA_FUNCTION( MSetCallback ){ // Conflicts with client one

	oubhack->GetLuaInterface( LUA_STATE_MENU )->CheckType( 1, GLua::TYPE_STRING );
	oubhack->GetLuaInterface( LUA_STATE_MENU )->CheckType( 2, GLua::TYPE_FUNCTION );

	const char* ccName = oubhack->GetLuaInterface( LUA_STATE_MENU )->GetString( 1 );
	ILuaObject* func = oubhack->GetLuaInterface( LUA_STATE_MENU )->IGetObject( 2 );

	oubhack->m_pMHooks->SetHook( ccName, func );

	func->UnReference();

	return 0;

}

/* ===============================
		Name : RunString
   =============================== */

LUA_FUNCTION( MRunString ){ // Conflicts with client one

	oubhack->GetLuaInterface( LUA_STATE_MENU )->CheckType( 1, GLua::TYPE_STRING );

	const char* ccString = oubhack->GetLuaInterface( LUA_STATE_MENU )->GetString( 1 );

	oubhack->GetLuaInterface( LUA_STATE_MENU )->RunString( "", "", ccString, true, true );

	return 0;

}

/* ===============================
		Name : RawRead
   =============================== */

LUA_FUNCTION( MRawRead ){

	oubhack->GetLuaInterface( LUA_STATE_MENU )->CheckType( 1, GLua::TYPE_STRING );

	const char* ccPath = oubhack->GetLuaInterface( LUA_STATE_MENU )->GetString( 1 );

	oubhack->GetLuaInterface( LUA_STATE_MENU )->PushString( oubhack->m_pFileIO->ReadFile( ccPath ).c_str() );

	return 1;

}

/* ===============================
   =============================== */

void OubHack::InitMenuBindings(){

	oubhack->Debug( "Loading menu C++ => Lua bindings " );

	this->GetLuaInterface( LUA_STATE_MENU )->NewGlobalTable( "OH_MENU_INTERFACE" );

	ILuaObject* l_oInterface = this->GetLuaInterface( LUA_STATE_MENU )->GetGlobal( "OH_MENU_INTERFACE" );
	
	l_oInterface->SetMember( "CallConsoleCommand", CallConsoleCommand );

	l_oInterface->SetMember( "SetCallback", MSetCallback );

	l_oInterface->SetMember( "RunString", MRunString );
	l_oInterface->SetMember( "RawRead", MRawRead );

	this->Log( Color( 0, 200, 100, 255 ), "Lua menu bindings initialized!" );

	l_oInterface->UnReference();

}