using System;
using System.Data;
using System.Text.RegularExpressions;

namespace FactorsNamespace
{
	/// <summary>
	/// Class represents factors that will be calculated according to formulas
	/// </summary>
	public class FutureCalculatingFactor : FutureFactor
	{
		/// <summary>
		/// formala expression (now in c#-code)
		/// </summary>
		private  String expression;

		public String mExpression
		{
			get { return expression; }
			set { expression = value; }
		}

		#region Construcotrs

		/// <summary>
		/// full version of constructor
		/// </summary>
		/// <param name="name"></param>
		/// <param name="function"></param>
		/// <param name="exp"></param>
		public FutureCalculatingFactor(String name, GetFactorValueDelegate function, String exp)
			: base(name, function)
		{
			this.expression = exp;
		}

		/// <summary>
		/// Simplified constructor
		/// </summary>
		/// <param name="name"></param>
		/// <param name="exp"></param>
		public FutureCalculatingFactor(String name, String exp)
			: this(name, null, exp)
		{ }
		#endregion

		public string ComplileExpr()
		{
			Regex rx = new Regex("p\\d+");

			return "(" + rx.Replace(this.expression, " toDouble((string)row[\"$0\"]) ") + ")";
		}

		#region overloaded fucntions


		public override bool IsDependentFrom(string FactorName)
		{//[\s+-/\*]?(p\d)[\s+-/\*]?
			Regex rx = new Regex("[\\*\\s\\+\\-/]?" + FactorName + "[\\*\\s\\+\\-/]?");

			return rx.Match(this.expression).Success;
		}
		#endregion
	}
}