namespace SamplesCommon
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Globalization;
    using System.Reflection;
    using System.ComponentModel;

    /// <summary>
    /// Wrapper used to the ColorPicker sample
    /// Supports intermediate values for color while the user is typing them
    /// </summary>
    public class ColorWrapper
    {
        #region ** constructor

        public ColorWrapper(string value)
        {
            bool valid = false;

            Name = value;
            if (value.Length > 0)
            {
                if (value.StartsWith("#"))
                {
                    if (value.Length == 7)
                    {
                        Color = HexStringToColor(value.Substring(1, 6));
                        valid = true;
                    }
                    else if (value.Length == 9)
                    {
                        Color = HexStringToColor(value.Substring(3, 6));
                        valid = true;
                    }
                }
                else
                {
                    Name = value.Substring(0, 1).ToUpper() + value.Substring(1, value.Length - 1);
                    PropertyInfo pi = typeof(Colors).GetProperty(Name);
                    if (pi != null)
                    {
                        Color = (Color)pi.GetValue(null, null);
                        valid = true;
                    }
                }
            }

            // inalid color yet
            if (!valid)
            {
                Color = default(Color);
            }
        }

        #endregion

        #region ** object model

        public Brush Brush
        {
            get { return new SolidColorBrush(Color); }
        }

        public Color Color { get; private set; }

        public string Name { get; private set; }

        #endregion

        #region ** private stuff

        /// <summary>
        /// Convert a hex string to a .NET Color object.
        /// </summary>
        /// <param name="hexColor">a hex string: "FFFFFF", "#000000"</param>
        private static Color HexStringToColor(string hexColor)
        {
            if (hexColor.Length != 6)
            {
                return default(Color);
            }
            string r = hexColor.Substring(0, 2);
            string g = hexColor.Substring(2, 2);
            string b = hexColor.Substring(4, 2);

            Color color = default(Color);
            try
            {
                int ri = Int32.Parse(r, NumberStyles.HexNumber);
                int gi = Int32.Parse(g, NumberStyles.HexNumber);
                int bi = Int32.Parse(b, NumberStyles.HexNumber);
                color = Color.FromArgb(255,
                                        byte.Parse(ri.ToString()),
                                        byte.Parse(gi.ToString()),
                                        byte.Parse(bi.ToString()));
            }
            catch
            {
                return default(Color);
            }
            return color;
        }
        #endregion


        public override bool Equals(object obj)
        {
            ColorWrapper wr = obj as ColorWrapper;

            if (wr == null)
                return false;

            return wr.Color.Equals(Color);
        }
    }


    /// <summary>
    /// Converted used to for AutoComplete and edition features of the <see cref="C1ComboBox"/>
    /// </summary>
    public class ColorWrapperConverter
        : TypeConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            return new ColorWrapper(value.ToString());
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value == null)
                return string.Empty;

            return ((ColorWrapper)value).Color.ToString();
        }
    }
}
