#include "MW4Headers.hpp"

#include "Engine.hpp"
#include "MWTool.hpp"

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

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

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

	message->m_engineUpgrades = 0;
	page->GetEntry("EngineUpgrades", &message->m_engineUpgrades);
}

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

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

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

	bool result = false;
	bool valid_data = (*data != '\0');

	//
	//----------------------------------------
	//Read in the values from the data entered
	//----------------------------------------
	//
	result = 
		BaseClass::ReadAndVerify(
			model, 
			attribute_entry, 
			data,
			error,
			error_buffer
		);
	//
	//---------------------------
	//Verify all the model values
	//---------------------------
	//
	switch(attribute_entry->attributeID)
	{
	case TonsPerUpgradeAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 1.0;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if (model->m_tonsPerUpgrade <= 0.0f)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]TonsPerUpgrade=%f}: value must be > 0!",
					model->m_tonsPerUpgrade
				);
				result = false;
			}

			break;
		}
	case MPSPerUpgradeAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 5.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if (model->m_mpsPerUpgrade <= 0.0f)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]MPSPerUpgrade=%f}: value must be > 0!",
					model->m_mpsPerUpgrade
				);
				result = false;
			}

			break;
		}
	case NumHeatSinksAttributeID:
		{
			if(!valid_data)
			{
				int value = 10;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if (model->m_numHeatSinks < 0)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]NumHeatSinks=%f}: value must be >= 0!",
					model->m_numHeatSinks
				);
				result = false;
			}

			break;
		}
	case HeatSinkEfficiencyAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 1.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if (model->m_heatSinkEfficiency < 0)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]HeatSinkEfficiency=%f}: value must be >= 0.0!",
					model->m_heatSinkEfficiency
				);
				result = false;
			}

			break;
		}
	}
	return result;
}
