#include "../../netwaker_member/NetWakerClientInc.h"
#include "CharacterSelect.h"

CCharacterSelect*		g_pCharacterSelect	= NULL;

int		LuaFunc_GetNumCharacters				(lua_State *L);
int		LuaFunc_GetCharacterInfo				(lua_State *L);
int		LuaFunc_GetSelectedItem					(lua_State *L);
int		LuaFunc_SelectCharacter					(lua_State *L);
int		LuaFunc_DeleteCharacter					(lua_State *L);
int		LuaFunc_EnterWorld						(lua_State *L);


// ----------------------------------------------------------------------------
CCharacterSelect::CCharacterSelect(CInterface* object) : CInterfaceBase(object)
{
	m_charInfo.clear();
	m_selected			= -1;
	g_pCharacterSelect	= this;
}

// ----------------------------------------------------------------------------
CCharacterSelect::~CCharacterSelect()
{
	g_pCharacterSelect = NULL;
}

// ----------------------------------------------------------------------------
void CCharacterSelect::RegisterFunc()
{
	lua_State* L = GetLuaStateLogin();
	if (L)
	{
		lua_register(L, "GetNumCharacters",		LuaFunc_GetNumCharacters);
		lua_register(L, "GetCharacterInfo",		LuaFunc_GetCharacterInfo);
		lua_register(L, "GetSelectedItem",		LuaFunc_GetSelectedItem);
		lua_register(L, "SelectCharacter",		LuaFunc_SelectCharacter);
		lua_register(L, "DeleteCharacter",		LuaFunc_DeleteCharacter);
		lua_register(L, "EnterWorld",			LuaFunc_EnterWorld);
	}
}

// --------------------------------------------------------------------------------------
void CCharacterSelect::Insert(int index, SampleRoleData* pRoleData)
{
	assert(pRoleData);
	pair<map<int, SampleRoleData>::iterator, bool> hr;
	hr = m_charInfo.insert(make_pair(index, *pRoleData));
	assert(hr.second);
}

// -------------------------------------------------------------------------------------- 
void CCharacterSelect::Delete(int index)
{
	map<int, SampleRoleData>::iterator iter = m_charInfo.find(index);
	if (iter != m_charInfo.end())
		m_charInfo.erase(iter);
}

// --------------------------------------------------------------------------------------
SampleRoleData* CCharacterSelect::GetCharInfo(int serial)
{
	if (serial >= 0)
	{
		for (map<int, SampleRoleData>::iterator iter = m_charInfo.begin(); iter != m_charInfo.end(); iter++, serial--)
		{
			if (serial == 0)
				return &(iter->second);
		}
	}
	return NULL;
}

// --------------------------------------------------------------------------------------
int CCharacterSelect::GetCharIndex(int serial)
{
	if (serial >= 0)
	{
		map<int, SampleRoleData>::iterator iter = m_charInfo.begin();
		for (; iter != m_charInfo.end(); iter++, serial--)
		{
			if (serial == 0)
				return iter->first;
		}
	}
	return -1;
}

// --------------------------------------------------------------------------------------
void CCharacterSelect::Clear()
{
	m_charInfo.clear();
	m_selected = -1;
}

// --------------------------------------------------------------------------------------
void CCharacterSelect::SetSelected(int item)
{
	item--;
	item = max(-1, item);
	item = min((int)m_charInfo.size()-1, item);

	if (m_selected != item)
	{
		m_selected = item;

		lua_State* L = m_interface->GetUiLogin()->GetLuaState();
		if ( L )
		{
			lua_pushnumber(L, GetSelected());
			lua_setglobal(L, "arg1");
			SendLoginEvent(UiEvent_UpdateCharacterSelected);
		}
	}
}

// --------------------------------------------------------------------------------------
void CCharacterSelect::SendCharacterListUpdate()
{
	lua_State* L = m_interface->GetUiLogin()->GetLuaState();
	if ( L )
	{
		lua_pushnumber(L, GetSelected());
		lua_setglobal(L, "arg1");
		SendLoginEvent(UiEvent_CharacterListUpdate);
	}
}

// --------------------------------------------------------------------------------------
int LuaFunc_GetNumCharacters(lua_State *L)
{
	lua_pushnumber(L, g_pCharacterSelect->GetSize());
	return 1;
}

// --------------------------------------------------------------------------------------
int LuaFunc_GetCharacterInfo(lua_State *L)
{
	SampleRoleData* pRoleData = NULL;
	int serial = luaL_checkint(L, 1) - 1;
	if ( (pRoleData = g_pCharacterSelect->GetCharInfo(serial)) == NULL )
		return 0;

	lua_pushstring(L, pRoleData->RoleName.Begin());			// Name
	lua_pushnumber(L, pRoleData->Race);						// Race
	lua_pushnumber(L, pRoleData->Sex);						// Sex
	lua_pushnumber(L, pRoleData->Level);					// Level
	lua_pushstring(L, pRoleData->ZoneName.Begin());			// Zone Name
	return 5;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetSelectedItem(lua_State *L)
{
	lua_pushnumber(L, g_pCharacterSelect->GetSelected());
	return 1;	
}

// ----------------------------------------------------------------------------
int LuaFunc_SelectCharacter(lua_State *L)
{
	int serial = luaL_checkint(L, 1);
	g_pCharacterSelect->SetSelected(serial);
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_DeleteCharacter(lua_State *L)
{
	int index = g_pCharacterSelect->GetCharIndex(luaL_checkint(L, 1) - 1);
	CNetCli_Login::DeleteRole(index);	
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_EnterWorld(lua_State *L)
{
	int index = g_pCharacterSelect->GetCharIndex(g_pCharacterSelect->GetSelected() - 1);	
	CNetCli_Login::SelectRole(index);
	return 0;
}