using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
namespace DevComponents.DotNetBar.SuperGrid.Style
{
    ///
    /// Padding
    ///
    [TypeConverter(typeof(PaddingTypeConverter))]
    public class Padding : Thickness
    {
        #region Static data
        /// 
        /// Returns Empty instance of Thickness.
        /// 
        public new static Padding Empty
        {
            get { return (new Padding()); }
        }
        #endregion
        #region Constructors
        /// 
        /// Creates new instance of the class and initializes it.
        /// 
        /// Left padding
        /// Right padding
        /// Top padding
        /// Bottom padding
        public Padding(int left, int top, int right, int bottom)
            : base(left, top, right, bottom)
        {
        }
        /// 
        /// Initializes a new instance of the Padding class.
        /// 
        /// Uniform padding.
        public Padding(int all)
            : base(all)
        {
        }
        /// 
        /// Initializes a new instance of the Padding class.
        /// 
        public Padding()
        {
        }
        #endregion
        #region Copy
        /// 
        /// Creates an exact copy of the Padding.
        /// 
        /// Copy of the Padding.
        public new Padding Copy()
        {
            Padding copy = new Padding(Left, Top, Right, Bottom);
            return (copy);
        }
        #endregion
    }
    #region PaddingTypeConverter
    ///
    /// PaddingTypeConverter
    ///
    public class PaddingTypeConverter : ExpandableObjectConverter
    {
        #region CanConvertTo
        /// 
        /// CanConvertTo
        /// 
        /// 
        /// 
        /// 
        public override bool CanConvertTo(
            ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
                return (true);
            return (base.CanConvertTo(context, destinationType));
        }
        #endregion
        #region ConvertTo
        /// 
        /// ConvertTo
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public override object ConvertTo(
            ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                Padding p = value as Padding;
                if (p != null)
                {
                    if (p.IsUniform == true)
                        return (p.Left.ToString());
                    return (String.Format("{0:d}, {1:d}, {2:d}, {3:d}",
                        p.Bottom, p.Left, p.Right, p.Top));
                }
            }
            return (base.ConvertTo(context, culture, value, destinationType));
        }
        #endregion
        #region CanConvertFrom
        /// 
        /// CanConvertFrom
        /// 
        /// 
        /// 
        /// 
        public override bool CanConvertFrom(
            ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return (true);
            return (base.CanConvertFrom(context, sourceType));
        }
        #endregion
        #region ConvertFrom
        /// 
        /// ConvertFrom
        /// 
        /// 
        /// 
        /// 
        /// 
        public override object ConvertFrom(
            ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                string[] values = ((string)value).Split(',');
                if (values.Length != 1 && values.Length != 4)
                    throw new ArgumentException("Invalid value to convert.");
                try
                {
                    int[] v = new int[values.Length];
                    for (int i = 0; i < values.Length; i++)
                        v[i] = int.Parse(values[i]);
                    Padding p = (values.Length == 1)
                        ? new Padding(v[0])
                        : new Padding(v[1], v[3], v[2], v[0]);
                    return (p);
                }
                catch (Exception)
                {
                    throw new ArgumentException("Invalid value to convert.");
                }
            }
            return base.ConvertFrom(context, culture, value);
        }
        #endregion
        #region GetCreateInstanceSupported
        /// 
        /// GetCreateInstanceSupported
        /// 
        /// 
        /// 
        public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
        {
            return (true);
        }
        #endregion
        #region CreateInstance
        /// 
        /// CreateInstance
        /// 
        /// 
        /// 
        /// 
        public override object CreateInstance(
            ITypeDescriptorContext context, IDictionary propertyValues)
        {
            return (new Padding((int)propertyValues["Left"], (int)propertyValues["Top"],
                (int)propertyValues["Right"], (int)propertyValues["Bottom"]));
        }
        #endregion
    }
    #endregion
}