using System;
using System.Reflection;

namespace FormulaEditor
{

    /// <summary>
    /// Operation associated with single property
    /// </summary>
	public class SinglePropertyOperation : IObjectOperation, IOperationAcceptor
	{
		private PropertyAcceptor propertyAcceptor;
		private string propertyName;
		private PropertyInfo propertyInfo;

		public SinglePropertyOperation(PropertyAcceptor propertyAcceptor, string propertyName)
		{
			this.propertyAcceptor = propertyAcceptor;
			this.propertyName = propertyName;
			propertyInfo = propertyAcceptor.ObjectType.GetProperty(propertyName);
			if (propertyInfo == null)
			{
				throw new Exception();
			}
		}
		#region IObjectOperation Members

		public int Arity
		{
			get
			{
				return 0;
			}
		}

		public object this[object[] x]
		{
			get
			{
				return propertyInfo.GetValue(propertyAcceptor.Object, null);
			}
		}

		public object ReturnType
		{
			get
			{
				return new object[]{propertyAcceptor.Object, propertyName};
			}
		}

		public bool IsPowered
		{
			get
			{
				return false;
			}
		}

		#endregion

		#region IOperationAcceptor Members

		public IObjectOperation Accept(object type)
		{
			return this;
		}

		#endregion
	}
}
