//===========================================================================//
// File:	D3DRMVideoRenderables.cpp                                        //
// Project: MUNGA           Brick: Win95 Video Renderer 	                 // 
// Contents: GOS95 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 "LightComponent.hpp"
#include "EntityClassData.hpp"
#include <MLR\MLRLight.hpp>

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
LightComponent::ClassData*
	LightComponent::CreateFactoryRequest(
		FactoryRequestParameters *parameters,
		Entity::ClassData *class_data
	)
{
	Check_Object(parameters);
	Check_Object(class_data);

	//
	//-------------------------------------------------------------------------
	// 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(LightComponent));

	//
	//----------------------------
	// Let our parent do its thing
	//----------------------------
	//
	bool result = BaseClass::CreateFactoryRequest(parameters) != NULL;

	//
	//------------------------------------
	// Find the parent object for the mesh
	//------------------------------------
	//
	Page *page = parameters->m_page;
	Check_Object(page);

	int attribute_id = -1;
	const char* attribute_name;
	if (page->GetEntry("IntensityAttribute", &attribute_name))
	{
		Check_Object(class_data);
		const AttributeEntry *attribute_entry =
			class_data->attributeTable.GetAttributeEntry(attribute_name);
		if (attribute_entry == NULL)
		{
			STOP((
				"%s: {[%s]IntensityAttribute=%s}: Unknown attribute!",
				page->GetNotationFile()->GetFileName(),
				page->GetName(),
				attribute_name
			));
			result = false;
		}
		else
		{
			Check_Object(attribute_entry);
			if (attribute_entry->attributeType != ScalarClassID)
			{
				PAUSE((
					"%s: {[%s]IntensityAttribute=%s}: Attribute is not correct type!",
					page->GetNotationFile()->GetFileName(),
					page->GetName(),
					attribute_name
				));
				result = false;
			}
			else
				attribute_id = attribute_entry->attributeID;
		}
	}

	//
	//-----------------------------
	// Read the light from the file
	//-----------------------------
	//
	MidLevelRenderer::WriteMLRVersion(component_stream);
	MidLevelRenderer::MLRLight* light = MidLevelRenderer::MLRLight::Make(page);
	Check_Object(light);
	light->Save(component_stream);
	delete light;

	*component_stream << attribute_id;

	Check_Object(DefaultData);
	return (result) ? DefaultData : NULL;
}

