// XmlBaseElement.cpp: implementation of the CXmlBaseElement class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XmlBaseElement.h"
#include "XmlElementsFactory.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CXmlBaseElement::CXmlBaseElement()
{

}

CXmlBaseElement::~CXmlBaseElement()
{

}

cmString CXmlBaseElement::GetName() const
{
	return name_;
}

void CXmlBaseElement::SetName(const cmString& name)
{
	name_ = name;
}

cmString CXmlBaseElement::GetDescription() const
{
	return description_;
}

void CXmlBaseElement::SetDescription(const cmString& description)
{
	description_ = description;
}

enDataType CXmlBaseElement::GetDataType() const
{
	return dataType_;
}

void CXmlBaseElement::SetDataType(enDataType dataType)
{	
	dataType_ = dataType;
}

cmString CXmlBaseElement::GetId() const
{
	return id_;
}

void CXmlBaseElement::SetId(const cmString& id)
{
	id_ = id;
}

cmString CXmlBaseElement::GetPath() const
{
	return path_;
}

void CXmlBaseElement::SetPath(const cmString& path)
{
	path_ = path;
}

bool CXmlBaseElement::BuildFromXml(IXMLDOMNode* node)
{
	Clear();
   
	ExtractMemberNode(node, _T("Name"), name_);
	ExtractMemberNode(node, _T("Description"), description_);
	description_ = EscapeToPlainText(description_);

	ExtractMemberNode(node, _T("DataType"), (int&)dataType_);
	ExtractMemberNode(node, _T("Id"), id_);
	CXmlElementsFactory::UpdateIdCounter(id_);
	ExtractMemberNode(node, _T("Path"), path_);
		
	return true;
}

bool CXmlBaseElement::AppendToDOMDocument(IXMLDOMDocument2* xmlDoc, IXMLDOMElement* parentElement)
{
	IXMLDOMElement* newElement;
	HRESULT hr;

	hr = xmlDoc->createElement(CComBSTR((const TCHAR*)CXmlBaseElement::GetXmlNodeName()), &newElement);
	
	AppendMemberNode(_T("Name"), name_, newElement, xmlDoc);
	AppendMemberNode(_T("Description"), EscapeToXml(description_), newElement, xmlDoc);
	AppendMemberNode(_T("DataType"), (int)dataType_, newElement, xmlDoc);
	AppendMemberNode(_T("DataTypeStr"), DataType2String(dataType_), newElement, xmlDoc);
	AppendMemberNode(_T("Id"), id_, newElement, xmlDoc);
	AppendMemberNode(_T("Path"), path_, newElement, xmlDoc);

	IXMLDOMNode* newOutNode;

	if (parentElement)
		hr = parentElement->appendChild(newElement, &newOutNode);
	else
		hr = xmlDoc->appendChild(newElement, &newOutNode);

	newOutNode->Release();

	return true;
}

void CXmlBaseElement::Clear()
{}

void CXmlBaseElement::SetDefaults()
{
}

cmString CXmlBaseElement::DataType2String(enDataType dataType)
{
	cmString ret;

	switch (dataType) {
		case enDataType_Class:
			ret = _T("Class");
			break;

		case enDataType_Long:
			ret = _T("Long");
			break;

		case enDataType_Double:
			ret = _T("Double");
			break;

		case enDataType_String:
			ret = _T("String");
			break;

		case enDataType_Date:
			ret = _T("Date");
			break;

		case enDataType_Boolean:
			ret = _T("Boolean");
			break;

		default:
			ret = _T("UnknownDataType");
	}

	return ret;
}

cmString CXmlBaseElement::Cardinality2String(enCardinality cardinality)
{
	cmString ret;

	switch(cardinality) {
		case enCardinality_SingleInstance:
			ret = _T("Single Instance");
			break;

		case enCardinality_Array:
			ret = _T("Array");
			break;

		case enCardinality_Map:
			ret = _T("Map");
			break;
	
		default:
			ret = _T("UnknownCardinality");
	}

	return ret;
}

