// CFormItem_ComboBox.cpp
//

#include "stdafx.h"
#include "XmlFormItem_ComboBox.h"
#include "StringUtils.h"

CXmlFormItem_ComboBox::CXmlFormItem_ComboBox():sorted_(false)
{}

CXmlFormItem_ComboBox::~CXmlFormItem_ComboBox()
{}

bool CXmlFormItem_ComboBox::BuildFromXml(IXMLDOMNode* node)
{
	/***
	USES_CONVERSION;

	CXMLDOMNode childNode;
	CXMLDOMNodeList children;
	wstring nodeName;

	children = node.GetChildNodes();
	
	valueList_.clear();
	sorted_ = false;

	for (long i = 0; i < children.GetLength(); i++) {
		childNode = children.GetItem(i);
	
		nodeName = childNode.GetNodeName();

		if (nodeName == _T("formItem")) {
			CXmlBaseFormItem::BuildFromXml(childNode);
		} else if (nodeName == _T("sorted")) {
			sorted_ = (atoi(W2A(childNode.GetNodeTypedValue().bstrVal)) == 1);
		} else if (nodeName == _T("valueList")) {
			CXMLDOMNodeList valueListChildren;
			CXMLDOMNode valueNode;
			wstring valueNodeName;

			valueListChildren = childNode.GetChildNodes();
			for (long j = 0; j < valueListChildren.GetLength(); j++) {
				valueNode = valueListChildren.GetItem(j);
				
				valueNodeName = valueNode.GetNodeName();

				if (valueNodeName == _T("value")) {
					valueList_.push_back(valueNode.GetNodeTypedValue().bstrVal);
				}
			}
		}
	}
	***/
	return true;
}

bool CXmlFormItem_ComboBox::AppendToDOMDocument(IXMLDOMDocument2* xmlDoc, IXMLDOMElement* parentElement)
{
	/***
	CXMLDOMElement newElement;
	CXMLDOMElement rectElement;
	CXMLDOMElement member;
	CXMLDOMElement valueListElement;
	wstring value;

	newElement = xmlDoc.CreateElement(GetXmlNodeName().c_str());

	CXmlBaseFormItem::AppendToDOMDocument(xmlDoc, newElement, parentIsValid);

	AppendMemberNode(_T("sorted"), sorted_, newElement, xmlDoc);
	
	valueListElement = xmlDoc.CreateElement(_T("valueList"));
	for (int i = 0; i < valueList_.size(); i++) {
		value = valueList_[i];
		AppendMemberNode(_T("value"), value, valueListElement , xmlDoc);
	}
	
	newElement.AppendChild(valueListElement);

	parentElement.AppendChild(newElement);
	***/

	return true;
}


void CXmlFormItem_ComboBox::SetValueList(const vector<cmString>& valueList)
{
	// TODO: copia vector
}

vector<cmString>* CXmlFormItem_ComboBox::GetValueList()
{
	return &valueList_;
}

void CXmlFormItem_ComboBox::SetSorted(bool sorted)
{
	sorted_ = sorted;
}

bool CXmlFormItem_ComboBox::GetSorted()
{
	return sorted_;
}

enFormItemType CXmlFormItem_ComboBox::GetItemType()
{
	return enFormItemType_ComboBox;
}

void CXmlFormItem_ComboBox::SetValueList(const cmString& valueList)
{
	valueList_.clear();
	StringUtils::SplitString(valueList, _T("\r\n"), valueList_);
}

void CXmlFormItem_ComboBox::Clear()
{}

void CXmlFormItem_ComboBox::SetDefaults()
{}