//
// ToolsAVP_Entity_VolumeTrigger.cpp
//
// Created by Anthony Gregson - 11/12/07
// 
// Copyright (C) 2007 Rebellion, All Rights Reserved.
//

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

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

#include "Entity/Triggers/AVP_ServerEntity_VolumeTrigger.h"

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

//////////////////////////////////////////////////////////////////////////////////////////////////
/*-------------------------------------------**
** ToolsAVP_Entity_VolumeTrigger **
**-------------------------------------------*/

ToolsAVP_Entity_VolumeTrigger* ToolsAVP_Entity_VolumeTrigger::s_pxThis = 0;

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

ToolsAVP_Entity_VolumeTrigger::ToolsAVP_Entity_VolumeTrigger(Asura_Entity* pxEntity)
: Tools_Entity_VolumeTrigger(pxEntity)
, m_pxChooseObject( 0 )
{
}

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

void ToolsAVP_Entity_VolumeTrigger::Create()
{
	//get a name , and allocate the tools data
	Asura_Guid uGuid = Asura_Entity_System::GetCreatedByToolsGuid();
	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("Volume Trigger");
	}
	if(!pxToolsData) return;

	//create the asura data
	AVP_ServerEntity_VolumeTrigger* pxEntity = AVP_ServerEntity_VolumeTrigger::CreateFromTools(uGuid);
	pxEntity->ToolsData = pxToolsData;

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

	Tools_SaveAsura::SetEntityModified();
}

void ToolsAVP_Entity_VolumeTrigger::OnSelection()
{
    Tools_Entity_VolumeTrigger::OnSelection();
    s_pxThis = this;
}

void ToolsAVP_Entity_VolumeTrigger::OnDeselection()
{
    Tools_Entity_VolumeTrigger::OnDeselection();
    s_pxThis = 0;
}

void ToolsAVP_Entity_VolumeTrigger::Copy(const Tools_Object* pxObject)
{
	PARENT::Copy(pxObject);
	
	const ToolsAVP_Entity_VolumeTrigger* pxFromObject = static_cast<const ToolsAVP_Entity_VolumeTrigger*>(pxObject);
	const AVP_ServerEntity_VolumeTrigger* pxFromEntity = static_cast<const AVP_ServerEntity_VolumeTrigger*>(pxFromObject->Entity);
	AVP_ServerEntity_VolumeTrigger* pxToEntity = static_cast<AVP_ServerEntity_VolumeTrigger*>(Entity);

	pxToEntity->m_uTriggerTypeFlags = pxFromEntity->m_uTriggerTypeFlags;
}

Tools_Object* ToolsAVP_Entity_VolumeTrigger::CreateObjectOfThisType() const
{
	Tools_Object* pxOldSelection = Tools_Object_System::SelectedObject;
	Create();
	Tools_Object* pxNewSelection = Tools_Object_System::SelectedObject;

	return (pxOldSelection!=pxNewSelection) ? pxNewSelection : 0;

}

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

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

	PARENT::AddOverlayControls(pxNewOverlay, xOffset);

	Asura_Assert(ThisEntity,"");
	Tools_Entity_VolumeTrigger* pxToolsEntity = static_cast<Tools_Entity_VolumeTrigger*>(ThisEntity);
	AVP_ServerEntity_VolumeTrigger* pxEntity = static_cast<AVP_ServerEntity_VolumeTrigger*>(pxToolsEntity->Entity);
	Asura_Assert(NUM_AVP_VOLUMETRIGGER_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 += pxNewDialog->DefaultSpacing();

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

	for (int i=0;i<NUM_AVP_VOLUMETRIGGER_TYPES;i++)
	{
		pxNewDialog = new Asura_FlagTickbox_Dialog(AVP_ServerEntity_VolumeTrigger::s_aszAVPVolumeTypes[i], xOffset, &pxEntity->m_uTriggerTypeFlags, (1 << i), &Changed);
		pxNewOverlay->AddDialog(pxNewDialog);
		xOffset.y += pxNewDialog->DefaultSpacing();
	}
}

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

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

void ToolsAVP_Entity_VolumeTrigger::GoToObjectWithGuid()
{
	Asura_Assert(ThisEntity,"");
	Tools_Entity_VolumeTrigger* pxToolsEntity = static_cast<Tools_Entity_VolumeTrigger*>(ThisEntity);
	AVP_ServerEntity_VolumeTrigger* pxEntity = static_cast<AVP_ServerEntity_VolumeTrigger*>(pxToolsEntity->Entity);
    Asura_Assert( pxEntity, "" );

    Asura_Guid uSpecificGuid = pxEntity->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_VolumeTrigger::SetSpecificGuid( Tools_Object* pxObject )
{
	Asura_Assert(ThisEntity,"");
	Tools_Entity_VolumeTrigger* pxToolsEntity = static_cast<Tools_Entity_VolumeTrigger*>(ThisEntity);
	AVP_ServerEntity_VolumeTrigger* pxEntity = static_cast<AVP_ServerEntity_VolumeTrigger*>(pxToolsEntity->Entity);
    Asura_Assert( pxEntity, "" );

	if(pxObject)
	{
	    pxEntity->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");
	}
}
