#include "AdeptHeaders.hpp"

#include "ComponentHeaders.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
RandomizerOf<Scalar>::ClassData*
	RandomizerOf<Scalar>::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(RandomizerOf<Scalar>));
	bool result =
		ChannelOf<Scalar>::CreateFactoryRequest(parameters) != NULL;

	//
	//------------------------------------------------------------------------
	// Find all the input lines in the page and determine the component ids of
	// each of them
	//------------------------------------------------------------------------
	//
	Page *page = parameters->m_page;
	Check_Object(page);
	Scalar minimum;
	page->GetEntry("MinimumValue", &minimum, true);
	*component_stream << minimum;
	Scalar range;
	page->GetEntry("MaximumValue", &range, true);
	if (range < minimum)
	{
		STOP((
			"%s: {[%s]MaximumValue=%f}: MaximumValue must not be less than Minimum Value!",
			page->GetNotationFile()->GetFileName(),
			page->GetName(),
			range
		));
		result = false;
		range = minimum;
	}
	else
	{
		range -= minimum;
		*component_stream << range;
	}

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

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

	//
	//------------------------------------------------------------------------
	// Find all the input lines in the page and determine the component ids of
	// each of them
	//------------------------------------------------------------------------
	//
	Page *page = parameters->m_page;
	Check_Object(page);
	int minimum;
	page->GetEntry("MinimumValue", &minimum, true);
	*component_stream << minimum;

	int range;
	page->GetEntry("MaximumValue", &range, true);
	if (range < minimum)
	{
		STOP((
			"%s: {[%s]MaximumValue=%d}: MaximumValue must not be less than Minimum Value!",
			page->GetNotationFile()->GetFileName(),
			page->GetName(),
			range
		));
		result = false;
		range = minimum;
	}
	else
	{
		range -= minimum - 1;
		*component_stream << range;
	}

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