// XmlMemberInfo.cpp: implementation of the CXmlMemberInfo class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XmlMemberInfo.h"

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

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

CXmlMemberInfo::CXmlMemberInfo():
	persistenceMode_(enPersistenceMode_AsChildNode),
	decimalPlaces_(2),
	digitPlaces_(1),
	maxChar_(255),
	readOnly_(false)
{

}

CXmlMemberInfo::~CXmlMemberInfo()
{

}

CXmlMemberValidation* CXmlMemberInfo::GetValidationInfo()
{
	return &validationInfo_;
}

enPersistenceMode CXmlMemberInfo::GetPersistenceMode() const
{
	return persistenceMode_;
}

void CXmlMemberInfo::SetPersistenceMode(enPersistenceMode persistenceMode)
{
	persistenceMode_ = persistenceMode;
}

int CXmlMemberInfo::GetDecimalPlaces() const
{
	return decimalPlaces_;
}

void CXmlMemberInfo::SetDecimalPlaces(int decimalPlaces)
{
	decimalPlaces_ = decimalPlaces;

	validationInfo_.GetIntervalInfo()->SetPrecision(decimalPlaces_);
}

int CXmlMemberInfo::GetDigitPlaces() const
{
	return digitPlaces_;
}

void CXmlMemberInfo::SetDigitPlaces(int digitPlaces)
{
	digitPlaces_ = digitPlaces;
}

int CXmlMemberInfo::GetMaxChar() const
{
	return maxChar_;
}

void CXmlMemberInfo::SetMaxChar(int maxChar)
{
	maxChar_ = maxChar;
}

cmString CXmlMemberInfo::GetCaption() const
{
	return caption_;
}

void CXmlMemberInfo::SetCaption(const cmString& caption)
{
	caption_ = caption;
}

bool CXmlMemberInfo::BuildFromXml(IXMLDOMNode* node)
{

	Clear();

	IXMLDOMNode* tmp;
	cmString dummy;

	tmp = GetChildNode(node, CXmlBaseElement::GetXmlNodeName(), dummy);
	assert(tmp);
	if (tmp) {
		CXmlBaseElement::BuildFromXml(tmp);
		tmp->Release();
	}

	ExtractMemberNode(node, _T("PersistanceMode"), (int&)persistenceMode_);
	ExtractMemberNode(node, _T("DecimalPlaces"), decimalPlaces_);
	ExtractMemberNode(node, _T("DigitPlaces"), digitPlaces_);
	ExtractMemberNode(node, _T("MaxChar"), maxChar_);
	ExtractMemberNode(node, _T("Caption"), caption_);
	ExtractMemberNode(node, _T("MultilineString"), multilineString_);
	ExtractMemberNode(node, _T("ReadOnly"), readOnly_);
	ExtractMemberNode(node, CXmlMemberValidation::GetXmlNodeName(), validationInfo_);
	ExtractMemberNode(node, CXmlMemberGui_Long::GetXmlNodeName(), longMemberGui_);
	ExtractMemberNode(node, CXmlMemberGui_String::GetXmlNodeName(), stringMemberGui_);
	ExtractMemberNode(node, CXmlMemberGui_Double::GetXmlNodeName(), doubleMemberGui_);
	ExtractMemberNode(node, CXmlMemberGui_Boolean::GetXmlNodeName(), booleanMemberGui_);
	
	return true;
}

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

	hr = xmlDoc->createElement(CComBSTR((const TCHAR*)GetXmlNodeName()), &newElement);

	ret = CXmlBaseElement::AppendToDOMDocument(xmlDoc, newElement);
	if (!ret)
		return false;

	AppendMemberNode(_T("PersistanceMode"), persistenceMode_, newElement, xmlDoc);
	AppendMemberNode(_T("DecimalPlaces"), decimalPlaces_, newElement, xmlDoc);
	AppendMemberNode(_T("DigitPlaces_"), digitPlaces_, newElement, xmlDoc);
	AppendMemberNode(_T("MaxChar"), maxChar_, newElement, xmlDoc);
	AppendMemberNode(_T("Caption"), caption_, newElement, xmlDoc);
	AppendMemberNode(_T("MultilineString"), multilineString_, newElement, xmlDoc);
	AppendMemberNode(_T("ReadOnly"), readOnly_, newElement, xmlDoc);

	validationInfo_.AppendToDOMDocument(xmlDoc, newElement);
	switch (dataType_) {
		case enDataType_Long:
			longMemberGui_.AppendToDOMDocument(xmlDoc, newElement);
			break;

		case enDataType_String:
			stringMemberGui_.AppendToDOMDocument(xmlDoc, newElement);
			break;

		case enDataType_Double:
			doubleMemberGui_.AppendToDOMDocument(xmlDoc, newElement);
			break;

		case enDataType_Boolean:
			booleanMemberGui_.AppendToDOMDocument(xmlDoc, newElement);
			break;
	}
	// append node to parent element
	//
	IXMLDOMNode* newOutNode;

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

	return ret;
}

bool CXmlMemberInfo::GetMultilineString() const
{
	return multilineString_;
}

void CXmlMemberInfo::SetMultilineString(bool multilineString)
{
	multilineString_ = multilineString;
}

CXmlMemberGui_Long* CXmlMemberInfo::GetLongMemberGuiInfo()
{
	return &longMemberGui_;
}

CXmlMemberGui_String* CXmlMemberInfo::GetStringMemberGuiInfo()
{
	return &stringMemberGui_;
}

CXmlMemberGui_Double* CXmlMemberInfo::GetDoubleMemberGuiInfo()
{
	return &doubleMemberGui_;
}

CXmlMemberGui_Boolean* CXmlMemberInfo::GetBooleanMemberGuiInfo()
{
	return &booleanMemberGui_;
}

bool CXmlMemberInfo::IsHidden()
{
	bool ret = true;

	switch (dataType_) {
		case enDataType_String:
			ret = (stringMemberGui_.GetDisplayMode() == enStringMemberDisplayMode_None);
			break;

		case enDataType_Long:
			ret = (longMemberGui_.GetDisplayMode() == enLongMemberDisplayMode_None);
			break;

		case enDataType_Double:
			ret = (doubleMemberGui_.GetDisplayMode() == enDoubleMemberDisplayMode_None);
			break;

		case enDataType_Boolean:
			ret = (booleanMemberGui_.GetDisplayMode() == enBooleanMemberDisplayMode_None);
			break;
	}

	return ret;
}

void CXmlMemberInfo::SetReadOnly(bool readOnly)
{
	readOnly_ = readOnly;
}

bool CXmlMemberInfo::IsReadOnly() const
{
	return readOnly_;
}