#include <windows.h>
#include "../uimessageframe.h"

int MessageFrame_AddMessage(lua_State* L);
int MessageFrame_GetLines(lua_State* L);
int MessageFrame_GetPosition(lua_State* L);
int MessageFrame_SetPosition(lua_State* L);
int	MessageFrame_ResetTime(lua_State* L);
int MessageFrame_ScrollDown(lua_State* L);
int MessageFrame_ScrollUp(lua_State* L);

// ----------------------------------------------------------------------------------
static const struct luaL_reg g_messageframeTable [] = {
	{"AddMessage",			MessageFrame_AddMessage},
	{"GetLines",			MessageFrame_GetLines},
	{"GetPosition",			MessageFrame_GetPosition},
	{"SetPosition",			MessageFrame_SetPosition},
	{"ResetTime",			MessageFrame_ResetTime},
	{"ScrollDown",			MessageFrame_ScrollDown},
	{"ScrollUp",			MessageFrame_ScrollUp},
	{NULL, NULL}
};

// ----------------------------------------------------------------------------------
void CUiLuaMessageFrame::OpenLib(lua_State* L)
{
	luaL_newmetatable(L, UI_LUA_TYPE_MESSAGEFRAME);		// create new metatable
	luaL_getmetatable(L, UI_LUA_TYPE_FRAME);			// inherit layout
	lua_setmetatable(L, -2);
	lua_pushstring(L, "__index");
	lua_pushvalue(L, -2);
	lua_settable(L, -3);
	luaL_openlib(L, NULL, g_messageframeTable, 0);
}

// ----------------------------------------------------------------------------------
int	CUiLuaMessageFrame::SetMetaTable(lua_State* L)
{
	if (lua_istable(L, -1))
	{
		luaL_getmetatable(L, UI_LUA_TYPE_MESSAGEFRAME);
		lua_setmetatable(L, -2);
		return 1;
	}
	return 0;
}

// ----------------------------------------------------------------------------------
int MessageFrame_AddMessage(lua_State* L)
{
	CUiMessageFrame* messageframe = dynamic_cast<CUiMessageFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (messageframe == NULL)
		lua_error(L);
	
	const char* text = luaL_checkstring(L, 2);
	CUiColor color = CUiLuaLayout::GetColor(L, 3);
	if (text)
	{
		WCHAR* tmp = TransToWChar(text);
		messageframe->AddMessage(tmp, color.GetColor());
		UI_SAFE_DELETE_ARRAY(tmp);
	}
	return 0;
}

// ----------------------------------------------------------------------------------
int MessageFrame_GetLines(lua_State* L)
{
	CUiMessageFrame* messageframe = dynamic_cast<CUiMessageFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (messageframe == NULL)
		lua_error(L);

	lua_pushnumber(L, messageframe->GetSizeLines());
	return 1;
}

// ----------------------------------------------------------------------------------
int MessageFrame_GetPosition(lua_State* L)
{
	CUiMessageFrame* messageframe = dynamic_cast<CUiMessageFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (messageframe == NULL)
		lua_error(L);

	lua_pushnumber(L, messageframe->GetLastLine());
	return 1;
}

// ----------------------------------------------------------------------------------
int MessageFrame_SetPosition(lua_State* L)
{
	CUiMessageFrame* messageframe = dynamic_cast<CUiMessageFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (messageframe == NULL)
		lua_error(L);

	messageframe->SetLastLine(luaL_checkint(L, 2));
	return 0;
}

// ----------------------------------------------------------------------------------
int	MessageFrame_ResetTime(lua_State* L)
{
	CUiMessageFrame* messageframe = dynamic_cast<CUiMessageFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (messageframe == NULL)
		lua_error(L);

	messageframe->ResetTime();
	return 0;
}

// ----------------------------------------------------------------------------------
int MessageFrame_ScrollDown(lua_State* L)
{
	CUiMessageFrame* messageframe = dynamic_cast<CUiMessageFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (messageframe == NULL)
		lua_error(L);

	messageframe->ResetTime();
	messageframe->SetLastLine(messageframe->GetLastLine()+1);
	return 0;
}

// ----------------------------------------------------------------------------------
int MessageFrame_ScrollUp(lua_State* L)
{
	CUiMessageFrame* messageframe = dynamic_cast<CUiMessageFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (messageframe == NULL)
		lua_error(L);

	messageframe->ResetTime();
	messageframe->SetLastLine(messageframe->GetLastLine()-1);
	return 0;
}