#include "MW4Headers.hpp"

#include "Objective.hpp"
#include "MWTool.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void Objective__CreateMessage::ConstructCreateMessage(Script *script)
{
	Check_Object(script);
	#if defined(_ARMOR)
		int stack_level = Tool::Instance->GetStackLevel();
	#endif
	//
	//--------------
	// Set up stream
	//--------------
	//

	MemoryStream *message_stream = script->messageStream;
	Check_Object(message_stream);
	message_stream->AllocateBytes(sizeof(Objective__CreateMessage));
	Entity__CreateMessage::ConstructCreateMessage(script);
	Objective__CreateMessage *message =
		Cast_Pointer(
			Objective__CreateMessage*,
			message_stream->GetPointer()
		);
	message->messageLength = sizeof(*message);

	//
	//---------------------------
	// Point at the notation file
	//---------------------------
	//
	Page *page = script->instancePage;
	Check_Object(page);
	NotationFile *instance_file = page->GetNotationFile();
	Check_Object(instance_file);
	Check_Object(Tool::Instance);
	Tool::Instance->PushFilePath(instance_file);

	message->primaryObjective = true;
	message->visibleObjective = false;
	message->helpMessage = false;
	message->objectiveState =	Objective::Objective_Neutral;

	message->failtextID = 0;
	message->succeedtextID = 0;
	message->neutraltextID = 0;
	message->descriptiontextID = 0;

	message->failtext[0] = 0;
	message->succeedtext[0] = 0;
	message->neutraltext[0] = 0;
	message->descriptiontext[0] = 0;

	page->GetEntry("Number", &message->objectiveID,true);
	page->GetEntry("Primary", &message->primaryObjective,false);
	page->GetEntry("Visible", &message->visibleObjective,false);
	page->GetEntry("Help", &message->helpMessage,false);
	page->GetEntry("State", &message->objectiveState,false);

	page->GetEntry("failtext",&message->failtextID,false);
	page->GetEntry("succeedtext",&message->succeedtextID,false);
	page->GetEntry("neutraltext",&message->neutraltextID,false);
	page->GetEntry("descriptext",&message->descriptiontextID,false);

	const char *temptext;
	if (page->GetEntry("failtextstr",&temptext,false) && temptext)
		Str_Copy (message->failtext,temptext,sizeof (message->failtext));
	if (page->GetEntry("succeedtextstr",&temptext,false) && temptext)
		Str_Copy (message->succeedtext,temptext,sizeof (message->succeedtext));
	if (page->GetEntry("neutraltextstr",&temptext,false) && temptext)
		Str_Copy (message->neutraltext,temptext,sizeof (message->neutraltext));
	if (page->GetEntry("descriptextstr",&temptext,false) && temptext)
		Str_Copy (message->descriptiontext,temptext,sizeof (message->descriptiontext));

	Tool::Instance->PopFilePath();
	Verify(stack_level == Tool::Instance->GetStackLevel());
}

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

	Entity::SaveInstanceText(instance_page);

	// Appended by SMJ 10/4/99
	instance_page->AppendEntry("Number",m_ObjectiveID);
	instance_page->AppendEntry("Primary",m_Primary);
	instance_page->AppendEntry("Visible",m_VisibleObjective);
	instance_page->AppendEntry("Help",m_HelpMessage);
	instance_page->AppendEntry("State",m_ObjectiveState);

	instance_page->AppendEntry("failtext",-1);
	instance_page->AppendEntry("succeedtext",-1);
	instance_page->AppendEntry("neutraltext",-1);
	instance_page->AppendEntry("descriptext",-1);

	instance_page->AppendEntry("@failtextstr",m_LoadedFailText);
	instance_page->AppendEntry("@succeedtextstr",m_LoadedSucceedText);
	instance_page->AppendEntry("@neutraltextstr",m_LoadedNeutralText);
	instance_page->AppendEntry("@descriptextstr",m_LoadedDescriptionText);

	int execution_state = executionState->GetState();
	MString execution_state_text;
	switch(execution_state)
	{
	case ExecutionStateEngine::RevealedState:
		{
			execution_state_text = "RevealedState";
			break;
		}
	}
	if (execution_state_text)
	{
		instance_page->SetEntry("ExecutionState", execution_state_text);
	}
}