//-----------------------------------------------------------------------------------------------
// GypsyXML.hpp
//
#pragma once
#ifndef include_GypsyXML
#define include_GypsyXML


//-----------------------------------------------------------------------------------------------
#include "Engine/Core/SquirrelXML.hpp"
#include "Engine/Core/MathLibrary.hpp"
#include "Engine/Core/Rgba.hpp"


//-----------------------------------------------------------------------------------------------
void XMLDebugDumpTree( const SquirrelXMLElement& xmlElementAtTop, int indentationLevel=0 );
bool XMLGetCharLiteralAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, char& out_charTextValue );
bool XMLGetTextAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, char* out_textValueBuffer, int bufferSize );
bool XMLGetStringAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, std::string& out_stringValue );
bool XMLGetBoolAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, bool& out_boolValue );
bool XMLGetIntAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, int& out_intValue );
bool XMLGetFloatAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, float& out_floatValue );
bool XMLGetVector2Attribute( const SquirrelXMLElement& xmlElement, const char* attributeName, Vector2& out_vector2Value );
bool XMLGetIntVector2Attribute( const SquirrelXMLElement& xmlElement, const char* attributeName, IntVector2& out_intVector2Value );
bool XMLGetRgbaAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, Rgba& out_rgbaValue );


//-----------------------------------------------------------------------------------------------
inline bool XMLGetCharLiteralAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, char& out_charTextValue)
{
	bool wasFound = xmlElement.GetCharLiteralAttribute( attributeName, out_charTextValue );
	return wasFound;
}


//-----------------------------------------------------------------------------------------------
inline bool XMLGetTextAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, char* out_textValueBuffer, int bufferSize )
{
	bool wasFound = xmlElement.GetTextAttribute( attributeName, out_textValueBuffer, bufferSize );
	return wasFound;
}


//-----------------------------------------------------------------------------------------------
inline bool XMLGetStringAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, std::string& out_stringValue)
{
	bool wasFound = xmlElement.GetTextAttribute( attributeName, out_stringValue );
	return wasFound;
}


//-----------------------------------------------------------------------------------------------
// Sets out_boolValue to true or false, but only if attribute is found (otherwise does not set).
// Returns true if attribute was present, false if absent.
// Boolean attributes are considered true if they begin with 'T' or 't' or '1'.
//
inline bool XMLGetBoolAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, bool& out_boolValue )
{
	const int BUFFER_SIZE = 6;
	char boolText[ BUFFER_SIZE ];
	bool wasFound = xmlElement.GetTextAttribute( attributeName, boolText, BUFFER_SIZE );
	if( wasFound )
	{
		if( boolText[0] == 't' || boolText[0] == 'T' || boolText[0] == '1' )
			out_boolValue = true;
		else
			out_boolValue = false;

		return true; // attribute was found
	}
	else
	{
		return false; // attribute was not found
	}
}


//-----------------------------------------------------------------------------------------------
inline bool XMLGetIntAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, int& out_intValue )
{
	const int BUFFER_SIZE = 16;
	char intText[ BUFFER_SIZE ];
	bool wasFound = xmlElement.GetTextAttribute( attributeName, intText, BUFFER_SIZE );
	if( wasFound )
	{
		out_intValue = atoi( intText );
		return true;
	}
	else
	{
		return false;
	}
}


//-----------------------------------------------------------------------------------------------
inline bool XMLGetFloatAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, float& out_floatValue )
{
	const int BUFFER_SIZE = 16;
	char floatText[ BUFFER_SIZE ];
	bool wasFound = xmlElement.GetTextAttribute( attributeName, floatText, BUFFER_SIZE );
	if( wasFound )
	{
		out_floatValue = (float) atof( floatText );
		return true;
	}
	else
	{
		return false;
	}
}


//-----------------------------------------------------------------------------------------------
inline bool XMLGetVector2Attribute( const SquirrelXMLElement& xmlElement, const char* attributeName, Vector2& out_vector2Value )
{
	const int BUFFER_SIZE = 1024;
	char vector2Text[ BUFFER_SIZE ];
	bool wasFound = xmlElement.GetTextAttribute( attributeName, vector2Text, BUFFER_SIZE );
	if( wasFound )
	{
		out_vector2Value.SetFromText( vector2Text );
		return true;
	}
	else
	{
		return false;
	}
}


//-----------------------------------------------------------------------------------------------
inline bool XMLGetIntVector2Attribute( const SquirrelXMLElement& xmlElement, const char* attributeName, IntVector2& out_intVector2Value )
{
	const int BUFFER_SIZE = 1024;
	char intVector2Text[ BUFFER_SIZE ];
	bool wasFound = xmlElement.GetTextAttribute( attributeName, intVector2Text, BUFFER_SIZE );
	if( wasFound )
	{
		out_intVector2Value.SetFromText( intVector2Text );
		return true;
	}
	else
	{
		return false;
	}
}


//-----------------------------------------------------------------------------------------------
inline bool XMLGetRgbaAttribute( const SquirrelXMLElement& xmlElement, const char* attributeName, Rgba& out_rgbaValue )
{
	const int BUFFER_SIZE = 1024;
	char rgbaText[ BUFFER_SIZE ];
	bool wasFound = xmlElement.GetTextAttribute( attributeName, rgbaText, BUFFER_SIZE );
	if( wasFound )
	{
		out_rgbaValue.SetFromText( rgbaText );
		return true;
	}
	else
	{
		return false;
	}
}


#endif // include_GypsyXML
