#include "MW4Headers.hpp"

#include "JumpJet.hpp"
#include "MWTool.hpp"

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	JumpJet__GameModel::ReadAndVerify(
		JumpJet__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 RechargeTimeAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 10.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			if (model->m_rechargeTime <= 0)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]RechargeTime=%f}: value must be > 0!",
					model->m_rechargeTime
				);
				result = false;
			}

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

			break;
		}
	case FlightTimeAttributeID:
		{
			if(!valid_data)
			{
				Scalar value = 8.0f;
				attribute_entry->SetValue(model, &value);
				result = true;
			}
			Scalar temp = model->m_flightTime - model->m_burnTime;
			if (temp <= SMALL)
			{
				_snprintf(
					*error,
					error_buffer,
					"{[GameData]FlightTime=%f}: value must be > %f!",
					model->m_flightTime,
					model->m_burnTime
				);
				result = false;
			}
			else
			{
				Scalar acceleration = g_Gravity*temp*temp / (2.0f*model->m_flightTime - model->m_burnTime);
				if (acceleration <= g_Gravity + SMALL)
				{
					_snprintf(
						*error,
						error_buffer,
						"{[GameData]FlightTime=%f}: flight time must be longer!",
						model->m_flightTime
					);
					result = false;
				}
			}

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

			break;
		}
	}
	return result;
}
