//===========================================================================//
// File:	D3DRMVideoRenderables.cpp                                        //
// Project: MUNGA           Brick: Win95 Video Renderer 	                 //
// Contents: Windows95 Layer Video Renderer									 //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 03/13/97  JTR  Initial coding.                                            //
//---------------------------------------------------------------------------//
// Copyright (C) 1997, Virtual World Entertainment, Inc.                     //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "AdeptHeaders.hpp"

#include "VideoHeaders.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
SwitchComponent::ClassData*
	SwitchComponent::CreateFactoryRequest(FactoryRequestParameters *parameters)
{
	Check_Object(parameters);

	//
	//-------------------------------------------------------------------------
	// Allocate enough room for what we need to write out, then call our parent
	//-------------------------------------------------------------------------
	//
	MemoryStream *component_stream = parameters->m_stream;
	Check_Object(component_stream);
	component_stream->AllocateBytes(sizeof(SwitchComponent));
	bool result = BaseClass::CreateFactoryRequest(parameters) != NULL;

	//
	//-------------------------------
	// Find the input channel to read
	//-------------------------------
	//
	Page *page = parameters->m_page;
	Check_Object(page);
	DynamicArrayOf<ComponentDescriptor> *component_names = parameters->m_components;
	Check_Object(component_names);
	const char* input_name;
	page->GetEntry("Input", &input_name, true);

	int input_id = ComponentDescriptor::FindName(component_names, parameters->m_index, input_name);
	if (input_id == -1)
	{
		PAUSE((
			"%s: {[%s]Input=%s}: Unknown component name!",
			page->GetNotationFile()->GetFileName(),
			page->GetName(),
			input_name
		));
		result = false;
	}
	else
	{
		Component::ClassData* derivation = (*component_names)[input_id].m_classData;
		Check_Object(derivation);
		if (!derivation->IsDerivedFrom(ChannelOf<int>::DefaultData))
		{
			PAUSE((
				"%s: {[%s]Input=%s}: Not an integer channel!",
				page->GetNotationFile()->GetFileName(),
				page->GetName(),
				input_name
			));
			result = false;
		}
		else
		{
			*component_stream << input_id;
		}
	}

	//
	//----------------------------------------
	// Set up a name list for all the children
	//----------------------------------------
	//
	ChainOf<Note*> *children_chain = page->MakeNoteChain("Child");
	Check_Object(children_chain);
	Page::NoteIterator children(children_chain);

	//
	//-----------------------------------
	// Write out the number of lod ranges
	//-----------------------------------
	//
	int child_count = children.GetSize();
	*component_stream << child_count;

	//
	//-----------------------------------------------------------
	// Loop through the children, making sure we can hook them up
	//-----------------------------------------------------------
	//
	Note* child;
	int child_id = -1;
	while ((child = children.ReadAndNext()) != NULL)
	{
		Check_Object(child);

		//
		//------------------------------------
		// Find the child and write out its ID
		//------------------------------------
		//
		const char* child_name;
		child->GetEntry(&child_name);
		Check_Pointer(child_name);
		child_id = ComponentDescriptor::FindName(component_names, parameters->m_index, child_name);
		if (child_id == -1)
		{
			STOP((
				"%s: {[%s]Child=%s}: Unknown component name!",
				page->GetNotationFile()->GetFileName(),
				page->GetName(),
				child_name
			));
			result = false;
		}
		else
		{
			Component::ClassData* class_data = (*component_names)[child_id].m_classData;
			Check_Object(class_data);
			if (!class_data->IsDerivedFrom(VideoComponent::DefaultData))
			{
				PAUSE((
					"%s: {[%s]Child=%s}: Child is not a VideoComponent component!",
					page->GetNotationFile()->GetFileName(),
					page->GetName(),
					child_name
				));
				result = false;
			}
		}
		*component_stream << child_id;
	}

	//
	//---------------------------------------------------------------
	// Make sure to always write the null ID to stop the child stream
	//---------------------------------------------------------------
	//
	Unregister_Object(children_chain);
	delete children_chain;
	Check_Object(DefaultData);
	return (result) ? DefaultData : NULL;
}
