namespace PerpetuumSoft.Widgets
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Design;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public abstract class ValuePresenter : InstrumentationControl
    {
        private bool dragging = false;
        private int increment = 10;
        private int maximum = 100;
        private int minimum = 0;
        private int numberScale = 1;
        private int value = 0;

        [Category("Action"), Description("Occurs when ScaledValue property of the control changed")]
        public event EventHandler ScaledValueChanged;

        [Category("Action"), Description("Occurs when Value of the control changed")]
        public event EventHandler ValueChanged;

        private void DecrementValue()
        {
            this.Value -= this.increment;
        }

        protected string FormatValue(int value)
        {
            if (this.numberScale == 1)
            {
                return value.ToString();
            }
            double num = ((double) value) / ((double) this.numberScale);
            return num.ToString();
        }

        private void IncrementValue()
        {
            this.Value += this.increment;
        }

        protected override bool IsInputKey(Keys keyData)
        {
            if (!this.Editable || !base.Enabled)
            {
                return false;
            }
            if (((keyData != Keys.Up) && (keyData != Keys.Down)) && (keyData != Keys.Left))
            {
                return (keyData == Keys.Right);
            }
            return true;
        }

        protected override void OnEnter(EventArgs e)
        {
            base.Invalidate();
            base.OnEnter(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (this.Editable && base.Enabled)
            {
                switch (e.KeyCode)
                {
                    case Keys.Left:
                        this.DecrementValue();
                        e.Handled = true;
                        return;

                    case Keys.Up:
                        this.IncrementValue();
                        e.Handled = true;
                        return;

                    case Keys.Right:
                        this.IncrementValue();
                        e.Handled = true;
                        return;

                    case Keys.Down:
                        this.DecrementValue();
                        e.Handled = true;
                        return;

                    default:
                        return;
                }
            }
        }

        protected override void OnLeave(EventArgs e)
        {
            base.Invalidate();
            base.OnLeave(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if ((this.Editable && base.Enabled) && (e.Button == MouseButtons.Left))
            {
                base.Capture = true;
                this.dragging = true;
                this.Value = this.PointToValue(new Point(e.X, e.Y));
                if (base.TabStop)
                {
                    base.Focus();
                }
                base.Invalidate();
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (this.Editable && this.dragging)
            {
                this.Value = this.PointToValue(new Point(e.X, e.Y));
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if ((this.Editable && this.dragging) && (e.Button == MouseButtons.Left))
            {
                base.Capture = false;
                this.dragging = false;
                base.Invalidate();
            }
        }

        protected virtual void OnValueChanged(EventArgs e)
        {
            if (this.ValueChanged != null)
            {
                this.ValueChanged(this, e);
            }
            if (this.ScaledValueChanged != null)
            {
                this.ScaledValueChanged(this, e);
            }
        }

        protected virtual void OptimizedInvalidate(int oldValue, int NewValue)
        {
            base.Invalidate();
        }

        protected virtual int PointToValue(Point p)
        {
            return 0;
        }

        protected virtual bool Editable
        {
            get
            {
                return false;
            }
        }

        [Category("Behavior"), Description("The increment interval for keyboard controlling"), DefaultValue(10)]
        public virtual int Increment
        {
            get
            {
                return this.increment;
            }
            set
            {
                if (this.increment != value)
                {
                    this.increment = value;
                    base.Invalidate();
                }
            }
        }

        [DefaultValue(100), Description("The upper bound of the value property"), Category("Behavior")]
        public virtual int Maximum
        {
            get
            {
                return this.maximum;
            }
            set
            {
                if (this.maximum != value)
                {
                    this.maximum = value;
                    base.Invalidate();
                }
            }
        }

        [DefaultValue(0), Description("The lower bound of the value property"), Category("Behavior")]
        public virtual int Minimum
        {
            get
            {
                return this.minimum;
            }
            set
            {
                if (this.minimum != value)
                {
                    this.minimum = value;
                    base.Invalidate();
                }
            }
        }

        [Category("Appearance"), DefaultValue(1)]
        public int NumberScale
        {
            get
            {
                return this.numberScale;
            }
            set
            {
                if (this.numberScale != value)
                {
                    this.numberScale = value;
                    base.Invalidate();
                }
            }
        }

        [DesignerSerializationVisibility(0), Description("The position of the marker with scaling"), Browsable(true), Category("Behavior"), Bindable(true)]
        public double ScaledValue
        {
            get
            {
                return (((double) this.Value) / ((double) this.numberScale));
            }
            set
            {
                this.Value = (int) (value * this.numberScale);
            }
        }

        [Editor(typeof(ValueEditor), typeof(UITypeEditor)), Description("The position of the marker"), DefaultValue(0), Bindable(true), Category("Behavior")]
        public int Value
        {
            get
            {
                return this.value;
            }
            set
            {
                if (value > this.maximum)
                {
                    value = this.maximum;
                }
                if (value < this.minimum)
                {
                    value = this.minimum;
                }
                if (value != this.value)
                {
                    int oldValue = this.value;
                    this.value = value;
                    this.OnValueChanged(EventArgs.Empty);
                    this.OptimizedInvalidate(oldValue, this.value);
                }
            }
        }
    }
}