cmString CXmlBaseElement::ValidationMode2String(enValidationMode validationMode)
{
	cmString ret;

	switch (validationMode) {
		case enValidationMode_None:
			ret = _T("None");
			break;

		case enValidationMode_Interval:
			ret = _T("Interval");
			break;

		case enValidationMode_StaticEnum:
			ret = _T("Static Enum");
			break;

		case enValidationMode_DynamicEnum:
			ret = _T("Dynamic Enum");
			break;

		default:
			ret = _T("Unknown Validation Mode");
	}

	return ret;
}

cmString CXmlBaseElement::RenderingMode2String(enRenderingMode renderingMode)
{
	cmString ret;

	switch (renderingMode) {
		case enRenderingMode_None:
			ret = _T("None");
			break;

		case enRenderingMode_FlatList:
			ret = _T("Flat List");
			break;

		case enRenderingMode_CategoryList:
			ret = _T("Category List");
			break;

		case enRenderingMode_Grid:
			ret = _T("Grid");
			break;

		case enRenderingMode_CustomDialog:
			ret = _T("Custom Dialog");
			break;

		default:
			ret = _T("Unknown Rendering Mode");
	}

	return ret;
}

cmString CXmlBaseElement::LongMemberDisplayMode2String(enLongMemberDisplayMode displayMode)
{
	cmString ret;

	switch (displayMode) {
		case enLongMemberDisplayMode_None:
			ret = _T("None");
			break;

		case enLongMemberDisplayMode_Decimal:
			ret = _T("Decimal");
			break;

		case enLongMemberDisplayMode_Hex:
			ret = _T("Hexadecimal");
			break;

		case enLongMemberDisplayMode_Binary:
			ret = _T("Binary");
			break;

		case enLongMemberDisplayMode_Smart:
			ret = _T("Smart");
			break;

		default:
			ret = _T("Unknown Display Mode");
	
	}

	return ret;
}

cmString CXmlBaseElement::StringMemberDisplayMode2String(enStringMemberDisplayMode displayMode)
{
	cmString ret;

	switch (displayMode) {
		case enStringMemberDisplayMode_None:
			ret = _T("None");
			break;

		case enStringMemberDisplayMode_GenericString:
			ret = _T("Generic string");
			break;

		default:
			ret = _T("Unknown Display Mode");
	
	}

	return ret;

}

cmString CXmlBaseElement::DoubleMemberDisplayMode2String(enDoubleMemberDisplayMode displayMode)
{
	cmString ret;

	switch (displayMode) {
		case enDoubleMemberDisplayMode_None:
			ret = _T("None");
			break;

		case enDoubleMemberDisplayMode_GenericNumber:
			ret = _T("Generic number");
			break;

		default:
			ret = _T("Unknown Display Mode");
	
	}

	return ret;
}

cmString CXmlBaseElement::BooleanMemberDisplayMode2String(enBooleanMemberDisplayMode displayMode)
{
	cmString ret;

	switch (displayMode) {
		case enBooleanMemberDisplayMode_None:
			ret = _T("None");
			break;

		case enBooleanMemberDisplayMode_CheckBox:
			ret = _T("Checkbox");
			break;

		default:
			ret = _T("Unknown Display Mode");
	
	}

	return ret;
}

cmString CXmlBaseElement::EscapeToXml(const cmString& str)
{
	cmString ret(str);

	ret.Replace(_T("\r"), _T("&#10;"));
	ret.Replace(_T("\n"), _T("&#13;"));

	return ret;
}

cmString CXmlBaseElement::EscapeToPlainText(const cmString& str)
{
	cmString ret(str);

	ret.Replace(_T("&#10;"), _T("\r"));
	ret.Replace(_T("&#13;"), _T("\n"));

	return ret;

}
