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

namespace FormulaEditor
{
    /// <summary>
    /// Elementary fraction
    /// </summary>
    public class ElementaryFraction : IMultiVariableOperationAcceptor, IMultiVariableOperation,
        IFormulaCreatorOperation, IDerivationOperation
    {

        public static readonly ElementaryFraction Object = new ElementaryFraction();

        /// <summary>
        /// Return type
        /// </summary>
        private const Double a = 0;



        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="symbol">Operation symbol</param>
        private ElementaryFraction()
        {
        }


        /// <summary>
        /// Calculates derivation
        /// </summary>
        /// <param name="tree">The function for derivation calculation</param>
        /// <param name="s">Derivation string</param>
        /// <returns>The derivation</returns>
        public ObjectFormulaTree Derivation(ObjectFormulaTree tree, string s)
        {
            Double a = 0;
            ObjectFormulaTree[] fc = new ObjectFormulaTree[2];
            ObjectFormulaTree[] fd = new ObjectFormulaTree[2];
            for (int i = 0; i < 2; i++)
            {
                fc[i] = tree[1 - i].Clone() as ObjectFormulaTree;
                fd[i] = DerivationPerformer.Derivation(fc[i], s);
            }
            IObjectOperation nom = new ElementaryBinaryOperation('-', a);
            List<ObjectFormulaTree> nomList = new List<ObjectFormulaTree>();
            for (int i = 0; i < 2; i++)
            {
                List<ObjectFormulaTree> l = new List<ObjectFormulaTree>();
                l.Add(fd[1 - i].Clone() as ObjectFormulaTree);
                l.Add(fc[i].Clone() as ObjectFormulaTree);
                IObjectOperation o = new ElementaryBinaryOperation('*', a);
                nomList.Add(new ObjectFormulaTree(o, l));
            }
            List<ObjectFormulaTree> list = new List<ObjectFormulaTree>();
            list.Add(new ObjectFormulaTree(nom, nomList));
            IObjectOperation square = ElementaryFunctionsCreator.Object.GetPowerOperation(a, a);
            List<ObjectFormulaTree> squareList = new List<ObjectFormulaTree>();
            squareList.Add(fc[0]);
            squareList.Add(new ObjectFormulaTree(new ElementaryRealConstant(2), new List<ObjectFormulaTree>()));
            list.Add(new ObjectFormulaTree(square, squareList));
            if (list.Count != 2)
            {
                list = list;
            }
            return new ObjectFormulaTree(new ElementaryFraction(), list);
        }


        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            MathFormula form = new MathFormula(level, sizes);
            MathSymbol sym = new FractionSymbol();
            sym.Append(form);
            for (int i = 0; i < 2; i++)
            {
                form.First[i] = FormulaCreator.CreateFormula(tree[i], level, sizes);
            }
            return form;
        }

        /// <summary>
        /// Operation priority
        /// </summary>
        public int OperationPriority
        {
            get
            {
                return (int)ElementaryOperationPriorities.Function;
            }
        }



        /// <summary>
        /// Arity of this operation
        /// </summary>
        public int Arity
        {
            get
            {
                return 2;
            }
        }

        /// <summary>
        /// Calculates result of this operation
        /// </summary>
        public object this[object[] x]
        {
            get
            {
                double a = (double)x[0];
                double b = (double)x[1];
                return a / b;
            }
        }

        /// <summary>
        /// Return type
        /// </summary>
        public object ReturnType
        {
            get
            {
                return a;
            }
        }

        /// <summary>
        /// The "is powered" sign
        /// </summary>
        public bool IsPowered
        {
            get
            {
                return false;
            }
        }



        /// <summary>
        /// Accepts operation
        /// </summary>
        /// <param name="type">Argument type</param>
        /// <returns>The operation</returns>
        public IObjectOperation Accept(object type)
        {
            return this;
        }

        /// <summary>
        /// Accepts operation
        /// </summary>
        /// <param name="types">Types of variables</param>
        /// <returns>Accepted operation</returns>
        public IObjectOperation Accept(object[] types)
        {
            if (types.Length < 2)
            {
                throw new Exception("Incomplete fraction");
            }
            return this;
        }


        /// <summary>
        /// Accepts operation
        /// </summary>
        /// <param name="symbol">The symbol</param>
        /// <returns>Accepted operation</returns>
        public IMultiVariableOperation AcceptOperation(MathSymbol symbol)
        {
            if (symbol is FractionSymbol)
            {
                return this;
            }
            return null;
        }
    }
}
