// XmlElementsFactory.cpp: implementation of the CXmlElementsFactory class.
//
//////////////////////////////////////////////////////////////////////

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

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

int CXmlElementsFactory::idCounter_ = 0;

CXmlBaseElement* CXmlElementsFactory::CreateXmlElement(enDataType dataType)
{
	CXmlBaseElement* newElement;

	switch (dataType) {
		case enDataType_Class:
			newElement = new CXmlClassInfo;
			break;

		default:
			newElement = new CXmlMemberInfo;
	}

	newElement->SetName(GetNewElementName(dataType, idCounter_));
	newElement->SetId(GetElementId(dataType, idCounter_));
	newElement->SetDataType(dataType);

	idCounter_++;

	return newElement;
}

void CXmlElementsFactory::DestroyXmlElement(CXmlBaseElement* xmlElement)
{
	delete xmlElement;
}
	
cmString CXmlElementsFactory::GetElementId(enDataType dataType, int idCounter)
{
	cmString id;
	wchar_t buf[20];

	wsprintf(buf, _T("%d"), idCounter);

	if (dataType == enDataType_Class)
		id = _T("Class");
	else
		id = _T("Member");

	id += _T("_");
	id += buf;

	return id;
}
	
cmString CXmlElementsFactory::GetNewElementName(enDataType dataType, int idCounter)
{
	cmString ret;

	ret = _T("New");
	ret += GetElementId(dataType, idCounter);

	return ret;
}

void CXmlElementsFactory::UpdateIdCounter(const cmString& newId)
{
	// extract number
	//
	int pos;
	int id;
	cmString number;

	pos = newId.Find(_T("_"));
	if (pos == -1)
		return;

	number = newId.Mid(pos + 1);

	id = number;
	if (id > idCounter_)
		idCounter_ = id;
}

void CXmlElementsFactory::ResetIdCounter()
{
	idCounter_ = 0;
}