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

int Frame_Enable(lua_State* L);
int Frame_Disable(lua_State* L);
int Frame_IsEnable(lua_State* L);
int Frame_SetID(lua_State* L);
int Frame_GetID(lua_State* L);
int	Frame_SetScale(lua_State* L);
int Frame_SetAlpha(lua_State* L);
int Frame_RegisterEvent(lua_State* L);
int Frame_UnregisterEvent(lua_State* L);
int Frame_RegisterForDrag(lua_State* L);
int Frame_RegisterForClicks(lua_State* L);
int Frame_SetBackdropTileColor(lua_State* L);
int Frame_SetBackdropEdgeColor(lua_State* L);
int Frame_StartMoving(lua_State* L);
int Frame_StartSizing(lua_State* L);
int Frame_StopMovingOrSizing(lua_State* L);

// ----------------------------------------------------------------------------------
static const struct luaL_reg g_frameTable [] = {
	{"Enable",					Frame_Enable},
	{"Disable",					Frame_Disable},
	{"IsEnable",				Frame_IsEnable},
	{"SetID",					Frame_SetID},
	{"GetID",					Frame_GetID},
	{"SetScale",				Frame_SetScale},
	{"SetAlpha",				Frame_SetAlpha},
	{"RegisterEvent",			Frame_RegisterEvent},
	{"UnregisterEvent",			Frame_UnregisterEvent},
	{"RegisterForDrag",			Frame_RegisterForDrag},
	{"RegisterForClicks",		Frame_RegisterForClicks},	
	{"SetBackdropTileColor",	Frame_SetBackdropTileColor},
	{"SetBackdropEdgeColor",	Frame_SetBackdropEdgeColor},
	{"StartMoving",				Frame_StartMoving},
	{"StartSizing",				Frame_StartSizing},
	{"StopMovingOrSizing",		Frame_StopMovingOrSizing},
	{NULL, NULL}
};

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

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

// ----------------------------------------------------------------------------------
int Frame_Enable(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	frame->SetEnable(true);
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_Disable(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	frame->SetEnable(false);
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_IsEnable(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	lua_pushboolean(L, frame->IsEnable());
	return 1;
}

// ----------------------------------------------------------------------------------
int Frame_SetID(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	frame->SetID(luaL_checkint(L, 2));
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_GetID(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	lua_pushnumber(L, frame->GetID());
	return 1;
}

// ----------------------------------------------------------------------------------
int	Frame_SetScale(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	frame->SetScale( (float)luaL_checknumber(L, 2) );
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_SetAlpha(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	frame->SetAlpha( (float)luaL_checknumber(L, 2) );
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_RegisterForDrag(lua_State* L)
{
	int size = lua_gettop(L);
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);
	
	int flag = 0;	
	for ( int i = 2; i <= size; i++ )
	{
		const char* key = luaL_checkstring(L, i);
		if (strcmp(key, "LeftButton") == 0)
			flag |= CUiFrame::eMouse_Flag_LButton;
		else if (strcmp(key, "RightButton") == 0)
			flag |= CUiFrame::eMouse_Flag_RButton;
		else if (strcmp(key, "MiddleButton") == 0)
			flag |= CUiFrame::eMouse_Flag_MButton;
		else if (strcmp(key, "OtherButton") == 0)
			flag |= CUiFrame::eMouse_Flag_XButton;
		else
			lua_error(L);
	}

	frame->RegisterForDrag(flag);
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_RegisterForClicks(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	int flag = 0;
	int size = lua_gettop(L);
	for ( int i = 2; i <= size; i++ )
	{
		const char* key = luaL_checkstring(L, i);
		if (strcmp(key, "LeftButton") == 0)
			flag |= CUiFrame::eMouse_Flag_LButton;
		else if (strcmp(key, "RightButton") == 0)
			flag |= CUiFrame::eMouse_Flag_RButton;
		else if (strcmp(key, "MiddleButton") == 0)
			flag |= CUiFrame::eMouse_Flag_MButton;
		else if (strcmp(key, "OtherButton") == 0)
			flag |= CUiFrame::eMouse_Flag_XButton;
		else
		{
			const char* name = frame->GetName();
			if (name == NULL) name = "";
			luaL_error(L, "[%s]RegisterForClicks argument %s is invalid", name, key);
		}
	}

	frame->RegisterForClicks(flag);
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_RegisterEvent(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	frame->RegisterEvent(luaL_checkstring(L, 2));
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_UnregisterEvent(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	frame->UnregisterEvent(luaL_checkstring(L, 2));
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_SetBackdropTileColor(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	CUiColor color = CUiLuaLayout::GetColor(L, 2);
	frame->SetBackdropTileColor( CUiRGBA(color.GetColor()) );
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_SetBackdropEdgeColor(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);

	CUiColor color = CUiLuaLayout::GetColor(L, 2);
	frame->SetBackdropEdgeColor( CUiRGBA(color.GetColor()) );
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_StartMoving(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);
	frame->StartMoving();
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_StartSizing(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);	
	frame->StartSizing( CUiAnchor::GetAnchorPoint(luaL_checkstring(L, 2)) );
	return 0;
}

// ----------------------------------------------------------------------------------
int Frame_StopMovingOrSizing(lua_State* L)
{
	CUiFrame* frame = dynamic_cast<CUiFrame*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (frame == NULL)
		lua_error(L);
	frame->StopMovingOrSizing();
	return 0;
}