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

#include "UI_QuestDetail.h"
#include "UI_QuestBook.h"

CUI_QuestBook*		g_pQuestBook	= NULL;
//-------------------------------------------------------------------
int			LuaFunc_GetNumQuestBookButton_QuestBook		( lua_State *L );
int			LuaFunc_GetQuestBookButtonStatus_QuestBook	( lua_State *L );
int			LuaFunc_ViewQuest_QuestBook					( lua_State *L );
/*
int			LuaFunc_OnLoad_QuestList		( lua_State *L );
int			LuaFunc_OnClick_QuestListButton	( lua_State *L );
int			LuaFunc_GetQuestListText		( lua_State *L );
int			LuaFunc_GetNumQuest				( lua_State *L );	
int			LuaFunc_GetQuestNameByIndex		( lua_State *L );	// 傳入 _Type, _Index 
*/
//-------------------------------------------------------------------
CUI_QuestBook::CUI_QuestBook( CInterface* object ) : CInterfaceBase( object )
{
	g_pQuestBook = this;
}
// --------------------------------------------------------------------------------------
CUI_QuestBook::~CUI_QuestBook(void)
{
	g_pQuestBook = NULL;
}
//--------------------------------------------------------------------------------------
void CUI_QuestBook::RegisterFunc(void)
{
	lua_State* l = GetLuaStateWorld();
	if (l)
	{
		lua_register( l, "GetNumQuestBookButton_QuestBook",			LuaFunc_GetNumQuestBookButton_QuestBook		);
		lua_register( l, "GetQuestBookButtonStatus_QuestBook",		LuaFunc_GetQuestBookButtonStatus_QuestBook	);
		lua_register( l, "ViewQuest_QuestBook",						LuaFunc_ViewQuest_QuestBook					);

		/*
		lua_register( l, "OnLoad_QuestList",			LuaFunc_OnLoad_QuestList		);
		lua_register( l, "OnClick_QuestListButton",		LuaFunc_OnClick_QuestListButton	);
		lua_register( l, "GetQuestListText",			LuaFunc_GetQuestListText		);
		lua_register( l, "GetNumQuest",					LuaFunc_GetNumQuest				);
		lua_register( l, "GetQuestNameByIndex",			LuaFunc_GetQuestNameByIndex		);
		*/
	}
}
//--------------------------------------------------------------------------------------
int	LuaFunc_GetNumQuestBookButton_QuestBook	( lua_State *L )
{
	if( g_pQuestBook )
	{
		g_pQuestBook->m_vecQuestID.clear();

		RoleDataEx*			pRole		= CNetGlobal::RoleData();

		// 計算任務
			int iTotalQuest = 0;
			for( int index = 0; index < _MAX_QUEST_COUNT_; index++ )
			{
				int iQuestID = pRole->BaseData.Quest.QuestLog[ index ].iQuestID;
				if( iQuestID != 0 )
				{
					g_pQuestBook->m_vecQuestID.push_back( iQuestID );
					iTotalQuest++;				
				}
			}

		lua_pushnumber( L, ( iTotalQuest + 1 ) );
		return 1;
	}

	return 0;
}
//--------------------------------------------------------------------------------------
int LuaFunc_GetQuestBookButtonStatus_QuestBook ( lua_State *L )
{
	if( g_pQuestBook )
	{
		int iIndex	= luaL_checkint( L, 1 ) - 1;

		if( iIndex == 0 )
		{
			lua_pushnumber( L, 0			);	// 分類夾
			lua_pushnumber( L, 1			);	// 打開狀態
			lua_pushstring( L, "尚未分類"	);

			return 3;
		}
		else
		{
			if( iIndex <= g_pQuestBook->m_vecQuestID.size() )
			{
				int					iQuestID	= g_pQuestBook->m_vecQuestID[ ( iIndex - 1 )];
				GameObjDbStruct*	pObj		= g_ObjectData->GetObj( iQuestID );

				if( pObj )
				{

					lua_pushnumber( L, 1			);	// 分類夾
					lua_pushnumber( L, 0			);	// 打開狀態
					lua_pushstring( L, pObj->Name	);

					return 3;
				}
			}
		}
	}
			
	return 0;
}
//--------------------------------------------------------------------------------------
int			LuaFunc_ViewQuest_QuestBook					( lua_State *L )
{
	if( g_pQuestBook )
	{
		int iIndex	= luaL_checkint( L, 1 ) - 1;

		if( iIndex > 0 )
		{
			int iQuestID = g_pQuestBook->m_vecQuestID[ ( iIndex - 1 ) ];
			g_pQuestDetail->SetQuest( 1, iQuestID );
		}
	}

	return 0;
}