// CXmlFormInfo.cpp
//
#include "stdafx.h"
#include "XmlFormInfo.h"
#include "XmlBaseFormItem.h"
#include "XmlFormItem_Static.h"
#include "XmlFormItem_TextBox.h"
#include "XmlFormItem_ComboBox.h"
#include "XmlFormItem_CheckBox.h"

CXmlFormInfo::CXmlFormInfo():idCounter_(1)
{

}

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

bool CXmlFormInfo::BuildFromXml(IXMLDOMNode* node)
{
	IXMLDOMNode* childNode;
	IXMLDOMNodeList* children;
	CXmlFormItem_Static* newStatic;
	CXmlFormItem_TextBox* newTextBox;
	CXmlFormItem_ComboBox* newComboBox;
	CXmlFormItem_CheckBox* newCheckBox;
	CComBSTR nodeName;
	CComVariant nodeValue;
	HRESULT hRes;
	long i;
	long size;

	Clear();

	hRes = node->get_childNodes(&children);
	hRes = children->get_length(&size);	
	for (i = 0; i < size; i++) {
		hRes = children->get_item(i, &childNode);
		hRes = childNode->get_nodeName(&nodeName);	
		
		if (wcscmp((BSTR)nodeName, _T("formItemStatic")) == 0) {
			
			newStatic = new CXmlFormItem_Static;
			newStatic->BuildFromXml(childNode);
			AddFormItem(newStatic);

		} else if(wcscmp((BSTR)nodeName, _T("formItemTextBox")) == 0) {
			
			newTextBox = new CXmlFormItem_TextBox;
			newTextBox->BuildFromXml(childNode);
			AddFormItem(newTextBox);

		} else if(wcscmp((BSTR)nodeName, _T("formItemComboBox")) == 0) {
			
			newComboBox = new CXmlFormItem_ComboBox;
			newComboBox->BuildFromXml(childNode);
			AddFormItem(newComboBox);

		} else if (wcscmp((BSTR)nodeName, _T("formItemCheckBox")) == 0) {
			
			newCheckBox = new CXmlFormItem_CheckBox;
			newCheckBox->BuildFromXml(childNode);
			AddFormItem(newCheckBox);

		}

		idCounter_++;
	}

	return true;
}

bool CXmlFormInfo::AppendToDOMDocument(IXMLDOMDocument2* xmlDoc, IXMLDOMElement* parentElement)
{
	IXMLDOMElement* newElement;
	FormItemList::iterator it;
	CXmlBaseFormItem* item;
	HRESULT hr;

	hr = xmlDoc->createElement(CComBSTR(_T("form")), &newElement);
   
	for (it = formItems_.begin(); it != formItems_.end(); it++) {
		item = it->second;	

		item->AppendToDOMDocument(xmlDoc, newElement);
	}	
	
	IXMLDOMNode* newOutNode;

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

	newOutNode->Release();

	return true;
}

CXmlFormItem_Static* CXmlFormInfo::AddStatic(const cmString& name)
{
	CXmlFormItem_Static* newStatic;

	newStatic = new CXmlFormItem_Static;
	newStatic->SetName(name);
	AddFormItem(newStatic);

	return newStatic;
}

CXmlFormItem_ComboBox* CXmlFormInfo::AddComboBox(const cmString& name)
{
	CXmlFormItem_ComboBox* newComboBox;

	newComboBox = new CXmlFormItem_ComboBox;
	newComboBox->SetName(name);
	AddFormItem(newComboBox);

	return newComboBox;
}

CXmlFormItem_TextBox* CXmlFormInfo::AddTextBox(const cmString& name)
{
	CXmlFormItem_TextBox* newTextBox;

	newTextBox = new CXmlFormItem_TextBox;
	newTextBox->SetName(name);
	AddFormItem(newTextBox);

	return newTextBox;
}
	
CXmlFormItem_CheckBox* CXmlFormInfo::AddCheckBox(const cmString& name)
{
	CXmlFormItem_CheckBox* newCheckBox;

	newCheckBox = new CXmlFormItem_CheckBox;
	newCheckBox->SetName(name);
	AddFormItem(newCheckBox);

	return newCheckBox;
}


void CXmlFormInfo::AddFormItem(CXmlBaseFormItem* newItem)
{
	formItems_[newItem->GetName()] = newItem;
	if (newItem->GetId() == 0) {
		// assign new id
		//
		newItem->SetId(idCounter_);
		idCounter_++;
	} else {
		// update idCounter
		//
		idCounter_ = newItem->GetId() + 1;
	}
}

void CXmlFormInfo::Clear()
{
	FormItemList::iterator it;

	for (it = formItems_.begin(); it != formItems_.end(); it++) {
		delete it->second;
	}

	formItems_.clear();
}

FormItemList* CXmlFormInfo::GetItemList()
{
	return &formItems_;
}

void CXmlFormInfo::DeleteItem(DWORD id)
{
	FormItemList::iterator it;
	CXmlBaseFormItem* item;
	
	for (it = formItems_.begin(); it != formItems_.end(); it++) {
		item = it->second;
		if (item->GetId() == id) {
			formItems_.erase(it);
			delete item;
			return;
		}
	}
}

CXmlBaseFormItem* CXmlFormInfo::GetItemById(DWORD id)
{
	FormItemList::iterator it;
	CXmlBaseFormItem* item;
	
	for (it = formItems_.begin(); it != formItems_.end(); it++) {
		item = it->second;
		if (item->GetId() == id)
			return item;
	}

	return NULL;
}

void CXmlFormInfo::Reset()
{
	Clear();
	SetDefaults();
}

void CXmlFormInfo::SetDefaults()
{
}