//
// ToolsAVP_Entity_AdvancedVolumeTrigger.cpp
//
// Created by Anthony Gregson - 08/09/05 (from ToolsFPSDemo_Entity_AdvancedVolumeTrigger by Andrew Weinkove - 14/04/2004)
// 
// Copyright (C) 2005 Rebellion, All Rights Reserved.
//

// core
#include "Core/Asura.h"
#include "Entity/Triggers/ToolsAVP_Entity_AdvancedVolumeTrigger.h"

// asura
#include "Interface/Asura_Dialog.h"
#include "Interface/Asura_Overlay.h"

#include "Entity/Triggers/AVP_ServerEntity_AdvancedVolumeTrigger.h"

#include "Objects/Tools_Object_System.h"
#include "Load&Save/Tools_SaveAsura.h"
#include "Misc/Tools_Timed_Messages.h"

//////////////////////////////////////////////////////////////////////////////////////////////////
/*-------------------------------------------**
** ToolsAVP_Entity_AdvancedVolumeTrigger **
**-------------------------------------------*/

ToolsAVP_Entity_AdvancedVolumeTrigger* ToolsAVP_Entity_AdvancedVolumeTrigger::s_pxThis = 0;

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////

ToolsAVP_Entity_AdvancedVolumeTrigger::ToolsAVP_Entity_AdvancedVolumeTrigger(Asura_Entity* pxEntity)
: Tools_Entity_AdvancedVolumeTrigger(pxEntity)
, m_pxChooseObject( 0 )
{
}

ToolsAVP_Entity_AdvancedVolumeTrigger::~ToolsAVP_Entity_AdvancedVolumeTrigger()
{
	if ( m_pxChooseObject )
	{
		Tools_Object_System::ChooseObject_Finish();
		Asura_Assert( !m_pxChooseObject, "" );
	}
}

void ToolsAVP_Entity_AdvancedVolumeTrigger::Create()
{
	//get a name , and allocate the tools data
	Asura_Guid uGuid = Asura_Entity_System::GetNextToolsCreatedGuid();
	Tools_Entity_Data* pxToolsData = 0;
	if(Tools_Object_System::IsCopying())
	{
		//This has been created as part of a copy/paste operation , so pick a default name. RAR 23/06/2006
		pxToolsData = Tools_Entity_Object::CreateToolsDataWithDefaultName("Volume Trigger","Volume Trigger",uGuid);
	}
	else
	{
		pxToolsData = Tools_Entity_Object::CreateToolsData("Advanced Volume Trigger");
	}
	if(!pxToolsData) return;

	//create the asura data
	Asura_Bounding_Box xBox = {0,0,0,0,0,0};
	AVP_ServerEntity_AdvancedVolumeTrigger* pxAsuraEntity = new AVP_ServerEntity_AdvancedVolumeTrigger(xBox,true);
	pxAsuraEntity->ToolsData = pxToolsData;

	//create the tools object
	Tools_Object* pxObject = new ToolsAVP_Entity_AdvancedVolumeTrigger(pxAsuraEntity);
	Tools_Object_System::SelectObject(pxObject);

	Tools_SaveAsura::SetEntityModified();
}

void ToolsAVP_Entity_AdvancedVolumeTrigger::OnSelection()
{
    Tools_Entity_AdvancedVolumeTrigger::OnSelection();
    s_pxThis = this;
}

void ToolsAVP_Entity_AdvancedVolumeTrigger::OnDeselection()
{
    Tools_Entity_AdvancedVolumeTrigger::OnDeselection();
    s_pxThis = 0;
}

Tools_Object* ToolsAVP_Entity_AdvancedVolumeTrigger::Create( Asura_Entity* pxEntity )
{
	return new ToolsAVP_Entity_AdvancedVolumeTrigger( pxEntity );
}

