//-----------------------------------------------------------------------------------------------
// GypsyXML.cpp
//
#include "GypsyPrecompiledHeader.hpp"
#include "Engine/Core/GypsyXML.hpp"


//-----------------------------------------------------------------------------------------------
void XMLDebugPrintIndendation( int indentationLevel )
{
	const char* indentationText = "    ";
	for( int i = 0; i < indentationLevel; ++ i )
	{
		DebuggerPrintf( "%s", indentationText );
	}
}


//-----------------------------------------------------------------------------------------------
void XMLDebugDumpTree( const SquirrelXMLElement& xmlElement, int indentationLevel )
{
	const int MAX_XML_ATTRIBUTE_DATA_LENGTH = 1024;
	const int MAX_XML_LARGE_BUFFER_LENGTH = 65535;
	char elementNameBuffer[ MAX_XML_ELEMENT_NAME_LENGTH ];
	char attributeNameBuffer[ MAX_XML_ATTRIBUTE_NAME_LENGTH ];
	char attributeDataBuffer[ MAX_XML_ATTRIBUTE_DATA_LENGTH ];
	char largeBuffer[ MAX_XML_LARGE_BUFFER_LENGTH ];

	xmlElement.GetElementName( elementNameBuffer, sizeof( elementNameBuffer ) );
// 	xmlElement.GetPCDataAsText( largeBuffer, sizeof( largeBuffer ) );
// 	if( largeBuffer[0] == '\0' )
// 	{
// 		DebuggerPrintf( "\n==================================================================\n" );
// 		DebuggerPrintf( "[No PCData for element \"%s\"]\n", elementNameBuffer );
// 	}
// 	else
// 	{
// 		DebuggerPrintf( "\n==================================================================\n" );
// 		DebuggerPrintf( "PCData for element \"%s\" was \"%s\"\n", elementNameBuffer, largeBuffer );
// 		DebuggerPrintf( "----- (END OF PCDATA) -----\n" );
// 	}

	xmlElement.GetCommentBeforeAsText( largeBuffer, sizeof( largeBuffer ) );
	if( largeBuffer[0] != '\0' )
	{
		XMLDebugPrintIndendation( indentationLevel );
		DebuggerPrintf( "<!--%s-->\n", largeBuffer );
	}

	XMLDebugPrintIndendation( indentationLevel );
	DebuggerPrintf( "[L=%i,c=%i]", 1+xmlElement.GetLineIndex(), 1+xmlElement.GetCharIndexOnLine() ); // print: <Element
	DebuggerPrintf( "<%s", elementNameBuffer ); // print: <Element

	SquirrelXMLAttribute* nextAttribute = xmlElement.GetFirstAttribute();
	while( nextAttribute )
	{
		nextAttribute->GetAttributeName( attributeNameBuffer, sizeof( elementNameBuffer ) );
		nextAttribute->GetAttributeDataAsText( attributeDataBuffer, sizeof( attributeDataBuffer ) );
//		DebuggerPrintf( " %s=\"%s\"", attributeNameBuffer, attributeDataBuffer ); // print: attrib="data"
		DebuggerPrintf( "[L=%i,c=%i]", 1+nextAttribute->GetLineIndex(), 1+nextAttribute->GetCharIndexOnLine() ); // print: <Element
		DebuggerPrintf( "%s=\"%s\"", attributeNameBuffer, attributeDataBuffer ); // print: attrib="data"
		nextAttribute = nextAttribute->GetNextSiblingAttribute();
	}

	SquirrelXMLElement* firstChildElement = xmlElement.GetFirstChildElement();

	xmlElement.GetPCDataAsText( largeBuffer, sizeof( largeBuffer ) );
	bool hasPCDataWithin = (largeBuffer[0] != '\0');
	bool hasChildElements = (firstChildElement != nullptr);
	if( hasPCDataWithin || hasChildElements )
	{
		DebuggerPrintf( ">" ); // print: >
		if( hasChildElements )
		{
			DebuggerPrintf( "\n" );
			SquirrelXMLElement* nextChildElement = firstChildElement;
			while( nextChildElement )
			{
				XMLDebugDumpTree( *nextChildElement, indentationLevel + 1 );
				nextChildElement = nextChildElement->GetNextSiblingElement();
			}

			XMLDebugPrintIndendation( indentationLevel );
		}

		DebuggerPrintf( "</%s>\n", elementNameBuffer ); //  </Element>
	}
	else
	{
		DebuggerPrintf( "/>\n" ); //  />  (no children - show as empty tag element)
	}
}


