// CXmlBaseFormItem.cpp
//
#include "stdafx.h"
#include "XmlBaseFormItem.h"

CXmlBaseFormItem::CXmlBaseFormItem():id_(0)
{}

CXmlBaseFormItem::~CXmlBaseFormItem()
{}

bool CXmlBaseFormItem::BuildFromXml(IXMLDOMNode* node)
{
	Clear();

	ExtractMemberNode(node, _T("Name"), name_);
	ExtractMemberNode(node, _T("Id"), (int&)id_);	
	ExtractMemberNode(node, _T("RelatedClassMemberName"), relatedClassMemberName_);
	ExtractMemberNode(node, _T("RelatedClassMemberid"), relatedClassMemberId_);
	//ExtractMemberNode(node, _T("Rect"), itemRect_);

	return true;
}

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

	hr = xmlDoc->createElement(CComBSTR((const TCHAR*)GetXmlNodeName()), &newElement);
   
	AppendMemberNode(_T("Name"), name_, newElement, xmlDoc);
	AppendMemberNode(_T("Id"), (long)id_, newElement, xmlDoc);
	AppendMemberNode(_T("RelatedClassMemberName"), relatedClassMemberName_, newElement, xmlDoc);
	AppendMemberNode(_T("RelatedClassMemberId"), relatedClassMemberId_, newElement, xmlDoc);

	hr = xmlDoc->createElement(_T("Rect"), &rectElement);

	AppendMemberNode(_T("Left"), itemRect_.left, rectElement, xmlDoc);
	AppendMemberNode(_T("Right"), itemRect_.right, rectElement, xmlDoc);
	AppendMemberNode(_T("Top"), itemRect_.top, rectElement, xmlDoc);
	AppendMemberNode(_T("Bottom"), itemRect_.bottom, rectElement, xmlDoc);
	
	IXMLDOMNode* outNewNode;

	hr = newElement->appendChild(rectElement, &outNewNode);
	outNewNode->Release();

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

	return true;
}


CRect CXmlBaseFormItem::GetItemRect()
{
	return itemRect_;
}

void CXmlBaseFormItem::SetItemRect(const CRect& itemRect)
{
	itemRect_ = itemRect;
}

cmString CXmlBaseFormItem::GetRelatedClassMemberId()
{
	return relatedClassMemberId_;
}

void CXmlBaseFormItem::SetRelatedClassMemberId(const cmString& id)
{
	relatedClassMemberId_ = id;
}

cmString CXmlBaseFormItem::GetName()
{
	return name_;
}

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

bool CXmlBaseFormItem::ReadRectNode(IXMLDOMNode* rectNode)
{
	USES_CONVERSION;

	IXMLDOMNode* childNode;
	IXMLDOMNodeList* children;
	CComBSTR nodeName;
	CComVariant nodeValue;
	HRESULT hRes;
	long size;

	hRes = rectNode->get_childNodes(&children);
	children->get_length(&size);

	for (long i = 0; i < size; i++) {
		children->get_item(i, &childNode);
			
		hRes = childNode->get_nodeName(&nodeName);

		if ((BSTR)nodeName == _T("Left")) {

			hRes = childNode->get_nodeTypedValue(&nodeValue);
			itemRect_.left = _wtoi(nodeValue.bstrVal);

		} else if((BSTR)nodeName == _T("Right")) {

			hRes = childNode->get_nodeTypedValue(&nodeValue);
			itemRect_.right = _wtoi(nodeValue.bstrVal);

		} else if((BSTR)nodeName == _T("Top")) {

			hRes = childNode->get_nodeTypedValue(&nodeValue);
			itemRect_.top = _wtoi(nodeValue.bstrVal);
		} else if((BSTR)nodeName == _T("Bottom")) {

			hRes = childNode->get_nodeTypedValue(&nodeValue);
			itemRect_.bottom = _wtoi(nodeValue.bstrVal);
		}
	}

	return true;
}

void CXmlBaseFormItem::SetTop(int top)
{
	int height;

	height = itemRect_.Height();

	itemRect_.top = top;
	itemRect_.bottom = itemRect_.top + height;
}

void CXmlBaseFormItem::SetLeft(int left)
{
	int width;

	width = itemRect_.Width();

	itemRect_.left = left;
	itemRect_.right = itemRect_.left + width;
}

void CXmlBaseFormItem::SetWidth(int width)
{
	itemRect_.right = itemRect_.left + width;
}

void CXmlBaseFormItem::SetHeight(int height)
{
	itemRect_.bottom = itemRect_.top + height;
}

cmString CXmlBaseFormItem::GetRelatedClassMemberName()
{
	return relatedClassMemberName_; 
}

void CXmlBaseFormItem::SetRelatedClassMemberName(const cmString& classMemberName)
{
	relatedClassMemberName_ = classMemberName;
}

void CXmlBaseFormItem::Clear()
{}

void CXmlBaseFormItem::SetDefaults()
{}
