#include "AdeptHeaders.hpp"

#include "ComponentHeaders.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MatcherOf<int>::ClassData*
	MatcherOf<int>::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(MatcherOf<int>));
	bool result =
		BaseClass::CreateFactoryRequest(parameters) != NULL;

	//
	//---------------
	// File variables
	//---------------
	//
	Page* page = parameters->m_page;
	Check_Object(page);

	//
	//---------------------
	// Read the match value
	//---------------------
	//
	int match_value;
	page->GetEntry("MatchValue", &match_value, true);
	*component_stream << match_value;

	//
	//-----------------------
	// Read the match command
	//-----------------------
	//
	const char* command;
	int command_id = -1;
	if (page->GetEntry("MatchCommand", &command))
	{
		command_id = (*parameters->m_commandEncoder)(command);
		if (command_id == -1)
		{
			STOP((
				"%s: {[%s]MatchCommand=%s}: Unknown command!",
				page->GetNotationFile()->GetFileName(),
				page->GetName(),
				command
			));
			result = false;
		}
	}
	*component_stream << command_id;

	//
	//----------------------
	// Read the miss command
	//----------------------
	//
	command_id = -1;
	if (page->GetEntry("MissCommand", &command))
	{
		command_id = (*parameters->m_commandEncoder)(command);
		if (command_id == -1)
		{
			STOP((
				"%s: {[%s]MissCommand=%s}: Unknown command!",
				page->GetNotationFile()->GetFileName(),
				page->GetName(),
				command
			));
			result = false;
		}
	}
	*component_stream << command_id;

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