using System;
using System.Collections.Generic;
using System.Text;

namespace FormulaEditor
{
    [Serializable()]
    public class FieldSymbol : SimpleSymbol, IObjectOperation, IOperationAcceptor
    {
        static public readonly FieldSymbol Object = new FieldSymbol();

        private IFieldSymbolFactory factory;

        public FieldSymbol(FieldSymbol s)
            : this(s.s)
        {
        }

        public FieldSymbol(System.Runtime.Serialization.SerializationInfo info,
            System.Runtime.Serialization.StreamingContext context)
            : base(info, context)
        {
        }

        public FieldSymbol(string name) : base('f', (int)FormulaConstants.Field, true, name)
        {
        }

        private FieldSymbol() : base('f')
        {
        }

        public IFieldSymbolFactory Factory
        {
            set
            {
                factory = value;
            }
        }

        public override string ToString()
        {
            if (factory == null)
            {
                return base.ToString();
            }
            return factory.GetValue(this) + "";
        }

        public override object Clone()
        {
            return new FieldSymbol(s + "");
        }

        #region IObjectOperation Members

        int IObjectOperation.Arity
        {
            get { return 0; }
        }

        object IObjectOperation.this[object[] x]
        {
            get 
            {
                if (factory == null)
                {
                    return this;
                }
                return factory.GetValue(this);
            
            }
        }

        object IObjectOperation.ReturnType
        {
            get { return Object; }
        }

        bool IObjectOperation.IsPowered
        {
            get { return false; }
        }

        #endregion

        #region IOperationAcceptor Members

        IObjectOperation IOperationAcceptor.Accept(object type)
        {
            return this;
        }

        #endregion
}
}
