#include "../mainproc/GameMain.h"
#include "Minimap.h"

CMinimapFrame*			g_pMinimapFrame = NULL;
CUiFrame*				g_uiMinimapFrame = NULL;

int LuaFunc_SetMiniMapMask( lua_State* L );

// ----------------------------------------------------------------------------
CMinimapFrame::CMinimapFrame(CInterface* object)
	: CInterfaceBase(object)
{
	g_pMinimapFrame = this;
	g_uiMinimapFrame = NULL;
}

// ----------------------------------------------------------------------------
CMinimapFrame::~CMinimapFrame()
{
	g_pMinimapFrame = NULL;
	g_uiMinimapFrame = NULL;
}
// ----------------------------------------------------------------------------
void CMinimapFrame::RegisterFunc()
{
	lua_State* L = GetLuaStateWorld();
	if (L)
	{
		lua_register( L, "SetMiniMapMaskFile",			LuaFunc_SetMiniMapMask );


	}
}
// ----------------------------------------------------------------------------
void CMinimapFrame::RenderMinimap(CUiFrame* frame)
{
	CRuFusion_Minimap*	minimap = g_pGameMain->GetFusionMinimap();
	CRoleSprite*		player	= g_pGameMain->GetPlayer();

	static DWORD dwLastUpdate	= 0;

	if ( minimap )
	{
		CRuRectangle rect;
		CRuVector2 origin;

		if ( g_uiMinimapFrame )
		{
			CUiPoint pt = g_uiMinimapFrame->GetPos();
			CUiSize size = g_uiMinimapFrame->GetSize();
			rect.m_left = pt.m_x;
			rect.m_top = pt.m_y;
			rect.m_right = rect.m_left + size.m_x;
			rect.m_bottom = rect.m_top + size.m_y;

			origin.m_x = size.m_x / 2;
			origin.m_y = size.m_y / 2;
		}

		minimap->SetMinimapViewport(rect);		// 顯示區域大小
		minimap->SetMinimapOrigin(origin);		// 中心點位置
		//minimap->SetZoom(0.75f);
		minimap->SetZoom( 1 );

		if ( player )
			minimap->SetPlayerPosition(player->GetPos());
		
		minimap->Display();
	}


	if( player && GetTickCount() > dwLastUpdate )
	{
		UpdateMiniMap();
		dwLastUpdate = GetTickCount() + 300;	// 下次處理時間
	}

}
// ----------------------------------------------------------------------------
void CMinimapFrame::UpdateMiniMap()
{
	multimap < DWORD, CRoleSprite* >	mapRoleDistance;
	
	const map< DWORD, CRoleSprite* >*	pRoleMap	= g_pRuneDev->GetRoleMap();
	CRoleSprite*				pPlayer				= g_pGameMain->GetPlayer();
	CRuFusion_Minimap*			pMinimap				= g_pGameMain->GetFusionMinimap();
	CRoleSprite*				pObj				= NULL;
	
	CRuVector3					dir;
	DWORD						dwDir;

	for( map< DWORD, CRoleSprite* >::const_iterator it = pRoleMap->begin(); it != pRoleMap->end(); it++ )
	{
		pObj	= it->second;
		dir		= pPlayer->GetPos() - pObj->GetPos();
		dwDir	= dir.Magnitude();

		if( pPlayer == pObj )
			continue;
		else
			mapRoleDistance.insert( pair< DWORD, CRoleSprite*> ( dwDir, pObj ) );		
	}

	// 暫時測試 標出前 10 距離近的

	DWORD		dwCount			= 0;
	CUiFrame*	pIconFrame[10];
	CRuVector3	Pos;

	char		szFrameName[64];
	
	CUiPoint	MinimapScreenPos	= g_uiMinimapFrame->GetPos();
	CUiPoint	IconPos;

	for( int i = 0; i < 10; i++ )
	{
		sprintf( szFrameName, "MinimapFrame_Icon%d", ( i + 1 ) );
		pIconFrame[i]	= (CUiFrame*)g_pMinimapFrame->m_interface->GetUiWorld()->FindObject( szFrameName );
		((CUiFrame*)pIconFrame[i])->SetVisible( false );
	}
	

	for( multimap < DWORD, CRoleSprite* >::iterator it = mapRoleDistance.begin(); it != mapRoleDistance.end(); it++ )
	{	
		pObj		= it->second;
		Pos			= pMinimap->WorldToMinimap( pObj->GetPos() );
		
		IconPos.m_x	= Pos.m_x;
		IconPos.m_y = Pos.m_y;

		IconPos		= IconPos - MinimapScreenPos;

		// 設定連結關希
		CUiAnchor anchors;

		anchors.m_relativePoint = 0;
		anchors.m_relativeTo	= (CUiLayout*) g_uiMinimapFrame;
		anchors.m_offset.m_x	= IconPos.m_x;
		anchors.m_offset.m_y	= IconPos.m_y * -1;

		pIconFrame[ dwCount ]->ClearAllAnchors();
		pIconFrame[ dwCount ]->SetAnchor( anchors );
		((CUiFrame*)pIconFrame[ dwCount ])->SetVisible( true );

		dwCount++;

		if( dwCount == 10 )
			break;
	}

}
// ----------------------------------------------------------------------------
void CMinimapFrame::BuildFrames()
{
	// 註冊繪出小地圖函式
	// 先取得CUIFrame* 指標,在設定繪出函式
	g_uiMinimapFrame = (CUiFrame*)m_interface->GetUiWorld()->FindObject("MinimapFrame");
 
	if ( g_uiMinimapFrame )
	{
		g_uiMinimapFrame->m_pfRender = RenderMinimap;
	}
}
// ----------------------------------------------------------------------------
int LuaFunc_SetMiniMapMask( lua_State* L )
{
	const char* szMask = luaL_checkstring( L, 1 );

	g_pMinimapFrame->m_sMapMaskFile = szMask;

	g_pRuneDev->SetMiniMapMask( szMask );

	/*
	IRuTexture *maskTexture = g_ruResourceManager->LoadTexture("texture\\MinimapMask");
	if(maskTexture)
	{
		g_pRuneDev->m_pRuMinimap->SetMinimapMask(maskTexture);
		ruSAFE_RELEASE(maskTexture);
	}
	*/
	
	return 0;
}
// ----------------------------------------------------------------------------
