#include "MW4Headers.hpp"

#include "Beagle.hpp"
#include "MWTool.hpp"

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	Beagle__GameModel::ReadAndVerify(
		Beagle__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 = 
		Subsystem__GameModel::ReadAndVerify(
			model, 
			attribute_entry, 
			data,
			error,
			error_buffer
		);
	//
	//---------------------------
	//Verify all the model values
	//---------------------------
	//

	switch(attribute_entry->attributeID)
	{
	case TargeterMissileLockPercentAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 0.3f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if(model->m_targeterMissileLockPercent < 0.0f)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]TargeterMissileLockPercent=%f}: value must be >= 0!",
					model->m_targeterMissileLockPercent
				);
				result = false;
			}
			if(model->m_targeterMissileLockPercent > 1.0f)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]TargeterMissileLockPercent=%f}: value must be <= 1.0!",
					model->m_targeterMissileLockPercent
				);
				result = false;
			}
			break;
		}
	case HolderMissileLockPercentAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 0.3f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if(model->m_holderMissileLockPercent < 0.0f)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]HolderMissileLockPercent=%f}: value must be >= 0!",
					model->m_holderMissileLockPercent
				);
				result = false;
			}
			if(model->m_holderMissileLockPercent > 1.0f)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]HolderMissileLockPercent=%f}: value must be >= 1.0!",
					model->m_holderMissileLockPercent
				);
				result = false;
			}
			break;
		}
	}
	return result;
}
