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

namespace FormulaEditor
{
    public class FormulaArrayObjectCreator : IFormulaObjectCreator
    {
        private IFormulaObjectCreator creator;
        protected ArrayList binary = new ArrayList();
        protected ArrayList multi = new ArrayList();
        protected ArrayList detectors = new ArrayList();


        public FormulaArrayObjectCreator(IFormulaObjectCreator creator)
        {
            this.creator = creator;
            for (int i = 0; i < creator.BinaryCount; i++)
            {
                binary.Add(new BinaryArrayOperationDetector(creator.GetBinaryDetector(i)));
            }
            for (int i = 0; i < creator.MultiOperationCount; i++)
            {
                multi.Add(new MultiArrayOperationDetector(creator.GetMultiOperationDetector(i)));
            }
            for (int i = 0; i < creator.OperationCount; i++)
            {
                IOperationDetector d = creator.GetDetector(i);
                detectors.Add(new ArrayOperationDetector(d));
            }
            detectors.Add(ArraySingleDetector.Object);
        }


        #region IFormulaObjectCreator Members

        public int BinaryCount
        {
            get
            {
                return binary.Count;
            }
        }

        public IBinaryDetector GetBinaryDetector(int i)
        {
            return binary[i] as IBinaryDetector;
        }

        public int MultiOperationCount
        {
            get
            {
                return multi.Count;
            }
        }

        public IMultiOperationDetector GetMultiOperationDetector(int n)
        {
            return multi[n] as IMultiOperationDetector;
        }

        public int OperationCount
        {
            get
            {
                return detectors.Count;
            }
        }

        public IOperationDetector GetDetector(int i)
        {
            return detectors[i] as IOperationDetector;
        }

        public bool IsBra(MathSymbol s)
        {
            return creator.IsBra(s);
        }

        public bool IsKet(MathSymbol s)
        {
            return creator.IsKet(s);
        }

        public IObjectOperation GetPowerOperation(object valType, object powType)
        {
            return getPowerOperation(valType, powType);
        }

        #endregion

        #region Specific members



        public IObjectOperation getPowerOperation(object valType, object powType)
        {
            object[] types = new object[] { ArrayReturnType.GetBaseType(valType), ArrayReturnType.GetBaseType(powType) };
            IObjectOperation op = creator.GetPowerOperation(types[0], types[1]);
            if (!(valType is ArrayReturnType) & !(powType is ArrayReturnType))
            {
                return op;
            }
            return new ArrayOperation(op, new object[] { valType, powType });
        }

        #endregion
    }

}
