namespace InstrumentationControls
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Globalization;

    public class DigitsDisplayConverter : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                DigitsDisplay display = (DigitsDisplay) value;
                return (display.DigitWidth.ToString() + 'x' + display.DigitHeight.ToString());
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

        public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
        {
            DigitsDisplay display = new DigitsDisplay();
            if (propertyValues.Contains("DigitWidth"))
            {
                display.DigitWidth = (int) propertyValues["DigitWidth"];
            }
            if (propertyValues.Contains("DigitHeight"))
            {
                display.DigitHeight = (int) propertyValues["DigitHeight"];
            }
            if (propertyValues.Contains("DigitSpace"))
            {
                display.DigitSpace = (int) propertyValues["DigitSpace"];
            }
            if (propertyValues.Contains("SegmentSpace"))
            {
                display.SegmentSpace = (int) propertyValues["SegmentSpace"];
            }
            if (propertyValues.Contains("SegmentThickness"))
            {
                display.SegmentThickness = (int) propertyValues["SegmentThickness"];
            }
            if (propertyValues.Contains("ActiveColor"))
            {
                display.ActiveColor = (Color) propertyValues["ActiveColor"];
            }
            if (propertyValues.Contains("InactiveColor"))
            {
                display.InactiveColor = (Color) propertyValues["InactiveColor"];
            }
            if (propertyValues.Contains("AntiAlias"))
            {
                display.AntiAlias = (bool) propertyValues["AntiAlias"];
            }
            return display;
        }

        public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            return TypeDescriptor.GetProperties(typeof(DigitsDisplay), attributes);
        }

        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
        {
            return true;
        }
    }
}
