#ifndef _TestEtude_XmlProperties_h_
#define _TestEtude_XmlProperties_h_

#include <Core/Core.h>

using namespace Upp;

class XmlProperties : public Moveable<XmlProperties>
{
public:
	XmlProperties () {}
	XmlProperties (const XmlProperties &op)                   { props = op.props;                     }
	const XmlProperties & operator= (pick_ XmlProperties &op) { props = op.props;                     }
	
	const Value   & Get         (const String &s) const           { return props.Get(s);                  }

	XmlProperties & Add         (const String &s, const Value &v) { props.Add(s, v);         return *this;  }
	XmlProperties & AddPick     (const String &s, pick_ Value &v) { props.AddPick(s, v);     return *this;  }
	XmlProperties & GetPut      (const String &s, const Value &v) { props.GetPut(s, v);      return *this;  }
	XmlProperties & GetPutPick  (const String &s, pick_ Value &v) { props.GetPutPick(s, v);  return *this;  }
	XmlProperties & FindAdd     (const String &s, const Value &v) { props.FindAdd(s, v);     return *this;  }
	XmlProperties & FindPutPick (const String &s, pick_ Value &v) { props.FindPutPick(s, v); return *this;  }
	void			Clear   ()							   	      { props.Clear();                          }
	
	XmlProperties & operator<<=(const String &s)
	{
		this->Clear();
		XmlParser p(s);
		XmlNode rootNode = ParseXML(p);
		FromNode(rootNode);
		return *this;
	}
	String ToString() const { return (String) *this; } 
	operator String() const
	{
		String propsStr;
		for (int i=0; i<props.GetCount(); ++i)
		{
			String t = "unknown";
			if (props[i].Is<int>())
				propsStr << MakeXmlPropTag<int>("int", props.GetKey(i), props[i]);
			else
			if (props[i].Is<double>())
				propsStr << MakeXmlPropTag<double>("double", props.GetKey(i), props[i]);
			else
			if (props[i].Is<Date>())
				propsStr << MakeXmlPropTag<Date>("date", props.GetKey(i), props[i]);
			else
			if (props[i].Is<String>())
				propsStr << MakeXmlPropTag<String>("string", props.GetKey(i), props[i]);
			else
			if (props[i].Is<XmlProperties>())
				propsStr << MakeXmlPropTag<XmlProperties>("props", props.GetKey(i), props[i]);
		}
		
		return propsStr;
	}
	
private:
	template<class T> String MakeXmlPropTag(const String &t, const String &k, const Value &v) const {return XmlTag(t)("name",k)(AsString(ValueTo<T>(v))); }
	void FromNode(const XmlNode &rootNode)	
	{
		for (int i=0; i<rootNode.GetCount(); ++i)
		{
			const XmlNode &node = rootNode[i];
			if (node.IsTag("int"))
				props.Add(node.Attr("name"), ScanInt(node[0].GetText()));
			else
			if (node.IsTag("double"))
				props.Add(node.Attr("name"), ScanDouble(node[0].GetText()));
			else
			if (node.IsTag("date"))
			{
				Date date;
				StrToDate(date,node[0].GetText());
				props.Add(node.Attr("name"), date);
			}
			else
			if (node.IsTag("string"))
				props.Add(node.Attr("name"), node[0].GetText());
			else
			if (node.IsTag("props"))
			{
				XmlProperties subProps;
				subProps.FromNode(node);
				props.Add(node.Attr("name"), RawToValue(subProps));
			}
		}
	}

	mutable VectorMap<String, Value> props;
};


#endif
