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

using Motion6D;

namespace MotionUI
{
    public class ColoredPositionFactory : PositionFactory
    {
        static public readonly string OwnName = "Colored";

        static public readonly ColoredPositionFactory Object = new ColoredPositionFactory();

        protected ColoredPositionFactory()
        {

        }


        public override string Name
        {
            get
            {
                return OwnName;
            }
        }

        public override string[] Names
        {
            get
            {
                return new string[] { PositionFactory.OwnName, OwnName };
            }
        }

        public override IPositionFactory this[string name]
        {
            get
            {
                if (OwnName.Equals(name))
                {
                    return Object;
                }
                return base[name];

            }
        }

        public override IPosition Create(object[] o)
        {
            try
            {
                double[] d = new double[3];
                for (int i = 0; i < d.Length; i++)
                {
                    d[i] = (double)o[i];
                }
                IPosition p = new Position(d);
                try
                {
                    int[] col = new int[3];
                    for (int i = 0; i < col.Length; i++)
                    {
                        double a = (double)o[i + d.Length];
                        if (a > 1)
                        {
                            a = 1;
                        }
                        if (a < 0)
                        {
                            a = 0;
                        }
                        col[i] = (int)(255 * a);
                    }
                    double size = (double)o[d.Length + col.Length];
                    if (size < 1)
                    {
                        size = 1;
                    }
                    Color color = Color.FromArgb(255, col[0], col[1], col[2]);
                    ColorSize cs = new ColorSize();
                    cs.Color = color;
                    cs.Size = size;
                    p.Parameters = cs;
                }
                catch (Exception)
                {
                }
                return p;
            }
            catch (Exception)
            {
            }
            return base.Create(o);
        }

    }
}
