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

CLootFrame*				g_pLootFrame = NULL;

int LuaFunc_RollOnLoot(lua_State* L);
int	LuaFunc_AssignOnLoot(lua_State* L);
int LuaFunc_GetLootRollItemInfo(lua_State* L);
int LuaFunc_GetLootAssignItemSize(lua_State* L);
int LuaFunc_GetLootAssignItemInfo(lua_State* L);
int LuaFunc_GetLootAssignMember(lua_State* L);
int LuaFunc_SetLootMethod(lua_State* L);
int LuaFunc_GetLootMethod(lua_State* L);
int LuaFunc_SetLootThreshold(lua_State* L);
int LuaFunc_GetLootThreshold(lua_State* L);

// ----------------------------------------------------------------------------
CLootFrame::CLootFrame(CInterface* object)
	: CInterfaceBase(object)
{
	g_pLootFrame = this;

	m_groupLootItem.empty();
}

// ----------------------------------------------------------------------------
CLootFrame::~CLootFrame()
{
	g_pLootFrame = NULL;
}

// ----------------------------------------------------------------------------
void CLootFrame::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register( L, "RollOnLoot",					LuaFunc_RollOnLoot );
		lua_register( L, "AssignOnLoot",				LuaFunc_AssignOnLoot );
		lua_register( L, "GetLootRollItemInfo",			LuaFunc_GetLootRollItemInfo );
		lua_register( L, "GetLootAssignItemSize",		LuaFunc_GetLootAssignItemSize );
		lua_register( L, "GetLootAssignItemInfo",		LuaFunc_GetLootAssignItemInfo );
		lua_register( L, "GetLootAssignMember",			LuaFunc_GetLootAssignMember );
		lua_register( L, "SetLootMethod",				LuaFunc_SetLootMethod );
		lua_register( L, "GetLootMethod",				LuaFunc_GetLootMethod );
		lua_register( L, "SetLootThreshold",			LuaFunc_SetLootThreshold );
		lua_register( L, "GetLootThreshold",			LuaFunc_GetLootThreshold );
	}
}

// ----------------------------------------------------------------------------
void CLootFrame::Update(float elapsedTime)
{
	// 檢查待處理項目
	vector<PartyRollItem>::iterator iter = NetCli_PartyChild::s_rollItem.begin();
	for ( ; iter != NetCli_PartyChild::s_rollItem.end(); iter++ )
	{
		PartyRollItem& item = *iter;
		switch ( item.status )
		{
		case eLootItem_Wait:
			if ( OpenNewLootItem( item.itemVersion ) )
			{
				item.status = eLootItem_Roll;
			}
			break;

		case eLootItem_Roll:
			break;

		case eLootItem_Done:
			break;
		}
	}
}

// ----------------------------------------------------------------------------
bool CLootFrame::OpenNewLootItem(int itemVersion)
{
	if ( m_groupLootItem.size() < NUM_GROUP_LOOT_ITEM )
	{
		lua_State* L = GetLuaStateWorld();
		if ( L )
		{
			/* the function name */
			lua_getglobal(L, "GroupLootFrame_OpenNewFrame");

			/* the argument */
			lua_pushnumber(L, itemVersion);

			/* call the function with 1 arguments, no return */
			if ( lua_pcall(L, 1, 0, 0) == 0 )
			{
				m_groupLootItem.insert(itemVersion);
				return true;
			}
			else
			{
				const char* err = lua_tostring(L, -1);
				lua_pop(L, 1);
			}
		}
	}
	return false;
}

// ----------------------------------------------------------------------------
void CLootFrame::DeleteLootItem(int itemVersion)
{
	set<int>::iterator iter = m_groupLootItem.find(itemVersion);
	if ( iter != m_groupLootItem.end() )
	{
		lua_State* L = m_interface->GetUiWorld()->GetLuaState();
		if ( L )
		{
			lua_pushnumber(L, itemVersion);
			SendWorldEvent(UiEvent_CancelLootRoll);
		}
		m_groupLootItem.erase(iter);
	}
}

// ----------------------------------------------------------------------------
void CLootFrame::RefreshAssignItem()
{
	SendWorldEvent(UIEvent_UpdateLootAssign);
}

