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

int EditBox_SetFocus(lua_State* L);
int EditBox_ClearFocus(lua_State* L);
int EditBox_GetText(lua_State* L);
int EditBox_SetText(lua_State* L);
int EditBox_SetNumber(lua_State* L);
int EditBox_GetNumber(lua_State* L);
int EditBox_SetTextColor(lua_State* L);
int EditBox_SetTextInset(lua_State* L);
int EditBox_AddHistoryLine(lua_State* L);

// ----------------------------------------------------------------------------------
static const struct luaL_reg g_editboxTable [] = {
	{"SetFocus",		EditBox_SetFocus},
	{"ClearFocus",		EditBox_ClearFocus},
	{"GetText",			EditBox_GetText},
	{"SetText",			EditBox_SetText},
	{"SetNumber",		EditBox_SetNumber},
	{"GetNumber",		EditBox_GetNumber},
	{"SetTextColor",	EditBox_SetTextColor},
	{"SetTextInset",	EditBox_SetTextInset},
	{"AddHistoryLine",	EditBox_AddHistoryLine},
	{NULL, NULL}
};

// ----------------------------------------------------------------------------------
void CUiLuaEditBox::OpenLib(lua_State* L)
{
	luaL_newmetatable(L, UI_LUA_TYPE_EDITBOX);			// 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_editboxTable, 0);
}

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

// ----------------------------------------------------------------------------------
int EditBox_SetFocus(lua_State* L)
{
	CUiEditBox* editbox = dynamic_cast<CUiEditBox*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (editbox == NULL)
		lua_error(L);

	editbox->SetFocus();
	return 0;
}

// ----------------------------------------------------------------------------------
int EditBox_ClearFocus(lua_State* L)
{
	CUiEditBox* editbox = dynamic_cast<CUiEditBox*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (editbox == NULL)
		lua_error(L);

	editbox->ClearFocus();
	return 0;
}

// ----------------------------------------------------------------------------------
int EditBox_GetText(lua_State* L)
{
	CUiEditBox* editbox = dynamic_cast<CUiEditBox*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (editbox == NULL)
		lua_error(L);

	const WCHAR* text = editbox->GetText();
	if (text)
	{
		char* tmp = TransToChar(text);
		lua_pushstring(L, tmp);
		UI_SAFE_DELETE_ARRAY(tmp);
		return 1;
	}
	return 0;
}

// ----------------------------------------------------------------------------------
int EditBox_SetText(lua_State* L)
{
	CUiEditBox* editbox = dynamic_cast<CUiEditBox*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (editbox == NULL)
		lua_error(L);

	editbox->SetTextA( luaL_checkstring(L, 2) );
	return 0;
}

// ----------------------------------------------------------------------------------
int EditBox_SetNumber(lua_State* L)
{
	CUiEditBox* editbox = dynamic_cast<CUiEditBox*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (editbox == NULL)
		lua_error(L);
	
	editbox->SetTextA(lua_tostring(L, 2));
	return 0;

	/*
	char tmp[MAX_PATH];
	itoa(luaL_checkint(L, 2), tmp, MAX_PATH);
	editbox->SetTextA(tmp);
	return 0;
	*/
}

// ----------------------------------------------------------------------------------
int EditBox_GetNumber(lua_State* L)
{
	CUiEditBox* editbox = dynamic_cast<CUiEditBox*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (editbox == NULL)
		lua_error(L);

	const WCHAR* text = editbox->GetText();
	if ( text == NULL )
		text = L"";
	lua_pushnumber(L, _wtof(text));
	return 1;
}

// ----------------------------------------------------------------------------------
int EditBox_SetTextColor(lua_State* L)
{
	CUiEditBox* editbox = dynamic_cast<CUiEditBox*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (editbox == NULL)
		lua_error(L);

	editbox->SetTextColor( CUiLuaLayout::GetColor(L, 2) );
	return 0;
}

// ----------------------------------------------------------------------------------
int EditBox_SetTextInset(lua_State* L)
{
	CUiEditBox* editbox = dynamic_cast<CUiEditBox*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (editbox == NULL)
		lua_error(L);

	editbox->SetTextInset( CUiLuaLayout::GetRectangle(L, 2) );
	return 0;
}

// ----------------------------------------------------------------------------------
int EditBox_AddHistoryLine(lua_State* L)
{
	CUiEditBox* editbox = dynamic_cast<CUiEditBox*>( CUiLuaLayout::CheckLayoutTable(L, 1) );
	if (editbox == NULL)
		lua_error(L);

	editbox->AddHistoryLineA( luaL_checkstring(L, 2) );
	return 0;
}