namespace InstrumentationControls
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;

    internal class ValueEditor : UITypeEditor
    {
        private IWindowsFormsEditorService edSvc = null;

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (((context != null) && (context.Instance != null)) && (provider != null))
            {
                this.edSvc = (IWindowsFormsEditorService) provider.GetService(typeof(IWindowsFormsEditorService));
                if (this.edSvc != null)
                {
                    AdvancedSlider editor = new AdvancedSlider();
                    editor.MouseUp += new MouseEventHandler(this.ValueChanged);
                    this.SetEditorProps((ValuePresenter) context.Instance, editor);
                    editor.Value = (int) value;
                    this.edSvc.DropDownControl(editor);
                    value = editor.Value;
                }
            }
            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if ((context != null) && (context.Instance != null))
            {
                return (UITypeEditorEditStyle)3;
            }
            return base.GetEditStyle(context);
        }

        protected virtual void SetEditorProps(ValuePresenter editingInstance, AdvancedSlider editor)
        {
            editor.Size = new Size(300, 90);
            editor.Orientation = Orientation.Horizontal;
            editor.Minimum = editingInstance.Minimum;
            editor.Maximum = editingInstance.Maximum;
        }

        private void ValueChanged(object sender, MouseEventArgs e)
        {
            if (this.edSvc != null)
            {
                this.edSvc.CloseDropDown();
            }
        }
    }
}
