#include "MW4Headers.hpp"

#include "EffectGenerator.hpp"
#include "MWTool.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	EffectGenerator__GameModel::ConstructGameModel(Script *script)
{
	Check_Object(script);

	//
	//--------------
	// Set up stream
	//--------------
	//
	MemoryStream *model_stream = script->modelStream;
	Check_Object(model_stream);
	model_stream->AllocateBytes(sizeof(EffectGenerator__GameModel));
	Entity__GameModel::ConstructGameModel(script);
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	EffectGenerator__CreateMessage::ConstructCreateMessage(Script *script)
{
	Check_Object(script);

	//
	//--------------
	// Set up stream
	//--------------
	//
	MemoryStream *message_stream = script->messageStream;
	Check_Object(message_stream);
	message_stream->AllocateBytes(sizeof(EffectGenerator__CreateMessage));
	BaseClass::ConstructCreateMessage(script);
	EffectGenerator__CreateMessage *message =
		Cast_Pointer(
			EffectGenerator__CreateMessage*,
			message_stream->GetPointer()
		);
	message->messageLength = sizeof(*message);

	//
	//---------------------------------------------------------------
	// Point at the notation file and make other files relative to it
	//---------------------------------------------------------------
	//
	Page *instance_page = script->instancePage;
	Check_Object(instance_page);
	NotationFile *instance_file = instance_page->GetNotationFile();
	Check_Object(instance_file);
	Check_Object(Tool::Instance);
	Tool::Instance->PushFilePath(instance_file);
	Check_Object(Resource::ParentFileDependencies);
	message->effectResourceID=ResourceID::Null;

	const char *effect_res_name;
	instance_page->GetEntry("Effect", &effect_res_name, true);
	Resource effect_res(effect_res_name);
	if(!effect_res.DoesResourceExist())
		STOP(("Resource %s Does Not Exist in Build",effect_res_name));
	message->effectResourceID=effect_res.GetResourceID();
	message->timeDelay=1.0f;
	message->m_nightOnly=false;
	message->m_randomizeTime=false;

	instance_page->GetEntry("TimeDelay", &(message->timeDelay));
	instance_page->GetEntry("NightOnly", &(message->m_nightOnly));
	instance_page->GetEntry("RandomizeTime",&(message->m_randomizeTime));

	Tool::Instance->PopFilePath();
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	EffectGenerator__GameModel::ReadAndVerify(
		EffectGenerator__GameModel *model,
		ModelAttributeEntry *attribute_entry, 
		const char *data,
		char **error,
		int error_buffer
	)
{
	Check_Object(attribute_entry);
	
	bool result = false;

	//
	//----------------------------------------
	//Read in the values from the data entered
	//----------------------------------------
	//
	result = 
		Entity__GameModel::ReadAndVerify(
			model, 
			attribute_entry, 
			data,
			error,
			error_buffer
		);
	return result;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	
void
	EffectGenerator::SaveInstanceText(Page *instance_page)
{
	Check_Object(this);
	Check_Object(instance_page);

	BaseClass::SaveInstanceText(instance_page);
	Resource effect_resource(effectResourceID);
	Verify(effect_resource.DoesResourceExist());
	instance_page->SetEntry("Effect", effect_resource.GetName());
	instance_page->SetEntry("TimeDelay", timeDelay);
	instance_page->SetEntry("NightOnly", m_nightOnly);
	instance_page->SetEntry("RandomizeTime", m_randomizeTime);

}