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

int Tooltip_AddLine				(lua_State* L);
int Tooltip_AddDoubleLine		(lua_State* L);
int Tooltip_ClearLines			(lua_State* L);
int Tooltip_SetText				(lua_State* L);

// ----------------------------------------------------------------------------------
static const struct luaL_reg g_tooltipTable [] = {		
	{"AddLine",				Tooltip_AddLine},
	{"AddDoubleLine",		Tooltip_AddDoubleLine},
	{"ClearLines",			Tooltip_ClearLines},
	{"SetText",				Tooltip_SetText},	
	{"SetBagItem",			Tooltip_SetBagItem},
	{"SetBankItem",			Tooltip_SetBankItem},
	{"SetEquipmentItem",	Tooltip_SetEquipmentItem},
	{"SetSkillItem",		Tooltip_SetSkillItem},
	{"SetActionItem",		Tooltip_SetActionItem},
	{"SetStoreItem",		Tooltip_SetStoreItem},
	{"SetTradeTargetItem",	Tooltip_SetTradeTargetItem},
	{"SetInboxItem",		Tooltip_SetInboxItem},
	{"SetAuctionItem",		Tooltip_SetAuctionItem},
	{"SetAuctionBrowseItem",Tooltip_SetAuctionBrowseItem},
	{"SetAuctionBidItem",	Tooltip_SetAuctionBidItem},
	{"SetAuctionSellItem",	Tooltip_SetAuctionSellItem},
	{NULL, NULL}
};

// ----------------------------------------------------------------------------------
void CUiLuaTooltip::OpenLib(lua_State* L)
{
	luaL_newmetatable(L, UI_LUA_TYPE_TOOLTIP);			// 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_tooltipTable, 0);
}

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

// ----------------------------------------------------------------------------------
int Tooltip_AddLine(lua_State* L)
{
	CUiTooltip* tooltip = dynamic_cast<CUiTooltip*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (tooltip == NULL)
		lua_error(L);

	const char* text = luaL_checkstring(L, 2);
	float r = (float)luaL_checknumber(L, 3);
	float g = (float)luaL_checknumber(L, 4);
	float b = (float)luaL_checknumber(L, 5);
	tooltip->AddLine(text, r, g, b, lua_toboolean(L, 6));
	return 0;
}

// ----------------------------------------------------------------------------------
int Tooltip_AddDoubleLine(lua_State* L)
{
	CUiTooltip* tooltip = dynamic_cast<CUiTooltip*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (tooltip == NULL)
		lua_error(L);

	const char* textL = luaL_checkstring(L, 2);
	const char* textR = luaL_checkstring(L, 3);
	float rL = (float)luaL_checknumber(L, 4);
	float gL = (float)luaL_checknumber(L, 5);
	float bL = (float)luaL_checknumber(L, 6);
	float rR = (float)luaL_checknumber(L, 7);
	float gR = (float)luaL_checknumber(L, 8);
	float bR = (float)luaL_checknumber(L, 9);
	tooltip->AddDoubleLine(textL, textR, rL, gL, bL, rR, gR, bR);
	return 0;
}

// ----------------------------------------------------------------------------------
int Tooltip_ClearLines(lua_State* L)
{
	CUiTooltip* tooltip = dynamic_cast<CUiTooltip*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (tooltip == NULL)
		lua_error(L);

	tooltip->ClearLines();
	return 0;
}

// ----------------------------------------------------------------------------------
int Tooltip_SetText(lua_State* L)
{
	CUiTooltip* tooltip = dynamic_cast<CUiTooltip*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (tooltip == NULL)
		lua_error(L);

	const char* text = luaL_checkstring(L, 2);
	float r = (float)luaL_checknumber(L, 3);
	float g = (float)luaL_checknumber(L, 4);
	float b = (float)luaL_checknumber(L, 5);
	tooltip->SetText(text, r, g, b, lua_toboolean(L, 6));
	return 0;
}