//===========================================================================//
// File:	cmpnnt.cc                                                           //
// Project: MUNGA           Brick: Entity												  //
// Contents: 																					  //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 12/14/94  ECH  Initial coding.                                            //
//---------------------------------------------------------------------------//
// Copyright (C) 1994-1995, Virtual World Entertainment, Inc.                //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "AdeptHeaders.hpp"

#include "ComponentHeaders.hpp"
#include "AttributeWatcher.hpp"

StateWatcher::ClassData*
	StateWatcher::DefaultData = NULL;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	StateWatcher::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			StateWatcherClassID,
			"Adept::StateWatcher",
			ChannelOf<int>::DefaultData,
			0,
			NULL
		);
	Register_Object(DefaultData);
}

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
StateWatcher::~StateWatcher()
{
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
StateWatcher*
	StateWatcher::Make(
		MemoryStream *stream,
		ComponentWeb *owning_web,
		Entity *entity
	)
{
	return
		new StateWatcher(
			DefaultData,
			stream,
			owning_web,
			entity
		);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
StateWatcher::StateWatcher(
	ClassData *class_data,
	MemoryStream *stream,
	ComponentWeb *owning_web,
	Entity *entity
):
	ChannelOf<int>(class_data, stream, owning_web)
{
	int attribute_id;
	*stream >> attribute_id >> oldStateMatch >> newStateMatch;

	Check_Object(entity);
	AttributeEntry *attribute_entry = entity->GetAttributeEntry(attribute_id);
	Check_Object(attribute_entry);
	#if defined(_ARMOR)
		RegisteredClass::ClassData *entry_class_data;
		entry_class_data = FindClassData(attribute_entry->attributeType);
		Check_Object(entry_class_data);
		Verify(entry_class_data->IsDerivedFrom(StateEngine::DefaultData));
	#endif

	attribute_entry->GetValue(entity, &stateEngine);
	Check_Object(stateEngine);

	stateEngine->AddWatcher(this);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	StateWatcher::Execute()
{
	Check_Object(stateEngine);
	int old_state = stateEngine->GetOldState();
	channelOutput = stateEngine->GetState();
	if (
		(oldStateMatch == -1 || oldStateMatch == old_state)
		 && (newStateMatch == -1 || newStateMatch == channelOutput)
	)
	{
		NotifyDependantsOfChange();
	}
}

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