#include "../../MainProc/GameMain.h"
//#include "WorldFrame.h"
//#include "../../netwaker_member/NetWakerClientInc.h"		// for RoleData
#include "Ui_CharHeadFrame.h"


// 全域宣告
//-------------------------------------------------------------------
//using namespace ui;
CUI_CharHeadFrame*		g_pUi_CharHeadFrame						= NULL;

/*
CFontString*		CUI_CharHeadFrame::m_pFont_RoleName		= NULL;
CFontString*		CUI_CharHeadFrame::m_pFont_RoleHP		= NULL;
CFontString*		CUI_CharHeadFrame::m_pFont_RoleMP		= NULL;
					
CFontString*		CUI_CharHeadFrame::m_pFont_TargetName	= NULL;
CFontString*		CUI_CharHeadFrame::m_pFont_TargetLV		= NULL;
CFontString*		CUI_CharHeadFrame::m_pFont_TargetHP		= NULL;
CFontString*		CUI_CharHeadFrame::m_pFont_TargetMP		= NULL;
CStatusBar*			CUI_CharHeadFrame::m_pBar_TargetHP		= NULL;
CStatusBar*			CUI_CharHeadFrame::m_pBar_TargetMP		= NULL;

CLayout*			CUI_CharHeadFrame::m_pTargetFrame		= NULL;
CLayout*			CUI_CharHeadFrame::m_pCharFrame			= NULL;		// 人物狀態欄
*/

//_HEADFREAMINFO		CUI_CharHeadFrame::m_RoleInfo;
// _HEADFREAMINFO		CUI_CharHeadFrame::m_TargetRoleInfo;

//CRoleSprite*		CUI_CharHeadFrame::m_pTargetRole		= NULL;
//-------------------------------------------------------------------
//int			LuaFunc_OnLoad_CharHeadFrame	( lua_State *L );
int	LuaFunc_GetRoleInfo			( lua_State *L );
int	LuaFunc_IsTargetAvailable	( lua_State *L );
int	LuaFunc_GetTargetInfo		( lua_State *L );
int	LuaFunc_GetRoleName			( lua_State *L );

