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

#include "oubhack_iluaobject.h"
#include "oubhack_concoms.h"
#include "oubhack.h"

IConsoleCommands::IConsoleCommands( ){

	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->CreateTable();
	this->m_objCommands = oubhack->GetLuaInterface( LUA_STATE_CLIENT )->IGetObject();

}

void IConsoleCommands::Close(){

	this->m_objCommands->UnReference();

}

void IConsoleCommands::AddConsoleCommand( const char* ccName, ILuaObject* objWriteFunc, ILuaObject* objExecuteFunc, const char* ccHelp ){

	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->CreateTable();

	ILuaObject* objCom = oubhack->GetLuaInterface( LUA_STATE_CLIENT )->IGetObject();

	objCom->SetMember( "writeFunc", objWriteFunc ); 
	objCom->SetMember( "executeFunc", objExecuteFunc ); 
	//objCom->SetMember( "help", ccHelp );

	this->m_objCommands->SetMember( ccName, objCom );
	
	objCom->UnReference();

}

void IConsoleCommands::RemoveConsoleCommand( const char* ccName ){

	this->m_objCommands->m_pLua->PushNil();
	this->m_objCommands->SetMember( ccName );

}

ILuaObject* IConsoleCommands::GetConsoleCommandExecute( const char * ccName ){

	return this->m_objCommands->GetMember( ccName )->GetMember( "executeFunc" );

}

ILuaObject* IConsoleCommands::GetConsoleCommandWrite( const char * ccName ){

	return this->m_objCommands->GetMember( ccName )->GetMember( "writeFunc" );

}

const char* IConsoleCommands::GetConsoleCommandHelp( const char * ccName ){

	return this->m_objCommands->GetMember( ccName )->GetMemberStr( "help" );

}

ILuaObject* IConsoleCommands::GetConsoleCommandList( ){

	return this->m_objCommands;

}

write_res IConsoleCommands::WriteConsoleCommand( const char* ccName, const char* ccArgs ){

	this->m_objCommands->GetMember( ccName )->Push();

	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->PushString( ccName );

	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->PushString( ccArgs );

	oubhack->GetLuaInterface( LUA_STATE_CLIENT )->Call( 3, 2 );

	write_res res;

	res.sug = oubhack->GetLuaInterface( LUA_STATE_CLIENT )->GetString( 1 );
	res.sugs = oubhack->GetLuaInterface( LUA_STATE_CLIENT )->GetString( 2 );

	return res;

}

concom_res IConsoleCommands::ExecuteConsoleCommand( const char* ccName, const char* ccArgs ){

	concom_res res;

	if ( this->m_objCommands->GetMember( ccName )->isTable() ){

		if ( this->m_objCommands->GetMember( ccName )->GetMember( "executeFunc" )->isFunction() ){

			this->m_objCommands->GetMember( ccName )->GetMember( "executeFunc" )->Push();

			oubhack->GetLuaInterface( LUA_STATE_CLIENT )->PushString( ccName );
			oubhack->GetLuaInterface( LUA_STATE_CLIENT )->PushString( ccArgs );

			oubhack->GetLuaInterface( LUA_STATE_CLIENT )->Call( 2, 2 );

			res.bSuccess = oubhack->GetLuaInterface( LUA_STATE_CLIENT )->GetBool( 1 );
			res.ccErr = res.bSuccess ? "" : oubhack->GetLuaInterface( LUA_STATE_CLIENT )->GetString( 2 );

			oubhack->GetLuaInterface( LUA_STATE_CLIENT )->Pop();
			oubhack->GetLuaInterface( LUA_STATE_CLIENT )->Pop();

			return res;

		}

	}

	res.bSuccess = false;
	res.ccErr = "INTERNAL FUNCTION ERROR";

	return res;

}