//////////////////////////////////////////////////////////////////////////////
//
// ToolsAVP_Entity_LocationMarker.cpp
//
// Created by AJK 25/02/2009
//
// Copyright (c) 2009 Rebellion. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////
// Core
#include "Core/Asura.h"

// Header
#include "Entity/ObjectiveSystem/PlayerObjective/ToolsAVP_Entity_LocationMarker.h"

// AVP
#include "Entity/ObjectiveSystem/PlayerObjective/AVP_ServerEntity_LocationMarker.h"
#include "Entity/ObjectiveSystem/PlayerObjective/AVP_ServerEntity_PlayerObjective.h"

// Tools Asura
#include "Objects/ToolsAVP_Object_Type.h"

// Tools
#include "Entity/ObjectiveSystem/PlayerObjective/ToolsAVP_Entity_PlayerObjective.h"

// Statics def
ToolsAVP_Entity_LocationMarker*	                  ToolsAVP_Entity_LocationMarker::s_pxThis = NULL;
Asura_Hashed_List<ToolsAVP_Entity_LocationMarker> ToolsAVP_Entity_LocationMarker::s_xList;

ToolsAVP_Entity_LocationMarker::ToolsAVP_Entity_LocationMarker(Asura_Entity* pxEntity)
: PARENT(pxEntity)
, m_pxMarker( static_cast< AVP_ServerEntity_LocationMarker* > ( pxEntity ) )
{
	Type = TOT_Entity_LocationMarker;

	SetMesh( "ToolsShape_CutsceneTrackPoint" );

	s_xList.AddToList(this);
}

void ToolsAVP_Entity_LocationMarker::Update()
{
	if ( m_pxMarker )
	{
		Position = m_pxMarker->GetPosition();
	}
}

void ToolsAVP_Entity_LocationMarker::OnSelection()
{
	s_pxThis = this;
}

ToolsAVP_Entity_LocationMarker::~ToolsAVP_Entity_LocationMarker()
{
	s_xList.RemoveFromList(this);
}

void ToolsAVP_Entity_LocationMarker::Destroy()
{
	// Remove me from any any room that links to me
	RemoveMeFromPlayerObjectiveList();

	PARENT::Destroy();
}

void ToolsAVP_Entity_LocationMarker::UpdatePositionInAsuraData( bool bUpdateLinkedObjects )
{
	if ( m_pxMarker )
	{
		m_pxMarker->SetPosition( Position );
	}
}

void ToolsAVP_Entity_LocationMarker::RemoveMeFromPlayerObjectiveList() const
{
	AVP_ServerEntity_PlayerObjective* pxPlayerObjective = AVP_ServerEntity_PlayerObjective::FindEntity(  m_pxMarker->m_uPlayerObjectiveGuid );
	if ( pxPlayerObjective )
	{
		Asura_LinkList_It< Asura_Guid > xLinkListIter( pxPlayerObjective->GetMarkerList() );
		while ( !xLinkListIter.Done() )
		{
			if( (*xLinkListIter.GetCurrent()) == m_pxMarker->GetGuid() )
			{
				ToolsAVP_Entity_PlayerObjective* pxToolEntity = ToolsAVP_Entity_PlayerObjective::FindEntity( pxPlayerObjective->GetGuid() );
				if (pxToolEntity)
				{
					pxToolEntity->SetEntityModified();
				}
				xLinkListIter.DeleteCurrent();
				break;
			}
			xLinkListIter.Next();
		}
	}
}

Tools_Object* ToolsAVP_Entity_LocationMarker::Create( Asura_Entity* pxEntity )
{
	ToolsAVP_Entity_LocationMarker* pxObject = new ToolsAVP_Entity_LocationMarker( AVP_ServerEntity_LocationMarker::FindEntity( pxEntity->GetGuid() ) );
	return pxObject;
}