//-------------------------------------------------------------------
CUI_CharHeadFrame::CUI_CharHeadFrame(CInterface* object) : CInterfaceBase(object)
{
	g_pUi_CharHeadFrame			= this;
	//m_RoleInfo.m_pFrame			= NULL;
	//m_TargetRoleInfo.m_pFrame	= NULL;

	//CNetGlobal::Schedular()->Push( OnTimeUpdate , 50 , NULL , NULL );
}
//--------------------------------------------------------------------------------------
CUI_CharHeadFrame::~CUI_CharHeadFrame(void)
{
	//int a ="33";
}
//--------------------------------------------------------------------------------------
void CUI_CharHeadFrame::RegisterFunc(void)
{
	lua_State* l = GetLuaStateWorld();
	if (l)
	{
		lua_register( l, "GetRoleInfo",						LuaFunc_GetRoleInfo					);	
		lua_register( l, "IsTargetAvailable",				LuaFunc_IsTargetAvailable			);
		lua_register( l, "GetTargetInfo",					LuaFunc_GetTargetInfo				);
		lua_register( l, "GetRoleName",						LuaFunc_GetRoleName					);
		

		//lua_register( l, "OnLoad_CharHeadFrame",			LuaFunc_OnLoad_CharHeadFrame		);
		//lua_register( l, "OnLoad_TargetHeadFrame",			LuaFunc_OnLoad_TargetHeadFrame		);
	}
}
//--------------------------------------------------------------------------------------
int	LuaFunc_GetRoleName ( lua_State *L )
{
	CRoleSprite* pRole = g_pGameMain->GetPlayer();

	if( pRole )
	{
		lua_pushstring ( L, pRole->GetName()	);	// 1
		return 1;
	}

	return 0;
}
//--------------------------------------------------------------------------------------
int	LuaFunc_GetRoleInfo ( lua_State *L )
{
	CRoleSprite* pRole = g_pGameMain->GetPlayer();

	if( pRole )
	{
		lua_pushstring ( L, pRole->GetName()	);	// 1
		lua_pushnumber ( L, pRole->GetLevel()	);	// 2
		lua_pushnumber ( L, pRole->GetHP()		);	// 3
		lua_pushnumber ( L, pRole->GetMaxHP()	);	// 4
		lua_pushnumber ( L, pRole->GetMP()		);	// 5
		lua_pushnumber ( L, pRole->GetMaxMP()	);	// 6
		lua_pushnumber ( L, pRole->GetSP()		);	// 7
		lua_pushnumber ( L, pRole->GetMaxSP()	);	// 8

		return 8;
	}

	return 0;
}
//-------------------------------------------------------------------
int	LuaFunc_IsTargetAvailable	( lua_State *L )
{
	CRoleSprite* pRole = g_pGameMain->GetTargetPlayer();

	if( pRole )
	{
		// 這裡應該判斷是那種類型的目標( 敵人 / 同伴 / NPC ... )
		
		lua_pushnumber( L, 1 );
	}
	else
	{
		lua_pushnumber( L, 0 );		// 代表目標不存在
	}

	return 1;
}
//-------------------------------------------------------------------
int	LuaFunc_GetTargetInfo ( lua_State *L )
{
	CRoleSprite* pRole = g_pGameMain->GetTargetPlayer();

	if( pRole )
	{
		lua_pushstring ( L, pRole->GetName()	);	// 1
		lua_pushnumber ( L, pRole->GetLevel()	);	// 2
		lua_pushnumber ( L, pRole->GetHP()		);	// 3
		lua_pushnumber ( L, pRole->GetMaxHP()	);	// 4
		lua_pushnumber ( L, pRole->GetMP()		);	// 5
		lua_pushnumber ( L, pRole->GetMaxMP()	);	// 6
		//lua_pushnumber ( L, pRole->GetSP()		);	// 7
		//lua_pushnumber ( L, pRole->GetMaxSP()	);	// 8

		return 6;
	}

	g_pGameMain->GetInterface()->GetUi()->SendEvent( UiEvent_TargetChange );

	return 0;
}
//-------------------------------------------------------------------
/*
int LuaFunc_OnLoad_CharHeadFrame( lua_State *L )
{
	CUI_CharHeadFrame*		p	= g_pUi_CharHeadFrame;
	if( !p ) return 0;

	// 這裡有點不爽, 想改成讀取時更新就好
	p->m_RoleInfo.m_pFrame			= g_pInterface->GetUi()->FindObject( "UI_RoleHeadFrame" );

	p->m_RoleInfo.m_pName			= (CUiFontString*)	g_pInterface->GetUi()->FindObject ( "UI_RoleHeadFrame_Border_Name"		);
	p->m_RoleInfo.m_pLV				= (CUiFontString*)	g_pInterface->GetUi()->FindObject ( "UI_RoleHeadFrame_Border_LVTEXT"		);

	p->m_RoleInfo.m_pHP_Bar			= (CUiStatusBar*)	g_pInterface->GetUi()->FindObject (	"UI_RoleHeadFrame_HPBAR"			);
	p->m_RoleInfo.m_pMP_Bar			= (CUiStatusBar*)	g_pInterface->GetUi()->FindObject (	"UI_RoleHeadFrame_MPBAR"			);
	p->m_RoleInfo.m_pSP_Bar			= (CUiStatusBar*)	g_pInterface->GetUi()->FindObject (	"UI_RoleHeadFrame_SPBAR"			);
	
	return 0;
}
*/
//-------------------------------------------------------------------
/*
int LuaFunc_OnLoad_TargetHeadFrame( lua_State *L )
{
	CUI_CharHeadFrame*		p	= g_pUi_CharHeadFrame;
	if( !p ) return 0;

	// 這裡有點不爽, 想改成讀取時更新就好
	p->m_TargetRoleInfo.m_pFrame 	= g_pInterface->GetUi()->FindObject( "UI_TargetHeadFrame" );

	p->m_TargetRoleInfo.m_pName		= (CUiFontString*)	g_pInterface->GetUi()->FindObject ( "UI_TargetHeadFrame_Border_Name"			);	
	p->m_TargetRoleInfo.m_pLV		= (CUiFontString*)	g_pInterface->GetUi()->FindObject ( "UI_TargetHeadFrame_Border_LVTEXT"		);
	p->m_TargetRoleInfo.m_pHP_Bar	= (CUiStatusBar*)	g_pInterface->GetUi()->FindObject (	"UI_TargetHeadFrame_HPBAR"				);
	p->m_TargetRoleInfo.m_pMP_Bar	= (CUiStatusBar*)	g_pInterface->GetUi()->FindObject (	"UI_TargetHeadFrame_MPBAR"				);
	p->m_TargetRoleInfo.m_pSP_Bar	= (CUiStatusBar*)	g_pInterface->GetUi()->FindObject (	"UI_TargetHeadFrame_SPBAR"				);

	return 0;
}
*/


