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

namespace FormulaEditor
{
    /// <summary>
    /// Detector of null arity real operation
    /// </summary>
    public class ElementaryRealDetector : IOperationDetector
    {

        public static readonly ElementaryRealDetector Object = new ElementaryRealDetector();

        /// <summary>
        /// Constructor
        /// </summary>
        protected ElementaryRealDetector()
        {
        }

        /// <summary>
        /// Detects operation
        /// </summary>
        /// <param name="s">First symbol of the formula</param>
        /// <returns>Acceptor of operation</returns>
        public virtual IOperationAcceptor Detect(MathSymbol s)
        {
            if (s.Symbol == '~')
            {
                return new BitwiseOperation();
            }
            if (s.Symbol == '¬')
            {
                return NegationOperation.Object;
            }
            if (s.Symbol == '-')
            {
                return new ElementaryFunctionOperation('-');
            }
            if (s is SeriesSymbol)
            {
                SeriesSymbol ss = s as SeriesSymbol;
                if (ss.Acceptor is IUnary)
                {
                    return new ElementaryUnaryOperation(ss.Acceptor as IUnary, ss.Index);
                }
                return ss.Acceptor;
            }
            if (s is BinaryFunctionSymbol)
            {
                return ElementaryAtan2.Object;
            }
            if (s is RootSymbol)
            {
                return new ElementaryRoot();
            }
            if (s is FractionSymbol)
            {
                return ElementaryFraction.Object;
            }
            if (s is BracketsSymbol)
            {
                return new ElementaryBrackets();
            }
            if (s.SymbolType == (byte)FormulaConstants.Variable)
            {
                if (s.Symbol == '%')
                {
                    return new ElementaryRealConstant(Math.PI);
                }
                if (s.Symbol == 'e')
                {
                    return new ElementaryRealConstant(Math.E);
                }
                return new ElementaryRealVariable(s.Symbol);
            }
            if (s.SymbolType == (byte)FormulaConstants.Number)
            {
                return new ElementaryRealConstant(s.DoubleValue);
            }
            if (s.SymbolType == (byte)FormulaConstants.Unary & s.Symbol != '\u2211')
            {
                string str = s.Symbol + "";
                if (str.Equals(str.ToLower()))
                {
                    return new ElementaryFunctionOperation(s.Symbol);
                }
                return new ElementaryIntegerOperation(s.Symbol);
            }
            if (s is SimpleSymbol)
            {
                SimpleSymbol ss = s as SimpleSymbol;
                if (!ss.Bold & !ss.Italic)
                {
                    return new StringOperationAcceptor(ss.String);
                }
            }

            return null;
        }
    }
}