// ----------------------------------------------------------------------------
int LuaFunc_RollOnLoot(lua_State* L)
{
	int itemVersion = luaL_checkint(L, 1);
	int type = 0;

	if ( lua_isstring(L, 2) )
	{
		const char* str = lua_tostring(L, 2);
		if ( strcmp(str, "roll") == 0 )				// 需求
			type = 1;
		else if ( strcmp(str, "greed") == 0 )		// 貪婪
			type = 2;
		else										// 放棄
			type = 0;
	}

	switch ( type )
	{
	case 0:
		NetCli_PartyChild::S_PassItem(itemVersion);
		break;

	case 1:
		NetCli_PartyChild::S_LootItem(itemVersion);
		break;

	case 2:
		NetCli_PartyChild::S_LootItem_NotNeed(itemVersion);
		break;
	}

	return 0;
}

// ----------------------------------------------------------------------------
int	LuaFunc_AssignOnLoot(lua_State* L)
{
	NetCli_PartyChild::AssignTo( luaL_checkint(L, 1), luaL_checkint(L, 2) - 1 );
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetLootRollItemInfo(lua_State* L)
{	
	PartyRollItem* loot = NetCli_PartyChild::GetRollItem(luaL_checkint(L, 1));
	if ( loot )
	{
		char tmp[MAX_PATH];
		RoleDataEx::GetItemFieldName(&loot->item, tmp, MAX_PATH);
		GameObjDbStruct* itemDB = CNetGlobal::GetObj(loot->item.OrgObjID);

		assert( itemDB != NULL );

		lua_pushstring(L, tmp);							// name
		lua_pushstring(L, itemDB->ImageObj->ACTField);	// image file
		lua_pushnumber(L, loot->item.Count);			// count
		lua_pushnumber(L, 0);							// quality
		return 4;
	}
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetLootAssignItemSize(lua_State* L)
{
	lua_pushnumber( L, NetCli_PartyChild::s_assignItem.size() );
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetLootAssignItemInfo(lua_State* L)
{	
	if ( NetCli_PartyChild::IsAssigner() )
	{
		int index = luaL_checkint(L, 1) - 1;
		if ( index >= 0 && index < (int)NetCli_PartyChild::s_assignItem.size() )
		{
			char tmp[MAX_PATH];
			PartyAssignItem& assign = NetCli_PartyChild::s_assignItem[index];
			RoleDataEx::GetItemFieldName(&assign.item, tmp, MAX_PATH);
			GameObjDbStruct* itemDB = CNetGlobal::GetObj(assign.item.OrgObjID);

			assert( itemDB != NULL );

			lua_pushnumber(L, assign.itemVersion);			// item version id
			lua_pushstring(L, tmp);							// name
			lua_pushstring(L, itemDB->ImageObj->ACTField);	// image file
			lua_pushnumber(L, assign.item.Count);			// count
			lua_pushnumber(L, 0);							// quality
			return 5;
		}
	}
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetLootAssignMember(lua_State* L)
{	
	lua_pushstring( L, NetCli_PartyChild::GetAssignItemMember(luaL_checkint(L, 1), luaL_checkint(L, 2) - 1) );
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_SetLootMethod(lua_State* L)
{
	const char* method = luaL_checkstring(L, 1);

	// 指定分配者
	if ( strcmp(method, "master") == 0 )
	{
		const char* name = NULL;
		if ( lua_isstring(L, 2) )
			name = lua_tostring(L, 2);
		NetCli_PartyChild::SetLootMethod( EM_PartyItemShare_Assign, name );
	}
	// 輪流取得
	else if ( strcmp(method, "roundrobin") == 0 )
	{
		NetCli_PartyChild::SetLootMethod( EM_PartyItemShare_Roll );
	}	
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetLootMethod(lua_State* L)
{
	PartyBaseInfoStruct& info = NetCli_PartyChild::GetPartyInfo();
	switch( info.ItemShareType )
	{
	case EM_PartyItemShare_Roll:
		lua_pushstring(L, "roundrobin");
		break;
	case EM_PartyItemShare_Assign:
		lua_pushstring(L, "master");
		break;
	default:
		lua_pushstring(L, "roundrobin");
		break;
	}
	return 1;
}

// ----------------------------------------------------------------------------
int LuaFunc_SetLootThreshold(lua_State* L)
{
	int quality = luaL_checkint(L, 1);
	NetCli_PartyChild::SetLootThreshold(quality);
	return 0;
}

// ----------------------------------------------------------------------------
int LuaFunc_GetLootThreshold(lua_State* L)
{
	PartyBaseInfoStruct& info = NetCli_PartyChild::GetPartyInfo();
	lua_pushnumber(L, info.RollTypeLV);
	return 1;
}