//////////////////////////////////////////////////////////////////////////////
//
// Asura_ServerEntity_DialogueTrigger.h
//
// Copyright (c) 2006 Rebellion. All rights reserved.
//
// Description:
//
//     entity which triggers dialogue using the new dialogue events
//
//////////////////////////////////////////////////////////////////////////////

// B E G I N   F I L E   W R A P P E R ///////////////////////////////////////

#ifdef ASURA_USE_PRAGMA_ONCE
    #pragma once
#endif

#ifndef ___ASURA_SERVERENTITY_DIALOGUETRIGGER_H___
#define ___ASURA_SERVERENTITY_DIALOGUETRIGGER_H___ 1

#ifdef ASURA_USE_DIALOGUE_SYSTEM

// I N C L U D E S ///////////////////////////////////////////////////////////
#include "Core/Asura_Hash.h"
#include "Entity/Triggers/Asura_ServerEntity_Activatable.h"

// F O R W A R D   D E C L A R A T I O N S ///////////////////////////////////
class Asura_Dialogue_CharacterMappingList;
class Asura_Dialogue_Event;

// C L A S S E S /////////////////////////////////////////////////////////////
class Asura_ServerEntity_DialogueTrigger : public Asura_ServerEntity_Activatable
{
	friend class Tools_Entity_DialogueTrigger;
public:

	Asura_ServerEntity_DialogueTrigger();
	Asura_ServerEntity_DialogueTrigger( Asura_Guid uGuid );
	Asura_ServerEntity_DialogueTrigger( Asura_Guid uGuid, const char*& pcChunkBuffer );
	~Asura_ServerEntity_DialogueTrigger();

	static void CreateFromChunkBuffer( Asura_Guid uGuid, const char*& pcChunkBuffer );

	void SetEventHashID(Asura_Hash_ID uEventHash);

	#ifdef ASURA_TOOLS
	//Clears any pointers to the event being deleted
	static void HandleEventDeletion(const Asura_Dialogue_Event* pxEvent);
	#endif

	Asura_Hash_ID GetEventHashID() const;

	#ifdef ASURA_TOOLS
	virtual bool SupportsCustomEvents() const { return true; }
	virtual int GetNumCustomEvents() const { return 1; }
	virtual unsigned short* GetCustomEvent(int iIndex) { return &m_usMessageBlock; }
	virtual void GetCustomEventName(int iEventIndex, Asura_String& xEventName) { xEventName = "Messages"; }
	#endif

protected :
	virtual bool CanBeSavedToChunk() const {return true;}
	virtual	bool IsFilledFromChunkBuffer(void) const{ return (false); }
	
	virtual	void WriteToChunkStream (Asura_Chunk_Stream& xStream) const;
	virtual	void ReadFromChunkStream (Asura_Chunk_Stream& xStream);

	virtual void OnTrigger();

	virtual bool HandleMessage(const Asura_Message* pxMessage);

	#ifdef ASURA_TOOLS
	virtual void ApplyChangesForTemplateInstance(class Tools_EntityTemplateInstance* pxInstance);
	virtual bool CanBeIncludedInTemplate() const;
	#endif

	static void TestDialogueTriggerStream(Asura_Guid uGuid, const char*& pcChunkBuffer);

	u_int GetNumMappings() const;

	// data
	Asura_Hash_ID m_uEventHash;
	const Asura_Dialogue_Event* m_pxEvent;
	Asura_Dialogue_CharacterMappingList* m_pxMappings;
	#ifdef ASURA_USE_STREAMING
	Asura_Hash_ID m_uHandlerID;//hash ID of dialogue handler that event should be taken from
	#endif
	unsigned short m_usMessageBlock;

	// todo: add dialogue-specific messages use during playback.

	#ifdef ASURA_TOOLS
	public:
		static Asura_ServerEntity_DialogueTrigger* FindEntity(Asura_Guid uGuid)
		{
			return s_xList.FindEntity(uGuid);
		}

		static const Asura_Hashed_List<Asura_ServerEntity_DialogueTrigger>* GetList()
		{
			return &s_xList;
		}

	private:
		static Asura_Hashed_List<Asura_ServerEntity_DialogueTrigger> s_xList;
	#endif

};

// message header has the event ID and the number of mappings.
// It is followed by that number of Asura_Dialogue_CharacterMapping.
class Asura_DialogueTrigger_MessageHeader
{
public:
	Asura_Hash_ID m_uEventID;
	u_int m_uNumMappings;
	#ifdef ASURA_USE_STREAMING
	Asura_Hash_ID m_uHandlerID;//hash ID of dialogue handler that event should be taken from
	#endif
	unsigned short m_usMessageBlock;

};

#endif

// E N D   F I L E   W R A P P E R ///////////////////////////////////////////

#endif // #ifndef ___ASURA_SERVERENTITY_DIALOGUETRIGGER_H___
