#include "../netwaker_member/NetWakerClientInc.h"
#include "../../mainproc/GameMain.h"
#include "UnitFrame.h"
#include "PartyFrame.h"

CPartyFrame*					g_pPartyFrame = NULL;

int	LuaFunc_InviteToParty		(lua_State* L);
int LuaFunc_InviteByName		(lua_State* L);
int	LuaFunc_UninviteFromParty	(lua_State* L);
int	LuaFunc_UninviteByName		(lua_State* L);
int	LuaFunc_PromoteToPartyLeader(lua_State* L);
int LuaFunc_LeaveParty			(lua_State* L);
int	LuaFunc_GetPartyMember		(lua_State* L);
int LuaFunc_GetPartyLeaderIndex	(lua_State* L);
int LuaFunc_IsPartyLeader		(lua_State* L);
int LuaFunc_GetNumPartyMembers	(lua_State* L);

int LuaFunc_AcceptGroup			(lua_State* L);
int LuaFunc_DeclineGroup		(lua_State* L);

// ----------------------------------------------------------------------------
CPartyFrame::CPartyFrame(CInterface* object)
	: CInterfaceBase(object)
{
	g_pPartyFrame = this;
}

// ----------------------------------------------------------------------------
CPartyFrame::~CPartyFrame()
{
	g_pPartyFrame = NULL;
}

// ----------------------------------------------------------------------------
void CPartyFrame::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register( L, "InviteToParty",			LuaFunc_InviteToParty );
		lua_register( L, "InviteByName",			LuaFunc_InviteByName );		
		lua_register( L, "UninviteFromParty",		LuaFunc_UninviteFromParty );
		lua_register( L, "UninviteByName",			LuaFunc_UninviteByName );
		lua_register( L, "PromoteToPartyLeader",	LuaFunc_PromoteToPartyLeader );
		lua_register( L, "LeaveParty",				LuaFunc_LeaveParty );
		lua_register( L, "GetPartyMember",			LuaFunc_GetPartyMember );
		lua_register( L, "GetPartyLeaderIndex",		LuaFunc_GetPartyLeaderIndex );
		lua_register( L, "IsPartyLeader",			LuaFunc_IsPartyLeader );
		lua_register( L, "GetNumPartyMembers",		LuaFunc_GetNumPartyMembers );

		lua_register( L, "AcceptGroup",				LuaFunc_AcceptGroup );
		lua_register( L, "DeclineGroup",			LuaFunc_DeclineGroup );
	}
}

// ----------------------------------------------------------------------------
void CPartyFrame::RefreshPartyMember()
{
	SendWorldEvent( UiEvent_PartyMemberChanged );
}

// ----------------------------------------------------------------------------
void CPartyFrame::RefreshPartyLeader()
{
	SendWorldEvent( UiEvent_PartyLeaderChanged );
}

// ----------------------------------------------------------------------------
int	LuaFunc_InviteToParty(lua_State* L)
{
	NetCli_PartyChild::InviteToParty( CUnitFrame::GetUnitName(luaL_checkstring(L, 1)) );
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_InviteByName(lua_State* L)
{
	NetCli_PartyChild::InviteToParty( luaL_checkstring(L, 1) );
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_UninviteFromParty(lua_State* L)
{
	NetCli_PartyChild::KickMember( CUnitFrame::GetUnitName(luaL_checkstring(L, 1)) );
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_UninviteByName(lua_State* L)
{
	NetCli_PartyChild::KickMember( luaL_checkstring(L, 1) );
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_PromoteToPartyLeader(lua_State* L)
{
	NetCli_PartyChild::PromoteToLeader( CUnitFrame::GetUnitName(luaL_checkstring(L, 1)) );
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_LeaveParty(lua_State* L)
{
	NetCli_PartyChild::KickMember( CUnitFrame::GetUnitName("player") );
	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_GetPartyMember(lua_State* L)
{
	PartyMemberInfo* member = NetCli_PartyChild::GetPartyMember( luaL_checkint(L, 1) - 1 );
	if ( member )
	{
		lua_pushstring( L, member->name.c_str() );
		return 1;
	}
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetPartyLeaderIndex(lua_State* L)
{
	lua_pushnumber(L, NetCli_PartyChild::GetPartyLeaderIndex() + 1);	
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_IsPartyLeader(lua_State* L)
{
	lua_pushboolean(L, NetCli_PartyChild::s_state == ePartyState_Leader);
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetNumPartyMembers(lua_State* L)
{
	int size = NetCli_PartyChild::s_member.size();
	if ( size > 0 )
		lua_pushnumber(L, size + 1);
	else
		lua_pushnumber(L, size);
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_AcceptGroup(lua_State* L)
{
	NetCli_PartyChild::InviteAnswerState(true);
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_DeclineGroup(lua_State* L)
{
	NetCli_PartyChild::InviteAnswerState(false);
	return 0;
}