#include "xmlfontstring.h"

// ----------------------------------------------------------------------------
CXmlShadow::CXmlShadow()
{
}

// ----------------------------------------------------------------------------
bool CXmlShadow::LoadFromDOMNode(const DOMNode* node)
{
	if ( node == NULL || node->getNodeType() != DOMNode::ELEMENT_NODE )
		return false;

	// Element
	node = node->getFirstChild();
	while ( node )
	{
		if ( node->getNodeType() == DOMNode::ELEMENT_NODE )
		{
			char* nodeName = XMLString::transcode(node->getNodeName());

			// Color
			if ( XMLString::compareString(nodeName, "Color") == 0 )
			{
				m_color.LoadFromDOMNode(node);
			}
			// Offset
			else if ( XMLString::compareString(nodeName, "Offset") == 0 )
			{
				m_offset.LoadFromDOMNode(node);
			}
			XMLString::release(&nodeName);
		}
		node = node->getNextSibling();
	}

	return true;
}

// ----------------------------------------------------------------------------
CXmlFontString::CXmlFontString(CXmlUi* pXmlUi, CXmlLayout* pParent)
	: CXmlLayout(pXmlUi, pParent)
{
	m_type				= eXmlUiType_FontString;

	m_font.clear();
	m_text.clear();
	m_pShadow			= NULL;
	m_weight			= eFontWeight_Normal;
	m_bytes				= 255;
	m_spacing			= 0.0f;
	m_justifyV			= eJustifyVType_Middle;
	m_justifyH			= eJustifyHType_Center;
}

// ----------------------------------------------------------------------------
CXmlFontString::~CXmlFontString()
{
	XML_SAFE_DLETE(m_pShadow);
}

// ----------------------------------------------------------------------------
int CXmlFontString::XmlValueToJustifyVType( const char* valueStr )
{
	if ( valueStr )
	{
		if ( XMLString::compareString(valueStr, "TOP") == 0 )
			return eJustifyVType_Top;
		else if ( XMLString::compareString(valueStr, "MIDDLE") == 0 )
			return eJustifyVType_Middle;
		else if ( XMLString::compareString(valueStr, "BOTTOM") == 0 )
			return eJustifyVType_Bottom;
	}
	return eJustifyVType_Top;
}

// ----------------------------------------------------------------------------
int CXmlFontString::XmlValueToJustifyHType( const char* valueStr )
{
	if ( valueStr )
	{
		if ( XMLString::compareString(valueStr, "LEFT") == 0 )
			return eJustifyHType_Left;
		else if ( XMLString::compareString(valueStr, "CENTER") == 0 )
			return eJustifyHType_Center;
		else if ( XMLString::compareString(valueStr, "RIGHT") == 0 )
			return eJustifyHType_Right;
	}
	return eJustifyVType_Top;
}

// ----------------------------------------------------------------------------
int	CXmlFontString::XmlValueToFontWeight( const char* valueStr )
{
	if ( valueStr )
	{
		if ( XMLString::compareString(valueStr, "THIN") == 0 )
			return eFontWeight_Thin;
		else if ( XMLString::compareString(valueStr, "NORMAL") == 0 )
			return eFontWeight_Normal;
		else if ( XMLString::compareString(valueStr, "BOLD") == 0 )
			return eFontWeight_Bold;
	}
	return eFontWeight_Normal;
}

// ----------------------------------------------------------------------------
void CXmlFontString::LoadNodeAttribute(const DOMNamedNodeMap* nodeMap)
{
	CXmlLayout::LoadNodeAttribute(nodeMap);

	const XMLCh* nodeValue;
	if ( (nodeValue = GetNodeAttribute(nodeMap, "font")) != NULL )
	{
		char* tempStr = XMLString::transcode(nodeValue);
		m_font = tempStr;
		XMLString::release(&tempStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "text")) != NULL )
	{
		char* tempStr = XMLString::transcode(nodeValue);
		m_text = tempStr;
		XMLString::release(&tempStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "bytes")) != NULL )
	{
		char* tempStr = XMLString::transcode(nodeValue);
		m_bytes = atoi(tempStr);
		XMLString::release(&tempStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "spacing")) != NULL )
	{
		char* tempStr = XMLString::transcode(nodeValue);
		m_spacing = (float)atof(tempStr);
		XMLString::release(&tempStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "outline")) != NULL )
	{
		int a = 0;
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "weight")) != NULL )
	{	
		char* tempStr = XMLString::transcode(nodeValue);
		m_weight = XmlValueToFontWeight(tempStr);
		XMLString::release(&tempStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "justifyV")) != NULL )
	{
		char* tempStr = XMLString::transcode(nodeValue);
		m_justifyV = XmlValueToJustifyVType(tempStr);
		XMLString::release(&tempStr);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "justifyH")) != NULL )
	{
		char* tempStr = XMLString::transcode(nodeValue);
		m_justifyH = XmlValueToJustifyHType(tempStr);
		XMLString::release(&tempStr);
	}
}

// ----------------------------------------------------------------------------
void CXmlFontString::LoadNodeElement(const DOMNode* node, const char* nodeName)
{
	CXmlLayout::LoadNodeElement(node, nodeName);

	// Color
	if ( XMLString::compareString(nodeName, "Color") == 0 )
	{
		m_color.LoadFromDOMNode(node);
	}
	// Font Height
	else if ( XMLString::compareString(nodeName, "FontHeight") == 0 )
	{
		m_height.LoadFromDOMNode(node);
	}
	// Shadow
	else if ( XMLString::compareString(nodeName, "Shadow") == 0 )
	{
		XML_SAFE_DLETE( m_pShadow );
		m_pShadow = new CXmlShadow;
		m_pShadow->LoadFromDOMNode(node);
	}
}

// ----------------------------------------------------------------------------
void CXmlFontString::Inherit(CXmlLayout* pXmlLayout)
{
	CXmlLayout::Inherit(pXmlLayout);
	CXmlFontString* pXmlFontString = dynamic_cast<CXmlFontString*>(pXmlLayout);

	if (pXmlFontString)
	{
		if (pXmlFontString->m_pShadow)
		{
			XML_SAFE_DLETE( m_pShadow );
			m_pShadow = new CXmlShadow;
			*m_pShadow = *pXmlFontString->m_pShadow;
		}
		
		m_color			= pXmlFontString->m_color;
		m_height		= pXmlFontString->m_height;
		m_font			= pXmlFontString->m_font;
		m_text			= pXmlFontString->m_text;
		m_weight		= pXmlFontString->m_weight;	
		m_bytes			= pXmlFontString->m_bytes;
		m_spacing		= pXmlFontString->m_spacing;
		m_justifyV		= pXmlFontString->m_justifyV;
		m_justifyH		= pXmlFontString->m_justifyH;
	}
}