#include "AdeptHeaders.hpp"
#include "Entity.hpp"

//#############################################################################
//#################    Entity::ExecutionStateEngine    #####################
//#############################################################################

const StateEngine::StateEntry
	Entity__ExecutionStateEngine::StateEntries[]=
{
	STATE_ENTRY(Entity__ExecutionStateEngine, NeverExecute),
	STATE_ENTRY(Entity__ExecutionStateEngine, AlwaysExecute),
	STATE_ENTRY(Entity__ExecutionStateEngine, ExecuteOnce)
};

Entity__ExecutionStateEngine::ClassData*
	Entity__ExecutionStateEngine::DefaultData = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Entity__ExecutionStateEngine::InitializeClass()
{
	Check_Object(BaseClass::DefaultData);
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			Entity__ExecutionStateEngineClassID,
			"Adept::EntityExecutionStateEngine",
			BaseClass::DefaultData,
			ELEMENTS(StateEntries), StateEntries,
			Make,
			&FactoryRequest::ConstructFactoryRequest
		);
	Register_Object(DefaultData);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Entity__ExecutionStateEngine::TerminateClass()
{
	Unregister_Object(DefaultData);
	delete DefaultData;
	DefaultData = NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
Entity__ExecutionStateEngine*
	Entity__ExecutionStateEngine::Make(
		Entity *entity,
		const FactoryRequest *request
	)
{
	Check_Object(entity);
	Check_Object(request);
	Entity__ExecutionStateEngine* engine =
		new Entity__ExecutionStateEngine(DefaultData, entity, request);
	return engine;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
int
	Entity__ExecutionStateEngine::RequestState(
		int new_state,
		void* data
	)
{
	Check_Object(this);
	Check_Object(owningEntity);

	//
	//----------------------------------------------
	// Now, switch the state and tickle the watchers
	//----------------------------------------------
	//
	switch (BaseClass::RequestState(new_state, data))
	{
	case NeverExecuteState:
		if (oldState != NeverExecuteState)
			owningEntity->EnterNeverExecuteState();
		break;

	default:
		if (oldState == NeverExecuteState)
			owningEntity->LeaveNeverExecuteState();
		break;
	}

	return currentState;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Entity__ExecutionStateEngine::TestInstance()
{
	Verify(IsDerivedFrom(DefaultData));
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Entity__ExecutionStateEngine__ClassData::TestInstance()
{
	Verify(IsDerivedFrom(Entity__ExecutionStateEngine::DefaultData));
}

