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

namespace FormulaEditor
{
    public class MultiArrayOperationDetector : IMultiOperationDetector
    {
        #region Fields

        private IMultiOperationDetector detector;


        #endregion

        #region Constructors
        public MultiArrayOperationDetector(IMultiOperationDetector detector)
        {
            this.detector = detector;
        }

        #endregion

        #region IMultiOperationDetector Members

        public bool HasBegin
        {
            get
            {
                return detector.HasBegin;
            }
        }

        public bool HasEnd
        {
            get
            {
                return detector.HasEnd;
            }
        }

        public int Count
        {
            get
            {
                return detector.Count;
            }
        }

        public bool Detect(int i, MathSymbol symbol)
        {
            return detector.Detect(i, symbol);
        }

        public IObjectOperation Accept(object[] types)
        {
            foreach (object o in types)
            {
                if (o is ArrayReturnType)
                {
                    return accept(types);
                }
            }
            return detector.Accept(types);
        }

        #endregion

        #region Specific members

        public IObjectOperation accept(object[] types)
        {
            object[] t = new object[types.Length];
            bool arr = false;
            for (int i = 0; i < types.Length; i++)
            {
                if (types[i] is ArrayReturnType)
                {
                    arr = true;
                }
                t[i] = ArrayReturnType.GetBaseType(types[i]);
            }
            IObjectOperation op = detector.Accept(t);
            if (arr)
            {
                return new ArrayOperation(op, types);
            }
            return op;
        }

        #endregion

    }
}
