using System;

namespace Mox.Shared.Serializer
{
	public class MemorySerializerSave : Serializer
	{
		public MemorySerializerSave()
		{
			Root = m_CurrentNode = new MemoryNode();
		}

		public override bool Serialize(ref int _value, string _sName, int _defaultValue)
		{
			if (_value != _defaultValue)
			{
				m_CurrentNode.m_dValues.Add(_sName, _value);
				return true;
			}

			return false;
		}

		public override bool Serialize(ref string _value, string _sName, string _defaultValue)
		{
			if (_value != _defaultValue)
			{
				m_CurrentNode.m_dValues.Add(_sName, _value);
				return true;
			}

			return false;
		}

		public override bool Serialize(ref bool _value, string _sName, bool _defaultValue)
		{
			if (_value != _defaultValue)
			{
				m_CurrentNode.m_dValues.Add(_sName, _value);
				return true;
			}

			return false;
		}

		public override bool StartChild(string[] _aValidTags, out string _sName)
		{
			throw new NotImplementedException();
		}

		public override bool StartChild(string _sName)
		{
			var node = new MemoryNode()
			{
				m_sName = _sName,
				m_Parent = m_CurrentNode
			};

			m_CurrentNode.m_lChildren.Add(node);
			m_CurrentNode = node;

			if (m_PreviousChild != null)
			{
				m_PreviousChild.m_Next = node;
				m_PreviousChild = null;
			}

			return true;
		}

		public override void EndChild()
		{
			m_PreviousChild = m_CurrentNode;
			m_CurrentNode = m_CurrentNode.m_Parent;
		}

		public override bool IsLoading => false;

		private MemoryNode m_PreviousChild;
		private MemoryNode m_CurrentNode;
		public readonly MemoryNode Root;
	}
}