void ToolsAVP_Entity_AdvancedVolumeTrigger::AddOverlayControls(class Asura_Overlay* pxNewOverlay,class Asura_Vector_2& xOffset)
{
	Asura_Dialog* pxNewDialog;

	Tools_Entity_AdvancedVolumeTrigger::AddOverlayControls(pxNewOverlay, xOffset);

	Asura_Assert(ThisEntity,"");
	Tools_Entity_AdvancedVolumeTrigger* pxToolsEntity = static_cast<Tools_Entity_AdvancedVolumeTrigger*>(ThisEntity);
	Asura_ServerEntity_AdvancedVolumeTrigger* pxAsuraEntity = static_cast<Asura_ServerEntity_AdvancedVolumeTrigger*>(pxToolsEntity->Entity);
	Asura_Assert(NUM_AVP_DEPRECATEDVOLUMETRIGGER_TYPES<=32, "Error, maximum number of volume trigger lists is 32");

	pxNewDialog = new Asura_Function_Dialog( "Set Specific Object", xOffset, SetSpecificGuid );
	pxNewOverlay->AddDialog( pxNewDialog );
	xOffset.y += 1.250f;

	pxNewDialog = new Asura_Function_Dialog( "Go To Specific Object", xOffset, GoToObjectWithGuid );
	pxNewOverlay->AddDialog( pxNewDialog );
	xOffset.y += 1.250f;

	for (int i=0;i<NUM_AVP_DEPRECATEDVOLUMETRIGGER_TYPES;i++)
	{
		pxNewDialog = new Asura_FlagTickbox_Dialog(AVP_ServerEntity_AdvancedVolumeTrigger::s_aszAVPVolumeTypes[i],xOffset,&pxAsuraEntity->m_iListFlags, (1 << i),&Changed);
		pxNewOverlay->AddDialog(pxNewDialog);
		xOffset.y += 1.250f;
	}
}

void ToolsAVP_Entity_AdvancedVolumeTrigger::SetSpecificGuid()
{
	Asura_Assert( ThisEntity, "" );
    Asura_Assert( s_pxThis, "ToolsAVP_Entity_AdvancedVolumeTrigger::SetSpecificGuid - no this ptr" );

    ToolsAVP_ChooseEntityForAdvancedVolumeTrigger* pxChooseObject = new ToolsAVP_ChooseEntityForAdvancedVolumeTrigger( s_pxThis );
    Tools_Object_System::ChooseObject( 0, pxChooseObject );
}

void ToolsAVP_Entity_AdvancedVolumeTrigger::GoToObjectWithGuid()
{
	Asura_Assert(ThisEntity,"");
	Tools_Entity_AdvancedVolumeTrigger* pxToolsEntity = static_cast<Tools_Entity_AdvancedVolumeTrigger*>(ThisEntity);
	Asura_ServerEntity_AdvancedVolumeTrigger* pxAsuraEntity = static_cast<Asura_ServerEntity_AdvancedVolumeTrigger*>(pxToolsEntity->Entity);
    Asura_Assert( pxAsuraEntity, "" );

    Asura_Guid uSpecificGuid = pxAsuraEntity->m_uSpecificGuid;
    if ( uSpecificGuid != ASURA_GUID_UNREGISTERED )
    {
        Tools_Object* pxToolsObject = Tools_Object_System::GetObjectFromGuid( uSpecificGuid );
        if ( pxToolsObject )
        {
            Tools_Object_System::GotoObject( pxToolsObject );
        }
        else
        {
            Asura_Assert( 0, "object with specific guid could not be found" );
        }
    }
    else
    {
        Tools_Timed_Message_System::AddMessage( Tools_Timed_Message_System::COLOUR_ERROR, "No Specific Object set" );
    }
}

void ToolsAVP_Entity_AdvancedVolumeTrigger::SetSpecificGuid( Tools_Object* pxObject )
{
	Asura_Assert(ThisEntity,"");
	Tools_Entity_AdvancedVolumeTrigger* pxToolsEntity = static_cast<Tools_Entity_AdvancedVolumeTrigger*>(ThisEntity);
	Asura_ServerEntity_AdvancedVolumeTrigger* pxAsuraEntity = static_cast<Asura_ServerEntity_AdvancedVolumeTrigger*>(pxToolsEntity->Entity);
    Asura_Assert( pxAsuraEntity, "" );

	if(pxObject)
	{
	    pxAsuraEntity->m_uSpecificGuid = pxObject->GetGuid();
	    Tools_Timed_Message_System::AddMessage( Tools_Timed_Message_System::COLOUR_INFO, "Found Specific Object: %s", pxObject->GetName() );
	}
	else
	{
		Tools_Timed_Message_System::AddMessage( Tools_Timed_Message_System::COLOUR_ERROR, "No Object selected");
	}
}
