#include "../../mainproc/GameMain.h"					// For Obj and Role struct
#include "../../netwaker_member/NetWakerClientInc.h"	// For NetGlobal

#include "UI_QuestList.h"
#include "UI_QuestDetail.h"

CUI_QuestDetail*		g_pQuestDetail	= NULL;
//-------------------------------------------------------------------
int			LuaFunc_GetQuestName_QuestDetail			( lua_State *L );
int			LuaFunc_GetQuestDesc_QuestDetail			( lua_State *L );
int			LuaFunc_GetQuestDetail_QuestDetail			( lua_State *L );
int			LuaFunc_GetQuestItemNumByType_QuestDetail	( lua_State *L );
int			LuaFunc_GetQuestItemInfo_QuestDetail		( lua_State *L );
int			LuaFunc_OnClick_ProcessQuest_QuestDetail	( lua_State *L );
int			LuaFunc_IsQuestComplete_QuestDetail			( lua_State *L );
int			LuaFunc_GetQuestStatus_QuestDetail			( lua_State *L );
int			LuaFunc_SetChoiceItem_QuestDetail			( lua_State *L );
int			LuaFunc_GetChoiceItem_QuestDetail			( lua_State *L );

//-------------------------------------------------------------------
CUI_QuestDetail::CUI_QuestDetail( CInterface* object ) : CInterfaceBase( object )
{
	g_pQuestDetail	= this;	
	m_iQuestID		= 0;
}
// --------------------------------------------------------------------------------------
CUI_QuestDetail::~CUI_QuestDetail(void)
{
	g_pQuestDetail = NULL;
}
//--------------------------------------------------------------------------------------
void CUI_QuestDetail::RegisterFunc(void)
{
	lua_State* l = GetLuaStateWorld();
	if (l)
	{
		lua_register( l, "GetQuestName_QuestDetail",			LuaFunc_GetQuestName_QuestDetail			);
		lua_register( l, "GetQuestDesc_QuestDetail",			LuaFunc_GetQuestDesc_QuestDetail			);
		lua_register( l, "GetQuestDetail_QuestDetail",			LuaFunc_GetQuestDetail_QuestDetail			);
		lua_register( l, "GetQuestItemNumByType_QuestDetail",	LuaFunc_GetQuestItemNumByType_QuestDetail	);
		lua_register( l, "GetQuestItemInfo_QuestDetail",		LuaFunc_GetQuestItemInfo_QuestDetail		);
		lua_register( l, "OnClick_ProcessQuest_QuestDetail",	LuaFunc_OnClick_ProcessQuest_QuestDetail	);
		lua_register( l, "IsQuestComplete_QuestDetail",			LuaFunc_IsQuestComplete_QuestDetail			);
		lua_register( l, "GetQuestStatus_QuestDetail",			LuaFunc_GetQuestStatus_QuestDetail			);
		lua_register( l, "SetChoiceItem_QuestDetail",			LuaFunc_SetChoiceItem_QuestDetail			);
		lua_register( l, "GetChoiceItem_QuestDetail",			LuaFunc_GetChoiceItem_QuestDetail			);
	}
}
//--------------------------------------------------------------------------------------
void CUI_QuestDetail::SetQuest		( int iMode, int iQuestID )
{ 
	GameObjDbStruct*	pObj	= NULL;
	if( g_pQuestDetail )
	{
		GameObjDbStruct*	pObj		= g_ObjectData->GetObj( iQuestID );

		if( pObj )
		{
			// 重置狀態
				m_vecQuestItem[0].clear();
				m_vecQuestItem[1].clear();
				m_vecQuestItem[2].clear();
				
				m_iRewardItemChoiceID	= -1;

			// 設定資料
				RoleDataEx*			pRole		= CNetGlobal::RoleData();
								
				m_iMode			= iMode;
				m_iQuestID		= iQuestID;
				m_sName			= pObj->Name;
				
				GameObjectQuestDetail	*pQuest		= &( pObj->QuestDetail );

			// 設定物品資料

				// 需求物品
					for( int i = 0; i < _MAX_SIZE_QUEST_ITEM; i++ )
					{
						int iItemID = pQuest->iRequest_ItemID[ i ] ;
						if( iItemID != 0 )
						{
							StructQuestItem		item;
							GameObjDbStructEx*	pItemObj	= g_ObjectData->GetObj( iItemID );

							if( pItemObj && pItemObj->IsItem() && pItemObj->IsKeyItem() )
							{
								if( pItemObj->IsKeyItem() && pItemObj->KeyItem.Type == 0 )
									continue; // 物品隱藏

								item.iItemID	= pQuest->iRequest_ItemID[ i ];
								item.iItemVal	= pQuest->iRequest_ItemVal[ i ];
								item.sIconPath	= pItemObj->ImageObj->ACTField;
								item.sName		= pItemObj->Name;

								m_vecQuestItem[0].push_back( item );
								
							}
						}
					}

				// 獎勵物品
					for( int i = 0; i < _MAX_SIZE_QUEST_ITEM; i++ )
					{
						int iItemID = pQuest->iReward_ItemID[ i ] ;
						if( iItemID != 0 )
						{
							StructQuestItem		item;
							GameObjDbStruct*	pItemObj	= g_ObjectData->GetObj( iItemID );

							if( pItemObj )
							{							
								item.iItemID	= pQuest->iReward_ItemID[ i ];
								item.iItemVal	= pQuest->iReward_ItemVal[ i ];
								item.sIconPath	= pItemObj->ImageObj->ACTField;
								item.sName		= pItemObj->Name;

								m_vecQuestItem[1].push_back( item );
							}
						}
					}

					// 獎勵選則物品
					for( int i = 0; i < _MAX_SIZE_QUEST_ITEM; i++ )
					{
						int iItemID = pQuest->iReward_ChoiceID[ i ] ;
						if( iItemID != 0 )
						{
							StructQuestItem		item;
							GameObjDbStruct*	pItemObj	= g_ObjectData->GetObj( iItemID );

							if( pItemObj )
							{							
								item.iItemID	= pQuest->iReward_ChoiceID[ i ];
								item.iItemVal	= pQuest->iReward_ChoiceVal[ i ];
								item.sIconPath	= pItemObj->ImageObj->ACTField;
								item.sName		= pItemObj->Name;

								m_vecQuestItem[2].push_back( item );
							}
						}
					}
				
			//m_isComplete	= pRole->CheckQuest_IsComplete( 

			if( iMode == 0 )
			{
				// From NPC

				int						iQuestField = pRole->CheckQuestList_IsGotQuest( iQuestID );
				
				if( iQuestField != -1 )
				{
					// 檢查是否已完成該 Quest
					if( pRole->CheckQuest_IsComplete( iQuestField, iQuestID, pQuest ) == EM_QUESTCHECK_RESULT_OK )
					{
						// 已完成
						m_sDetail	= pQuest->szQuest_Complete_Detail;
						m_iStatus	= 2;
					}
					else
					{
						m_iStatus	= 1;

						if( strlen( pQuest->szQuest_UnComplete_Detail ) != 0 )
						{							
							m_sDetail	= pQuest->szQuest_UnComplete_Detail;	// 未完成							
						}
						else
						{
							m_sDetail	= pQuest->szQuest_Complete_Detail;
						}
					}
				}
				else
				{
					// 待接
					m_sDesc		= pQuest->szQuest_Desc;
					m_sDetail	= pQuest->szQuest_Accept_Detail;
					m_iStatus	= 0;
				}

				g_pGameMain->GetInterface()->GetUiWorld()->SendEvent( UiEvent_ShowQuestDetailFromNPC );
			}
			else
			{
				// From Book

				m_sDesc		= pQuest->szQuest_Desc;
				m_sDetail	= pQuest->szQuest_Accept_Detail;

				g_pGameMain->GetInterface()->GetUiWorld()->SendEvent( UiEvent_ShowQuestDetailFromBook );
			}
		}
	}

	
}
//--------------------------------------------------------------------------------------
int			LuaFunc_GetQuestItemNumByType_QuestDetail	( lua_State *L )
{
	if( g_pQuestDetail )
	{
		int iType	= luaL_checkint( L, 1 ) - 1;

		if( iType >= 0 && iType <= 2 )
		{
			int iNumItem = g_pQuestDetail->m_vecQuestItem[ iType ].size();
			lua_pushnumber( L, iNumItem );
			return 1;
		}
	}
	return 0;
}
//--------------------------------------------------------------------------------------
int			LuaFunc_GetQuestItemInfo_QuestDetail	( lua_State *L )
{
	if( g_pQuestDetail )
	{
		int iType	= luaL_checkint( L, 1 ) - 1;
		int iIndex	= luaL_checkint( L, 2 ) - 1;

		if( iType >= 0 && iType <= 2 )
		{
			int iNumItem = g_pQuestDetail->m_vecQuestItem[ iType ].size();

			if( iIndex < iNumItem )
			{
				CUI_QuestDetail::StructQuestItem&	item		= g_pQuestDetail->m_vecQuestItem[ iType ][ iIndex ];

				lua_pushstring( L, item.sName.c_str() );
				lua_pushstring( L, item.sIconPath.c_str() );
				return 2;
			}
		}
	}	
	return 0;
}
//--------------------------------------------------------------------------------------
int			LuaFunc_GetQuestName_QuestDetail			( lua_State *L )
{
	if( g_pQuestDetail )
	{
		lua_pushstring( L, g_pQuestDetail->m_sName.c_str() );
		return 1;
	}

	return 0;
}
//--------------------------------------------------------------------------------------
int			LuaFunc_GetQuestDesc_QuestDetail			( lua_State *L )
{
	if( g_pQuestDetail )
	{
		lua_pushstring( L, g_pQuestDetail->m_sDesc.c_str() );
		return 1;
	}
	
	return 0;
}
//--------------------------------------------------------------------------------------
int			LuaFunc_GetQuestDetail_QuestDetail			( lua_State *L )
{
	if( g_pQuestDetail )
	{
		lua_pushstring( L, g_pQuestDetail->m_sDetail.c_str() );
		return 1;
	}
	return 1;
}
//--------------------------------------------------------------------------------------
int			LuaFunc_OnClick_ProcessQuest_QuestDetail	( lua_State *L )
{
	if( g_pQuestDetail )
	{
		if( g_pQuestDetail->m_iMode == 0 )
		{
			// From NPC
			if( g_pQuestDetail->m_iStatus == 0 )
				CNetCli_Script::AcceptQuest( g_pQuestList->GetQuestNPC(), g_pQuestDetail->m_iQuestID ) ;
			else
			{
				if( g_pQuestDetail->m_iRewardItemChoiceID != -1 )
					CNetCli_Script::CompleteQuest( g_pQuestList->GetQuestNPC(), g_pQuestDetail->m_iQuestID, g_pQuestDetail->m_iRewardItemChoiceID );
				else
				{
					lua_pushnumber( L, -1 );
					return -1;
				}
			}
		}
		else
		{
			// From Book
			// Search Role Struct for field
				RoleDataEx*		pRole	= CNetGlobal::RoleData();
				int				iField	= pRole->CheckQuestList_IsGotQuest( g_pQuestDetail->m_iQuestID );
			
				CNetCli_Script::DeleteQuest( iField, g_pQuestDetail->m_iQuestID );
		}
		lua_pushnumber( L, 1 );
		return 1;
	}
	
	return 0;
}
//--------------------------------------------------------------------------------------
int	LuaFunc_IsQuestComplete_QuestDetail	( lua_State *L )
{
	if( g_pQuestDetail )
	{





	}

	return 0;
}
//--------------------------------------------------------------------------------------
int	LuaFunc_GetQuestStatus_QuestDetail ( lua_State *L )
{
	if( g_pQuestDetail )
	{
		lua_pushnumber( L, g_pQuestDetail->m_iStatus );
		return 1;
	}
	return 0;
}
//--------------------------------------------------------------------------------------
int LuaFunc_SetChoiceItem_QuestDetail ( lua_State *L )
{
	if( g_pQuestDetail )
	{
		int iIndex		= luaL_checkint( L, 1 ) - 1;
		int	iNumItem	= g_pQuestDetail->m_vecQuestItem[2].size();

		if( iNumItem > iIndex  )
		{
			g_pQuestDetail->m_iRewardItemChoiceID = iIndex;
			return iIndex;
		}
	}
	return -1;
}
//--------------------------------------------------------------------------------------
int LuaFunc_GetChoiceItem_QuestDetail ( lua_State *L )
{
	if( g_pQuestDetail )
	{
		lua_pushnumber( L, g_pQuestDetail->m_iRewardItemChoiceID );
		return 1;
	}
	return -1;
}
//--------------------------------------------------------------------------------------