using System;
using System.Text.RegularExpressions;

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

		#region Constructores
			public RecursiveConditionParam(string pTag, string pLogicalOperator, string pColumnName, string pArithmeticalOperator, string pParamValue)
			{
				Tag = pTag;
				logicalOperator = pLogicalOperator;
				columnName = pColumnName;
				arithmeticalOperator = pArithmeticalOperator;
				paramValue = pParamValue;
			}

			public RecursiveConditionParam(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 + "&&";
				}

				Regex whereTag = new Regex("WHERE *" + auxTag.ToUpper());
				Match m = whereTag.Match(pSql.ToUpper());
				if (m.Success)
				{
					return pSql.Replace(auxTag, " " + columnName + " " + arithmeticalOperator + " " + paramValue + auxTag);
				}
				else
				{
					Regex parentesisTag = new Regex("[(] *" + auxTag.ToUpper());
					Match m2 = parentesisTag.Match(pSql.ToUpper());
					if (m2.Success)
					{
						return pSql.Replace(auxTag, " " + columnName + " " + arithmeticalOperator + " " + paramValue + auxTag);
					}
					else
					{
						return pSql.Replace(auxTag, " " + logicalOperator + " " + columnName + " " + arithmeticalOperator + " " + paramValue + auxTag);
					}
				}
			}

			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 operadorLogico = docto.CreateAttribute("operadorLogico");
				System.Xml.XmlAttribute columna = docto.CreateAttribute("columna");
				System.Xml.XmlAttribute operadorAritmetico = docto.CreateAttribute("operadorArimetico");
				System.Xml.XmlAttribute valor = docto.CreateAttribute("valor");
			//-- Inicializamos los elementos
				tipo.Value = this.GetType().ToString();
				tag.Value = this.tag; 
				operadorLogico.Value = this.logicalOperator;
				columna.Value = this.columnName;
				operadorAritmetico.Value = this.arithmeticalOperator;
				valor.Value = this.paramValue;
			//-- Agregamos los elementos
				node.Attributes.Append(tipo);
				node.Attributes.Append(tag);
				node.Attributes.Append(operadorLogico);
				node.Attributes.Append(columna);
				node.Attributes.Append(operadorAritmetico);
				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 operadorLogico = node.Attributes["operadorLogico"];
				System.Xml.XmlAttribute columna = node.Attributes["columna"];
				System.Xml.XmlAttribute operadorAritmetico = node.Attributes["operadorArimetico"];
				System.Xml.XmlAttribute valor = node.Attributes["valor"];
			//-- Inicializamos los valores
				this.tag = tag.Value; 
				this.logicalOperator = operadorLogico.Value;
				this.columnName = columna.Value;
				this.arithmeticalOperator = operadorAritmetico.Value;
				this.paramValue = valor.Value;
			}

		#endregion

	}
}