//--------------------------------------------------------------------------------------
/*
void CUI_CharHeadFrame::Initial()
{
if ( g_pUi_CharHeadFrame == NULL )
{
g_pUi_CharHeadFrame = new CUI_CharHeadFrame;
}
}
*/
// --------------------------------------------------------------------------------------
/*
int	CUI_CharHeadFrame::OnTimeUpdate( SchedularInfo* Obj , void* InputClass )
{
char szOut[64];
CNetGlobal::Schedular()->Push( OnTimeUpdate , 50 , NULL , NULL );

CRoleSprite* pRole		= g_pWorldFrame->GetPlayer();

if( m_RoleInfo.m_pFrame && pRole )
{
// 更新 HP 及 MP

if( m_RoleInfo.m_pName )
{
m_RoleInfo.m_pName->SetTextA(  pRole->GetName() );
}

if( m_RoleInfo.m_pLV )
{
sprintf( szOut, "%d", pRole->GetLevel() );
m_RoleInfo.m_pLV->SetTextA( szOut );
}

if( m_RoleInfo.m_pHP_Bar )
{
m_RoleInfo.m_pHP_Bar ->SetMaxValue( pRole->GetMaxHP() );
m_RoleInfo.m_pHP_Bar ->SetValue( pRole->GetHP() );
}

if( m_RoleInfo.m_pMP_Bar )
{
m_RoleInfo.m_pMP_Bar->SetMaxValue( pRole->GetMaxMP() );
m_RoleInfo.m_pMP_Bar->SetValue( pRole->GetMP() );
}

if( m_RoleInfo.m_pSP_Bar )
{
m_RoleInfo.m_pSP_Bar->SetMaxValue( pRole->GetMaxMP() );
m_RoleInfo.m_pSP_Bar->SetValue( pRole->GetMP() );
}
}

if( m_TargetRoleInfo.m_pFrame )
{
// 檢查更新
CRoleSprite* pTargetRole = g_pWorldFrame->GetTargetPlayer();

if( pTargetRole )
{
if( m_TargetRoleInfo.m_pFrame->IsVisible() == false )
m_TargetRoleInfo.m_pFrame->Show();

if( m_pTargetRole != pTargetRole )
{
m_pTargetRole = pTargetRole;
}



if( m_TargetRoleInfo.m_pName )
{
m_TargetRoleInfo.m_pName->SetTextA(  m_pTargetRole->GetName() );
}

if( m_TargetRoleInfo.m_pLV )
{
sprintf( szOut, "%d", m_pTargetRole->GetLevel() );
m_TargetRoleInfo.m_pLV->SetTextA( szOut );
}

if( m_TargetRoleInfo.m_pHP_Bar )
{
m_TargetRoleInfo.m_pHP_Bar ->SetMaxValue( m_pTargetRole->GetMaxHP() );
m_TargetRoleInfo.m_pHP_Bar ->SetValue( m_pTargetRole->GetHP() );
}

if( m_TargetRoleInfo.m_pMP_Bar )
{
m_TargetRoleInfo.m_pMP_Bar->SetMaxValue( m_pTargetRole->GetMaxMP() );
m_TargetRoleInfo.m_pMP_Bar->SetValue( m_pTargetRole->GetMP() );
}

if( m_TargetRoleInfo.m_pSP_Bar )
{
m_TargetRoleInfo.m_pSP_Bar->SetMaxValue( m_pTargetRole->GetMaxMP() );
m_TargetRoleInfo.m_pSP_Bar->SetValue( m_pTargetRole->GetMP() );
}
}
else
{
m_pTargetRole = NULL;
m_TargetRoleInfo.m_pFrame->Hide();
}



}

return 0;
}
*/
// --------------------------------------------------------------------------------------
/*
void CUI_CharHeadFrame::Release()
{
SAFE_DELETE( g_pUi_CharHeadFrame );	
}
*/