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

namespace FormulaEditor
{
    public class ArrayOperation : IObjectOperation
    {

        #region Fields

        protected IObjectOperation operation;
        protected object[] y;
        protected ArrayReturnType returnType;
        protected object returnValue;
        protected bool[] isArray;
        protected int length = 0;
        protected object[] types;
        protected int[][] ranks;
        protected object[][] yy;
        protected int[] rank;
        protected object[] yt;

        #endregion

        #region Constructors

        public ArrayOperation(IObjectOperation operation, object[] types)
        {
            //            rank = getRank(types);
            this.operation = operation;
            this.types = types;
            yy = new object[types.Length][];
            CreateAllArrays(operation, types, out y, out yy, out rank, out ranks);
            returnValue = y;
            if (types != null)
            {
                if (types.Length > 0)
                {
                    yt = new object[types.Length];
                }
            }

            returnType = new ArrayReturnType(operation.ReturnType, rank);
        }

        #endregion

        public static void CreateAllArrays(IObjectOperation op, object[] types, out object[] y, out object[][] yy, out int[] rank, out int[][] ranks)
        {
            rank = getRank(types);
            ranks = new int[types.Length][];
            for (int i = 0; i < ranks.Length; i++)
            {
                ranks[i] = getRank(types);
            }

            y = createArray(op.ReturnType, rank);
            yy = createArrays(types, rank, rank.Length);
        }


        private static object[] createArray(object type, int[] ranks)
        {
            return createArray(type, ranks, ranks.Length);
        }

        static int[] getRank(object[] types)
        {
            int i = 0;
            ArrayReturnType t = null;
            for (; i < types.Length; i++)
            {
                if (types[i] is ArrayReturnType)
                {
                    t = types[i] as ArrayReturnType;
                    return t.Dimension;
                }
            }
            return null;
        }

        private static object[] createArray(object type, int[] ranks, int rank)
        {
            if (rank == 1)
            {
                object[] o = new object[ranks[ranks.Length - 1]];
                for (int i = 0; i < o.Length; i++)
                {
                    o[i] = type;
                }
                return o;
            }
            int m = ranks.Length - rank;
            int n = ranks[m];
            object[] ob = new object[n];
            for (int i = 0; i < ob.Length; i++)
            {
                ob[i] = createArray(type, ranks, rank - 1);
            }
            return ob;
        }

        private static object[][] createArrays(object[] types, int[] ranks, int rank)
        {
            int length = types.Length;
            bool b = false;
            foreach (object t in types)
            {
                if (t is ArrayReturnType)
                {
                    ArrayReturnType rt = t as ArrayReturnType;
                    length = rt.Dimension[0];
                    b = true;
                    break;
                }
            }
            object[][] o = new object[length][];
            if (rank == 1)
            {
                for (int i = 0; i < o.Length; i++)
                {
                    object[] ob = new object[types.Length];
                    o[i] = ob;
                    for (int j = 0; j < types.Length; j++)
                    {
                        object tt = ArrayReturnType.GetBaseType(types[j]);
                        ob[j] = tt;
                    }
                }
                return o;
            }
            return null;
        }



        #region IObjectOperation Members

        public virtual int Arity
        {
            get
            {
                return types.Length;
            }
        }

        public virtual object this[object[] x]
        {
            get
            {
                PerformOperation(operation, returnValue as object[], x, yy, rank.Length, ranks, rank, yt);
                return returnValue;
            }
        }

        public object ReturnType
        {
            get
            {
                return returnType;
            }
        }

        public virtual bool IsPowered
        {
            get
            {
                return operation.IsPowered;
            }
        }

        #endregion

        #region Specific Members

        public IObjectOperation SingleOperation
        {
            get
            {
                return operation;
            }
        }



        public static void PerformOperation(IObjectOperation operation, object[] o, object[] y, object[][] yy, int rank, int[][] ranks, int[] mainRank, object[] yt)
        {
            for (int i = 0; i < mainRank[rank - 1]; i++)
            {
                for (int j = 0; j < operation.Arity; j++)
                {
                    object yo = y[j];
                    if (yo is Array)
                    {
                        Array oby = yo as Array;
                        yt[j] = oby.GetValue(i);
                    }
                    else
                    {
                        yt[j] = yo;
                    }
                }
                o[i] = operation[yt];
            }
        }

        #endregion

    }
}
