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

#include "stdafx.h"
#include "XmlProjectIcons.h"
#include "CPathSplit.h"

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

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

CXmlProjectIcons::CXmlProjectIcons():
	keyCounter_(0)
{
}

CXmlProjectIcons::~CXmlProjectIcons()
{
	Clear();
}



void CXmlProjectIcons::Clear()
{
	IconMap::iterator it;

	for (it = icons_.begin(); it != icons_.end(); it++)
		delete it->second;
	icons_.clear();

	keyCounter_ = 0;
}

bool CXmlProjectIcons::BuildFromXml(IXMLDOMNode* node)
{
	USES_CONVERSION;

	IXMLDOMNode* childNode;
	IXMLDOMNodeList* children;
	CComBSTR nodeName;
	CComVariant nodeValue;
	HRESULT hRes;
	long i;
	long size;
	CXmlIcon* newIcon;
	Clear();
	
	hRes = node->get_childNodes(&children);
	children->get_length(&size);
	for (i = 0; i < size; i++) {
		hRes = children->get_item(i, &childNode);
		hRes = childNode->get_nodeName(&nodeName);
		hRes = childNode->get_nodeTypedValue(&nodeValue);

		if (wcscmp((BSTR)nodeName, _T("Icon")) == 0) {
			newIcon = new CXmlIcon;

			newIcon->BuildFromXml(childNode);
			newIcon->SetKey(GetNewKey());
			icons_[newIcon->GetFileName()] = newIcon;
		}
	}

	return true;
}

bool CXmlProjectIcons::AppendToDOMDocument(IXMLDOMDocument2* xmlDoc, IXMLDOMElement* parentElement)
{
	CComPtr<IXMLDOMElement> newElement;
	IconMap::iterator it;
	HRESULT hr;
	CXmlIcon* icon;

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

	for (it = icons_.begin(); it != icons_.end(); it++) {
		icon = it->second;
		
		icon->AppendToDOMDocument(xmlDoc, newElement);
	}

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

cmString CXmlProjectIcons::GetIconKey(const cmString& iconFileName)
{
	IconMap::iterator it;
	cmString key;

	it = icons_.find(iconFileName);
	if (it != icons_.end())
		key = it->second->GetKey();

	return key;
}

IconMap* CXmlProjectIcons::GetIconList()
{
	return &icons_;
}

CXmlIcon* CXmlProjectIcons::AddIcon(const cmString& iconFileName)
{
	IconMap::iterator it;
	CPathSplit p(iconFileName);
	cmString fileName;
	CXmlIcon* newIcon = NULL;

	fileName = p.GetFileName() + p.GetExtension();
	it = icons_.find(fileName);
	if (it == icons_.end()) {
		
		newIcon = new CXmlIcon;

		newIcon->LoadFile(iconFileName);
		newIcon->SetKey(GetNewKey());
		icons_[newIcon->GetFileName()] = newIcon;
		it = icons_.find(fileName);

	}

	return it->second;
}

void CXmlProjectIcons::RemoveIcon(const cmString& iconFileName)
{
	IconMap::iterator it;

	it = icons_.find(iconFileName);
	if (it != icons_.end()) {
		delete it->second;
		icons_.erase(iconFileName);
	}
}

cmString CXmlProjectIcons::GetNewKey()
{
	cmString tmp;

	keyCounter_++;
	tmp.Format(_T("TREE_ICON_%04d"), keyCounter_);

	return tmp;
}

void CXmlProjectIcons::SetDefaults()
{
}

CXmlIcon* CXmlProjectIcons::GetIcon(const cmString& name)
{
	IconMap::iterator it;
	CXmlIcon* ret = NULL;

	it = icons_.find(name);
	if (it != icons_.end())
		ret = it->second;

	return ret;
}
