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

#include "../WorldFrames/ItemClipboard.h"
#include "./UI_SkillBook.h"

#define DF_SKILLLIST_MAXSPELL		5

// 全域宣告
//-------------------------------------------------------------------
CUI_SkillBook*		g_pUi_SkillBook						= NULL;
//-------------------------------------------------------------------
int			LuaFunc_GetNumSkill_SkillBook				( lua_State *L );
int			LuaFunc_GetSkillPoint_SkillBook				( lua_State *L );
int			LuaFunc_GetSkillDetail_SkillBook			( lua_State *L );
int			LuaFunc_SkillUp_SkillBook					( lua_State *L );
int			LuaFunc_UseSkill_SkillBook					( lua_State *L );
int			LuaFunc_OpenSkillList_SkillBook				( lua_State *L );
int			LuaFunc_GetSkillInfo_SkillList				( lua_State *L );
int			LuaFunc_GetNumMaxSkillInfo_SkillList		( lua_State *L );
int			LuaFunc_GetSkillIndex_SkillBook				( lua_State *L );
int			LuaFunc_OnDrag_SkillButton					( lua_State *L );

//-------------------------------------------------------------------
CUI_SkillBook::CUI_SkillBook( CInterface* object ) : CInterfaceBase( object )
{
	g_pUi_SkillBook		= this;
	m_iLastSkillIndex	= -1;
	m_iSpellingGUID		= 0;
}
// --------------------------------------------------------------------------------------
CUI_SkillBook::~CUI_SkillBook(void)
{
	g_pUi_SkillBook	= NULL;
}
// --------------------------------------------------------------------------------------
void CUI_SkillBook::RegisterFunc(void)
{
	lua_State* l = GetLuaStateWorld();
	if (l)
	{
		lua_register( l, "GetNumSkill_SkillBook",			LuaFunc_GetNumSkill_SkillBook		);
		lua_register( l, "GetSkillPoint_SkillBook",			LuaFunc_GetSkillPoint_SkillBook		);
		lua_register( l, "GetSkillDetail_SkillBook",		LuaFunc_GetSkillDetail_SkillBook	);
		lua_register( l, "SkillUp_SkillBook",				LuaFunc_SkillUp_SkillBook			);
		lua_register( l, "UseSkill_SkillBook",				LuaFunc_UseSkill_SkillBook			);
		lua_register( l, "OpenSkillList_SkillBook",			LuaFunc_OpenSkillList_SkillBook		);
		lua_register( l, "GetSkillInfo_SkillList",			LuaFunc_GetSkillInfo_SkillList		);
		lua_register( l, "GetNumMaxSkillInfo_SkillList",	LuaFunc_GetNumMaxSkillInfo_SkillList);		
		lua_register( l, "GetSkillIndex_SkillBook",			LuaFunc_GetSkillIndex_SkillBook		);
		lua_register( l, "OnDrag_SkillButton",				LuaFunc_OnDrag_SkillButton			);
	}
}
// --------------------------------------------------------------------------------------
void CUI_SkillBook::SetArea ( float x , float y , float z )
{
	if( m_iSpellingGUID != 0 )
	{
		int					iPlayerID		= g_pGameMain->GetPlayerID();

		NetCli_Magic::BeginSpell_Pos( iPlayerID, x, y, z, m_iSpellingGUID, m_iSpellingIndex );

		m_iSpellingGUID		= 0;
		m_iSpellingIndex	= 0;
	}
}
// --------------------------------------------------------------------------------------
void CUI_SkillBook::CancleSpelling ()
{
	m_iSpellingGUID		= 0;
	m_iSpellingIndex	= 0;
}
// --------------------------------------------------------------------------------------
EM_SKILLBOOK_RESULT CUI_SkillBook::Use	( int iIndex, int iLevel )
{
	// 檢查看看
		if( m_iSpellingGUID != 0 )			
			return EM_SKILLBOOK_CASTING;


	// clear item clipboard data
		g_pItemClipboard->Clear();

	RoleDataEx*			pRole 			= CNetGlobal::RoleData();
	SkillBaseStruct*	pSkillStruct	= NULL;
	GameObjDbStruct*	pBaseSkillObj	= NULL;
	int					iMaxSkill		= _MAX_SPSkill_COUNT_+_MAX_NormalSkill_COUNT_+_MAX_NormalSkill_COUNT_;
	//int					iLevel			= -1;

	if( iIndex >= 0 && iIndex < iMaxSkill )
	{
		pSkillStruct = &( pRole->BaseData.Skill.AllMagic[ iIndex ] );
		
		if( pSkillStruct->SkillID != 0 && pSkillStruct->Point != 0 )
		{
			pBaseSkillObj	= g_ObjectData->GetObj( pSkillStruct->SkillID );

			if( iLevel < pSkillStruct->Point && iLevel >= 0 )
			{
				GameObjDbStruct*	pSkillObj		= pBaseSkillObj->AllMagicCol[ iLevel ];

				if( pSkillObj->MagicCol.TargetType == EM_Releation_Locatoin )
				{
					m_iSpellingGUID		= pSkillObj->GUID;
					m_iSpellingIndex	= iIndex;

					return EM_SKILLBOOK_SETAREA;
				}
				else
				{
					int					iTargetID		= 0;
					int					iPlayerID		= g_pGameMain->GetPlayerID();

					CRoleSprite*		pTarget			= g_pGameMain->GetTargetPlayer();


					if( pTarget )
					{
						iTargetID = pTarget->GetIdentify();
					}
					else
					{
						// 沒目標
						return EM_SKILLBOOk_NOTARGET;
					}

					if( pSkillObj )
					{
						NetCli_Magic::BeginSpell( iPlayerID, iTargetID, pSkillObj->GUID, iIndex );
						return EM_SKILLBOOK_OK;
					}
				}
			}
		}	
	}

	return EM_SKILLBOOK_NOSPELL;
}
// --------------------------------------------------------------------------------------
int	CUI_SkillBook::GetSkillGUID	( int iIndex, int iLevel )
{
	RoleDataEx*			pRole 			= CNetGlobal::RoleData();
	SkillBaseStruct*	pSkillStruct	= NULL;
	GameObjDbStruct*	pBaseSkillObj	= NULL;
	int					iMaxSkill		= _MAX_SPSkill_COUNT_+_MAX_NormalSkill_COUNT_+_MAX_NormalSkill_COUNT_;
	//int					iLevel			= -1;

	if( iIndex >= 0 && iIndex < iMaxSkill )
	{
		pSkillStruct = &( pRole->BaseData.Skill.AllMagic[ iIndex ] );

		if( pSkillStruct->SkillID != 0 && pSkillStruct->Point != 0 )
		{
			pBaseSkillObj	= g_ObjectData->GetObj( pSkillStruct->SkillID );

			if( iLevel < pSkillStruct->Point && iLevel >= 0 )
			{
				GameObjDbStruct*	pSkillObj		= pBaseSkillObj->AllMagicCol[ iLevel ];
				return pSkillObj->GUID;
			}
		}
	}

	return -1;
}
// --------------------------------------------------------------------------------------
int			LuaFunc_GetNumSkill_SkillBook				( lua_State *L )
{

	RoleDataEx*			pRole		= CNetGlobal::RoleData();

	int iCount = 0;
	g_pUi_SkillBook->m_vecMainJobAvailableSpell.clear();

	for( int i = 0; i < _MAX_NormalSkill_COUNT_; i++ )
	{
		SkillBaseStruct* pSkillStruct	= &( pRole->BaseData.Skill.MainJob[ i ] );

		if( pSkillStruct->SkillID != 0 )
		{
			SkillBaseStruct*	pSkillStruct	= &( pRole->BaseData.Skill.MainJob[ i ] );
			
			if( pRole->CheckSetMagicPoint( i ) == true || pSkillStruct->Point != 0 )
			{		
				g_pUi_SkillBook->m_vecMainJobAvailableSpell.push_back( i );
				iCount++;
			}
		}
	}

	lua_pushnumber( L, iCount );
	return 1;
}
// --------------------------------------------------------------------------------------
int			LuaFunc_GetSkillPoint_SkillBook				( lua_State *L )
{
	RoleDataEx*		pRole	= CNetGlobal::RoleData();
	int				iPoint	= pRole->TempData.Attr.Ability->SkillPoint;

	lua_pushnumber( L, iPoint );

	return 1;
}
// --------------------------------------------------------------------------------------
int			LuaFunc_GetSkillDetail_SkillBook				( lua_State *L )
{
	RoleDataEx*			pRole 		= CNetGlobal::RoleData();

	int iType	= luaL_checkint( L, 1 ) - 1;
	int iIndex	= luaL_checkint( L, 2 ) - 1;

	/*
	if( iIndex >= 0 && iIndex < _MAX_NormalSkill_COUNT_ )
	{
		SkillBaseStruct*	pSkillStruct	= &( pRole->BaseData.Skill.MainJob[ iIndex ] );
		
		if( pSkillStruct->SkillID != 0 )
		{
			GameObjDbStruct*	pBaseSkillObj	= g_ObjectData->GetObj( pSkillStruct->SkillID );

			int					iSkillLevel		= pSkillStruct->Point;
			GameObjDbStruct*	pSKillObj		= NULL;

			if( iSkillLevel != 0 )
			{
				pSKillObj		= pBaseSkillObj->AllMagic[ ( iSkillLevel-1) ];			
							
				lua_pushstring( L, pSKillObj->Name );	// NAME			
				lua_pushnumber( L, iSkillLevel );	// LV
			}
			else
			{
				pSKillObj		= pBaseSkillObj;
							
				lua_pushstring( L, pSKillObj->Name );	// NAME
				lua_pushstring( L, "" );				// LV
			}

			
			lua_pushstring( L, pSKillObj->ImageObj->ACTField );		// ICON
			lua_pushnumber( L, 1 );									// POINT REQUIRE
			lua_pushnumber( L, iSkillLevel );						// LV
			lua_pushnumber( L, pBaseSkillObj->MaxMagicCount );		// MAX LV
		
			return 6;
		}
	}
	*-/
/*
	int iSize = g_pUi_SkillBook->m_vecMainJobAvailableSpell.size();

	if( iIndex >= 0 && iIndex < iSize )
	{
		int					iSkillIndex		= g_pUi_SkillBook->m_vecMainJobAvailableSpell[ iIndex ];
		SkillBaseStruct*	pSkillStruct	= &( pRole->BaseData.Skill.MainJob[ iSkillIndex ] );
		
		if( pSkillStruct->SkillID != 0 )
		{
			GameObjDbStruct*	pBaseSkillObj	= g_ObjectData->GetObj( pSkillStruct->SkillID );

			int					iSkillLevel		= pSkillStruct->Point;
			GameObjDbStruct*	pSKillObj		= NULL;

			if( iSkillLevel != 0 )
			{
				pSKillObj		= pBaseSkillObj->AllMagicCol[ ( iSkillLevel-1) ];			
							
				lua_pushstring( L, pSKillObj->Name );	// NAME			
				lua_pushnumber( L, iSkillLevel );	// LV
			}
			else
			{
				pSKillObj		= pBaseSkillObj;
							
				lua_pushstring( L, pSKillObj->Name );	// NAME
				lua_pushstring( L, "" );				// LV
			}

			
			lua_pushstring( L, pSKillObj->ImageObj->ACTField );		// ICON
			lua_pushnumber( L, 1 );									// POINT REQUIRE
			lua_pushnumber( L, iSkillLevel );						// LV
			lua_pushnumber( L, pBaseSkillObj->MaxMagicColCount );		// MAX LV
		
			return 6;
		}
	}
*-/
	return 0;
}
// --------------------------------------------------------------------------------------
int	LuaFunc_SkillUp_SkillBook ( lua_State *L )
{
	int iIndex	= luaL_checkint( L, 1 ) - 1;
	
	if( iIndex >= 0 && iIndex < _MAX_NormalSkill_COUNT_ )
	{
		RoleDataEx*			pRole 			= CNetGlobal::RoleData();
		SkillBaseStruct*	pSkillStruct	= &( pRole->BaseData.Skill.MainJob[ iIndex ] );
		GameObjDbStruct*	pBaseSkillObj	= g_ObjectData->GetObj( pSkillStruct->SkillID );
		
		if( pBaseSkillObj )
		{
			NetCli_Magic::SetMagicPoint( pSkillStruct->SkillID, iIndex );
		}
	}

	return 0;
}
// --------------------------------------------------------------------------------------
int	LuaFunc_UseSkill_SkillBook ( lua_State *L )
{	
	int iType	= luaL_checkint( L, 1 );
	int iIndex	= luaL_checkint( L, 2 ) - 1;

	RoleDataEx*			pRole 			= CNetGlobal::RoleData();
	SkillBaseStruct*	pSkillStruct	= NULL;
	GameObjDbStruct*	pBaseSkillObj	= NULL;
	int					iSkillIndex		= -1;
	int					iLevel			= -1;

	if( iType == 1 )
	{
		iSkillIndex = iIndex;
		if( iSkillIndex >= 0 && iSkillIndex < _MAX_NormalSkill_COUNT_ )
		{			
			pSkillStruct	= &( pRole->BaseData.Skill.MainJob[ iIndex ] );
			iLevel			= pSkillStruct->Point - 1;
		}
	}
	else
	{
		if( iType == 2 )
		{
			iSkillIndex = g_pUi_SkillBook->m_iLastSkillIndex;
			if( iSkillIndex >= 0 && iSkillIndex  < _MAX_NormalSkill_COUNT_ )
			{
				iLevel			= iIndex;
			}
		}
	}

	if( iLevel != -1 )
	{
		//GameObjDbStruct*	pSkillObj		= pBaseSkillObj->AllMagic[ iLevel ];

		//int					iTargetID		= 0;
		//int					iPlayerID		= g_pGameMain->GetPlayerID();

		//CRoleSprite*		pTarget			= g_pGameMain->GetTargetPlayer();

		if( g_pUi_SkillBook )
			g_pUi_SkillBook->Use( ( iSkillIndex + _MAX_SPSkill_COUNT_ ), iLevel );

		/*
		if( pTarget )
		{
		iTargetID = pTarget->GetIdentify();
		}
		else
		{
		// 沒目標
		return 0;
		}


		if( pBaseSkillObj )
		{
		//NetCli_Magic::SetMagicPoint( pSkillStruct->SkillID, iInde );
		g_pUi_SkillBook->Use( ( iIndex + _MAX_SPSkill_COUNT_ ), iLevel );

		// 偏移以便 Server 計算
		//NetCli_Magic::BeginSpell( iPlayerID, iTargetID, pSkillObj->GUID, ( iIndex + _MAX_SPSkill_COUNT_ ) );
		}
		*-/
	}


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

	if( iIndex >= 0 && iIndex < _MAX_NormalSkill_COUNT_ )
	{
		if( g_pUi_SkillBook )
		{
				RoleDataEx*			pRole 			= CNetGlobal::RoleData();
				SkillBaseStruct*	pSkillStruct	= &( pRole->BaseData.Skill.MainJob[ iIndex ] );
				//GameObjDbStruct*	pBaseSkillObj	= g_ObjectData->GetObj( pSkillStruct->SkillID );

				if( pSkillStruct->SkillID != 0 )
				{
					// 記錄開啟的技能
					g_pUi_SkillBook->m_iLastSkillIndex	= iIndex;
					g_pUi_SkillBook->m_iMaxSkillList	= pSkillStruct->Point;

					if( pSkillStruct->Point > 1 )
					{
						CUiFrame* frame = (CUiFrame*) g_pGameMain->GetInterface()->GetUiWorld()->FindObject( "UI_SkillList" );
						if (frame)
							frame->SetVisible(!frame->IsVisible());
					}


				}
		}
	}

	return 0;
}
// --------------------------------------------------------------------------------------
int	LuaFunc_GetSkillInfo_SkillList				( lua_State *L )
{
	int iSkillIndex	= luaL_checkint( L, 1 ) - 1;
	int iIndex		= g_pUi_SkillBook->m_iLastSkillIndex;

	int iCount	= 0;

	RoleDataEx*			pRole 			= CNetGlobal::RoleData();
	SkillBaseStruct*	pSkillStruct	= &( pRole->BaseData.Skill.MainJob[ iIndex ] );
	

	if( iSkillIndex >= 0 && iSkillIndex < pSkillStruct->Point )
	{
		GameObjDbStruct*	pBaseSkillObj= g_ObjectData->GetObj( pSkillStruct->SkillID );
		GameObjDbStruct*	pSkillObj		= pBaseSkillObj->AllMagicCol[ iSkillIndex ];

		if( pSkillObj )
		{
			lua_pushstring( L, pSkillObj->Name );					// NAME
			lua_pushstring( L, pSkillObj->ImageObj->ACTField );		// ICON
			lua_pushnumber( L, pSkillObj->MagicCol.MagicLV );			// LV

			return 3;

		}
	}

	return 0;
}
// --------------------------------------------------------------------------------------
int			LuaFunc_GetNumMaxSkillInfo_SkillList		( lua_State *L )
{
	int iIndex		= g_pUi_SkillBook->m_iLastSkillIndex;

	if( iIndex >= 0 && iIndex < _MAX_NormalSkill_COUNT_ )
	{

		RoleDataEx*			pRole 			= CNetGlobal::RoleData();
		SkillBaseStruct*	pSkillStruct	= &( pRole->BaseData.Skill.MainJob[ iIndex ] );
		//GameObjDbStruct*	pBaseSkillObj	= g_ObjectData->GetObj( pSkillStruct->SkillID );

		if( pSkillStruct->SkillID != 0 )
		{
			lua_pushnumber( L, pSkillStruct->Point );
			return 1;
		}
	}
	
	return 0;
}
// --------------------------------------------------------------------------------------
int			LuaFunc_GetSkillIndex_SkillBook				( lua_State *L )
{
	int iType	= luaL_checkint( L, 1 );
	int iIndex	= luaL_checkint( L, 2 ) - 1;

	RoleDataEx*			pRole 			= CNetGlobal::RoleData();
	SkillBaseStruct*	pSkillStruct	= NULL;
	GameObjDbStruct*	pBaseSkillObj	= NULL;
	int					iLevel			= -1;
	int					iSkillIndex		= -1;

	if( iType == 1 )
	{
		//iSkillIndex		= iIndex;
		iSkillIndex			= g_pUi_SkillBook->GetSkillIndex( 1, iIndex );

		if( iSkillIndex != -1 )
		{
			pSkillStruct	= g_pUi_SkillBook->GetSkill( 1, iIndex );
			pBaseSkillObj	= g_ObjectData->GetObj( pSkillStruct->SkillID );
			iLevel			= pSkillStruct->Point;

			if( pSkillStruct->Point != 0 )
				iLevel--;
		}
		
		/*
		if( iSkillIndex >= 0 && iSkillIndex < _MAX_NormalSkill_COUNT_ )
		{
			iSkillIndex		= iIndex;
			//pSkillStruct	= &( pRole->BaseData.Skill.MainJob[ iIndex ] );
			pSkillStruct	= g_pUi_SkillBook->GetSkill( iIndex );

			pBaseSkillObj	= g_ObjectData->GetObj( pSkillStruct->SkillID );
			iLevel			= pSkillStruct->Point;

			if( pSkillStruct->Point != 0 )
				iLevel--;
		}
		*-/
	}
	else
	{
		if( iType == 2 )
		{
			iSkillIndex		= g_pUi_SkillBook->m_iLastSkillIndex;

			if( iSkillIndex >= 0 && iSkillIndex < _MAX_NormalSkill_COUNT_ )
			{
				//pSkillStruct	= &( pRole->BaseData.Skill.MainJob[ iSkillIndex ] );
				pSkillStruct	= g_pUi_SkillBook->GetSkill( 1, iSkillIndex );
				pBaseSkillObj	= g_ObjectData->GetObj( pSkillStruct->SkillID );
				iLevel			= iIndex;
			}
		}
	}

	if( iLevel != -1 && pSkillStruct && iLevel <= pSkillStruct->Point )
	{
		GameObjDbStruct*	pSkillObj		= pBaseSkillObj->AllMagicCol[ iLevel ];

		if( pBaseSkillObj &&  pSkillObj )
		{
			//iSkillIndex += _MAX_SPSkill_COUNT_;

			lua_pushnumber( L, iSkillIndex	);
			lua_pushnumber( L, iLevel		);

			return 2;
		}
	}


	return 0;

}
// --------------------------------------------------------------------------------------
int	LuaFunc_OnDrag_SkillButton					( lua_State *L )
{	
	int iIndex	= luaL_checkint( L, 1 );
	int iLevel	= luaL_checkint( L, 2 );
	
	SkillBaseStruct*	pSkillStruct	= NULL;
	RoleDataEx*			pRole 			= CNetGlobal::RoleData();

	if( iIndex != -1 &&  iIndex >= 0 && iIndex < _MAX_SPSkill_COUNT_+_MAX_NormalSkill_COUNT_+_MAX_NormalSkill_COUNT_ )
	{		
		pSkillStruct	= &( pRole->BaseData.Skill.AllMagic[ iIndex ] );
	}

	if( pSkillStruct != NULL && pSkillStruct->SkillID != 0 )
	{
		CUI_SkillBook*		p				= g_pUi_SkillBook;
		
		GameObjDbStruct*	pBaseSkillObj	= NULL;
		GameObjDbStruct*	pSkillObj		= NULL;
		
		if( p )
		{

			RoleDataEx*			pRole 			= CNetGlobal::RoleData();

			switch( g_pItemClipboard->GetType() )
			{
				case CItemClipboard::eItem_Type_None:			// 拿取物品
					{

						// 拿取法術
						// 檢查可否拿取
						//pSkillStruct	= &( pRole->BaseData.Skill.AllMagic[ iSkillIndex ] );
						pBaseSkillObj	= g_ObjectData->GetObj( pSkillStruct->SkillID );

						if( pSkillStruct->Point > iLevel && iLevel >= 0 )
						{
							pSkillObj		= pBaseSkillObj->AllMagicCol[ iLevel ];
							
							g_pItemClipboard->SetFile( pSkillObj->ImageObj->ACTField );
							g_pItemClipboard->PickupSkill( NULL, iIndex, iLevel );
						}	
					} break;

				default:
					{
						// clear item clipboard data
						g_pItemClipboard->Clear();
					} break;
			}
		}
	}

	return 0;
}
// --------------------------------------------------------------------------------------
SkillBaseStruct* CUI_SkillBook::GetSkill		( int iType, int iIndex )
{
	//int iSize = m_vecMainJobAvailableSpell.size();
	int iSkillIndex		= g_pUi_SkillBook->GetSkillIndex( iType, iIndex );

	if( iSkillIndex != -1 )
	{		
		RoleDataEx*			pRole 			= CNetGlobal::RoleData();

		SkillBaseStruct*	pSkillStruct	= &( pRole->BaseData.Skill.AllMagic[ iSkillIndex ] );

		if( pSkillStruct != NULL )
			return pSkillStruct;
	}

	return NULL;
}
// --------------------------------------------------------------------------------------
int CUI_SkillBook::GetSkillIndex	( int iType, int iIndex )
{
	switch( iType )
	{
		case 1: // 主職1
		{
			int iSize = m_vecMainJobAvailableSpell.size();

			if( iIndex >= 0 && iIndex < iSize )
			{
				int					iSkillIndex		= g_pUi_SkillBook->m_vecMainJobAvailableSpell[ iIndex ] + _MAX_SPSkill_COUNT_;
				return iSkillIndex;
			}
		} break;
	}

	return -1;
}

*/