#region Arthea License
/***********************************************************************
*  Arthea MUD by R. Jennings (2007)      http://arthea.googlecode.com/ *
*  By using this code you comply with the Artistic and GPLv2 Licenses. *
***********************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Arthea.Connections.Colors
{
    /// <summary>
    /// Implements a customizable color
    /// </summary>
    public class CustomColor
    {
        #region [rgn] Fields (6)
        private static int count = 0;
        private readonly int ordinal = count++;
        /// <summary>
        /// Color for items in rooms
        /// </summary>
        public static readonly CustomColor Items = new CustomColor();
        /// <summary>
        /// Color for room names
        /// </summary>
        public static readonly CustomColor RoomName = new CustomColor();
        /// <summary>
        /// Color for say command
        /// </summary>
        public static readonly CustomColor Say = new CustomColor();
        /// <summary>
        /// Color for say command text
        /// </summary>
        public static readonly CustomColor SayText = new CustomColor();
        #endregion [rgn]
        #region [rgn] Properties (1)
        /// <summary>
        /// Gets the count.
        /// </summary>
        /// <value>The count.</value>
        public static int Count
        {
            get { return count; }
        }
        #endregion [rgn]
        #region [rgn] Methods (7)
        // [rgn] Public Methods (7)
        /// <summary>
        /// Creates a color array.
        /// </summary>
        /// <returns></returns>
        public static Color[] CreateColorArray()
        {
            Color[] colors = new Color[count];
            colors[Say] = new Color('g');
            colors[SayText] = new Color('G');
            colors[RoomName] = new Color('G');
            colors[Items] = new Color('Y');
            return colors;
        }
        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <param name="color">The color.</param>
        /// <returns>returns the name of the custom color</returns>
        public static string GetName(CustomColor color)
        {
            foreach (FieldInfo fi in typeof (CustomColor).GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                if (fi.FieldType != color.GetType())
                    continue;
                if (fi.GetValue(color) == color)
                    return fi.Name;
            }
            return "unknown";
        }
        /// <summary>
        /// Gets the names.
        /// </summary>
        /// <returns>an array custom color names</returns>
        public static string[] GetNames()
        {
            List<string> names = new List<string>();
            Type type = typeof (CustomColor);
            foreach (FieldInfo fi in type.GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                if (fi.FieldType != type)
                    continue;
                names.Add(fi.Name);
            }
            return names.ToArray();
        }
        /// <summary>
        /// Performs an implicit conversion from <see cref="CustomColor"/> to <see cref="System.Int32"/>.
        /// </summary>
        /// <param name="c">The custom color.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator int(CustomColor c)
        {
            return c.ordinal;
        }
        /// <summary>
        /// Parses the specified word.
        /// </summary>
        /// <param name="word">The word.</param>
        /// <returns>a custom color</returns>
        public static CustomColor Parse(String word)
        {
            Type type = typeof (CustomColor);
            foreach (FieldInfo fi in type.GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                if (fi.FieldType != type)
                    continue;
                if (word.IsPrefixOf(fi.Name))
                    return (CustomColor) fi.GetValue(null);
            }
            return null;
        }
        /// <summary>
        /// Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.
        /// </returns>
        public override string ToString()
        {
            return BEGIN + ordinal.ToString() + END;
        }
        /// <summary>
        /// Valids the specified index.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <returns></returns>
        public static bool Valid(int index)
        {
            return (index >= 0 && index < count);
        }
        #endregion [rgn]
        #region Constants (2)
        /// <summary>
        /// Custom color begining escape code
        /// </summary>
        public const char BEGIN = '\x96';
        /// <summary>
        /// Custom color ending escape code
        /// </summary>
        public const char END = '\x97';
        #endregion
    }
}