using System;

namespace LLNet.Utils
{
	/// <summary>
	/// Summary description for ReplaceParam.
	/// </summary>
	public class ReplaceParam : ParamBase
	{
		#region Variables
			private string paramValue;
		#endregion

		#region Constructores
			public ReplaceParam(string pTag, string pValue)
			{
				Tag = pTag;
				paramValue = pValue;
			}

			public ReplaceParam(System.Xml.XmlNode node)
			{
				FromXML(node);
			}

		#endregion

		#region Sobrecarga de metodos
			public override string Replace(string pSql)
			{
				string auxTag;			
				if (tag.Trim().StartsWith("&&") && tag.Trim().EndsWith("&&"))
				{
					auxTag = tag;
				}
				else
				{
					auxTag = "&&" + tag + "&&";
				}

				return pSql.Replace(auxTag,paramValue);
			}

			public override System.Xml.XmlNode ToXML(System.Xml.XmlDocument docto)
			{
			//-- Creamos los elementos
				System.Xml.XmlElement node = docto.CreateElement("parametro");
				System.Xml.XmlAttribute tipo = docto.CreateAttribute("tipo");
				System.Xml.XmlAttribute tag = docto.CreateAttribute("tag");
				System.Xml.XmlAttribute valor = docto.CreateAttribute("valor");
			//-- Inicializamos los elementos
				tipo.Value = this.GetType().ToString();
				tag.Value = this.tag; 
				valor.Value = this.paramValue;
			//-- Agregamos los elementos
				node.Attributes.Append(tipo);
				node.Attributes.Append(tag);
				node.Attributes.Append(valor);
				return node;
			}

			public override void FromXML(System.Xml.XmlNode node)
			{
			//-- Obtenemos los elementos
				System.Xml.XmlAttribute tag = node.Attributes["tag"];
				System.Xml.XmlAttribute valor = node.Attributes["valor"];
			//-- Inicializamos los valores
				this.tag = tag.Value; 
				this.paramValue = valor.Value;
			}

		#endregion

	}
}
