#include "../mainproc/GameMain.h"
#include "MessageBox.h"

CMessageBox*			g_pMessageBoxFrame	= NULL;

int	LuaFunc_ProcessMessageBox			( lua_State *L );

// ----------------------------------------------------------------------------
CMessageBox::CMessageBox(CInterface* object)
	: CInterfaceBase(object)
{
	g_pMessageBoxFrame = this;
}

// ----------------------------------------------------------------------------
CMessageBox::~CMessageBox()
{
	g_pMessageBoxFrame = NULL;
}
// ----------------------------------------------------------------------------
void CMessageBox::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register( L, "ProcessMessageBox",			LuaFunc_ProcessMessageBox );
	}
}
// ----------------------------------------------------------------------------
void CMessageBox::MessageBox( MessageBoxInfo msgInfo, pfMessageBoxCB pfun )
{
	lua_State* L = GetLuaStateWorld();
	
	if ( L == NULL )
		return;

	lua_pushnumber(L, msgInfo.iType );
	lua_setglobal(L, "arg1");
	
	lua_pushstring(L, msgInfo.szString.c_str() );
	lua_setglobal(L, "arg2");

	lua_pushnumber(L, msgInfo.iNumButton );
	lua_setglobal(L, "arg3");

	lua_pushstring(L, msgInfo.szButtonName[0].c_str() );
	lua_setglobal(L, "arg4");

	lua_pushstring(L, msgInfo.szButtonName[1].c_str() );
	lua_setglobal(L, "arg5");

	lua_pushnumber(L, msgInfo.iInputMode );
	lua_setglobal(L, "arg6");

	m_pfun = pfun;

	g_pGameMain->GetInterface()->GetUiWorld()->SendEvent( UiEvent_InitMessageBox );

}
// ----------------------------------------------------------------------------
int	LuaFunc_GetMessageBoxInfo			( lua_State *L )
{

	return 0;
}
// ----------------------------------------------------------------------------
int	LuaFunc_ProcessMessageBox			( lua_State *L )
{
	int iProcess	= luaL_checkint( L, 1 );
	

	if( iProcess > 0 && iProcess < 3 )
	{
		switch( iProcess )
		{
		case 1:
			{
				const char* cszString = luaL_checkstring( L, 2 );
				if( g_pMessageBoxFrame->m_pfun )
					g_pMessageBoxFrame->m_pfun( EM_MB_BUTTON1, cszString );

			} break;
		case 2:
			{
				const char* cszString = luaL_checkstring( L, 2 );
				if( g_pMessageBoxFrame->m_pfun )
					g_pMessageBoxFrame->m_pfun( EM_MB_BUTTON1, cszString );
			} break;
		}


	}

	return 0;
}