// XmlProject.cpp: implementation of the CXmlProject class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XmlProject.h"
#include "XmlClassInfo.h"
#include "XmlElementsFactory.h"

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

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

CXmlProject::CXmlProject():
	rootElement_(NULL)
{
	Clear();
}

CXmlProject::~CXmlProject()
{
	if (rootElement_)
		CXmlElementsFactory::DestroyXmlElement(rootElement_);
}

CXmlProjectSettings* CXmlProject::GetProjectSettings()
{
	return &projectSettings_;
}

CXmlClassInfo* CXmlProject::GetRootElement()
{
	return rootElement_;
}

void CXmlProject::Clear()
{
	if (rootElement_)
		CXmlElementsFactory::DestroyXmlElement(rootElement_);
	
	rootElement_ = dynamic_cast<CXmlClassInfo*>(CXmlElementsFactory::CreateXmlElement(enDataType_Class));
	rootElement_->Clear();
	rootElement_->SetName(_T("Root"));
	
	icons_.Clear();

	projectSettings_.Clear();
	mainDialog_.Clear();
}

void CXmlProject::SetDefaults()
{
	projectSettings_.SetDefaults();
	icons_.SetDefaults();
	mainDialog_.SetDefaults();
}

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

	ExtractMemberNode(node, CXmlProjectSettings::GetXmlNodeName(), projectSettings_);
	ExtractMemberNode(node, CXmlClassInfo::GetXmlNodeName(), *rootElement_);
	ExtractMemberNode(node, CXmlProjectIcons::GetXmlNodeName(), icons_);
	ExtractMemberNode(node, CXmlMainDialog::GetXmlNodeName(), mainDialog_);

	return true;
}

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

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

	projectSettings_.AppendToDOMDocument(xmlDoc, newElement);
	rootElement_->AppendToDOMDocument(xmlDoc, newElement);
	icons_.AppendToDOMDocument(xmlDoc, newElement);
	mainDialog_.AppendToDOMDocument(xmlDoc, newElement);

	IXMLDOMNode* newOutNode;

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

bool CXmlProject::IsEmpty()
{
	return rootElement_->GetMembers()->size() == 0;
}

CXmlProjectIcons* CXmlProject::GetProjectIcons()
{
	return &icons_;
}

CXmlMainDialog* CXmlProject::GetMainDialog()
{
	return &mainDialog_;
}
