//
// Tools_Entity_EnvTextureAnimControl.cpp
//
// Created by Richard Rice - 24/05/2005
// 
// Copyright (C) 2005 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"
#include "Entity/Tools_Entity_EnvTextureAnimControl.h"

#include "Animation/Asura_TextureAnimation.h"
#include "Camera/Asura_Camera.h"
#include "Entity/Asura_Entity_Classification.h"
#include "Entity/Animation/Asura_ServerEntity_EnvTextureAnimControl.h"
#include "Mesh/Asura_Mesh_System.h"
#include "Network/Asura_Message.h"

#include "Entity/Tools_Entity_Data.h"
#include "Load&Save/Tools_SaveAsura.h"
#include "Misc/Tools_Modal_Overlay.h"
#include "Objects/Tools_Object_System.h"


Tools_Entity_EnvTextureAnimControl::Tools_Entity_EnvTextureAnimControl(Asura_Entity* pxEntity)
:Tools_Entity_Object(pxEntity)
{
	Type = TOT_Entity_EnvTextureAnimControl;
	Mesh = Asura_Mesh_System::GetMesh("ToolsShape_TextureAnimation");
}

void Tools_Entity_EnvTextureAnimControl::Create()
{
	//Get a list of existing anim controllers to determine which anims have them
	List<Tools_Object*> xControllerList;
	Tools_Object_System::ListObjectsOfType(TOT_Entity_EnvTextureAnimControl,xControllerList);

	List<Asura_Hash_ID> xHashList;
	for(LIF<Tools_Object*> xControllerLIF(&xControllerList);!xControllerLIF.done();xControllerLIF.next())
	{
		Asura_Entity* pxEntity = ((Tools_Entity_Object*) xControllerLIF())->Entity;
		Asura_Assert(pxEntity->GetClassification() == AsuraEntityClass_EnvTextureAnimControl,"");

		Asura_ServerEntity_EnvTextureAnimControl* pxAnimEntity = (Asura_ServerEntity_EnvTextureAnimControl*) pxEntity;
		xHashList.add_entry(pxAnimEntity->m_uAnimHash);
	}

	//populate a list selection overlay with the animations that don't currently have controllers
	ListSelection_Overlay xOverlay("Create controller for which anim?");
	for(int i=0;i<Asura_TextureAnimation_System::GetNumberOfAnimations();i++)
	{
		Asura_TextureAnimation* pxAnim = Asura_TextureAnimation_System::GetAnimationByIndex(i);
		Asura_Hash_ID uHash = Asura_GetHashID(pxAnim->GetName());
		if(xHashList.contains(uHash)) continue;

		xOverlay.AddItem(pxAnim->GetName(),(int) pxAnim);
	}

	if(!xOverlay.GetNumItems())
	{
		ShowMessageOverlay("All animations already have animation controllers");
		return;
	}

	if(!xOverlay.DoModal()) return;

	Asura_TextureAnimation* pxAnim = (Asura_TextureAnimation*) xOverlay.GetSelectionData();

	//create tools data
	Tools_Entity_Data* pxToolsData = new Tools_Entity_Data;
	pxToolsData->Name = GetNamePrefix();
	pxToolsData->Name += pxAnim->GetName();
	pxToolsData->Position = *Asura_Camera::GetPosition();
	pxToolsData->m_bEntityModified = true;

	//create the asura entity
	Asura_ServerEntity_EnvTextureAnimControl* pxAnimEntity = new Asura_ServerEntity_EnvTextureAnimControl(Asura_Entity_System::GetCreatedByToolsGuid());
	pxAnimEntity->m_uAnimHash = Asura_GetHashID(pxAnim->GetName());
	pxAnimEntity->ToolsData = pxToolsData;

	//create the tools object
	Tools_Entity_EnvTextureAnimControl* pxObject = new Tools_Entity_EnvTextureAnimControl(pxAnimEntity);
	Tools_Object_System::SelectObject(pxObject);

	Tools_SaveAsura::SetEntityModified();

}

bool Tools_Entity_EnvTextureAnimControl::HandlesThisMessage(int iMessageID)
{
	//Only handles the one message , which is the sole reason for our existence.
	return (iMessageID == ASURA_MSG_TEXTUREANIMATION);
}

void Tools_Entity_EnvTextureAnimControl::AddOverlayControls(class Asura_Overlay* pxOverlay,class Asura_Vector_2& xOffset)
{
	PARENT::AddOverlayControls( pxOverlay, xOffset );

	char szText[100];
	Asura_ServerEntity_EnvTextureAnimControl* pxEntity = static_cast<Asura_ServerEntity_EnvTextureAnimControl*>(Entity);

	Asura_TextureAnimation* pxAnim = Asura_TextureAnimation_System::GetAnimationByHash( pxEntity->GetAnimHash() );
	
	const char* pszName = NULL;
	if (pxAnim)
	{
		pszName = pxAnim->GetName();
	}
	else
	{
		pszName = "<UNKNOWN>";
	}

	Asura_PrintfToBfr(szText,"Texture Anim: 0x%08x (%s)",100,true,pxEntity->GetAnimHash(), pszName);

	Asura_Dialog* pxNewDialog = new Asura_StringDisplay_Dialog( szText, xOffset );

	pxOverlay->AddDialog( pxNewDialog );
	xOffset.y += Asura_Dialog::DefaultSpacing();
}
