namespace InstrumentationControls
{
	using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    [ToolboxBitmap(typeof(LevelSlider)), DefaultProperty("Value"), DefaultEvent("ValueChanged")]
    public class LevelSlider : LevelBar
    {
        private Color activeLedColor = Color.LightGreen;
        private Color inactiveLedColor = Color.Green;
        private const int sliderHalfHeight = 5;

        public LevelSlider()
        {
            base.SetStyle((System.Windows.Forms.ControlStyles)0x200, true);
        }

        protected override void Draw(Graphics g, Rectangle r)
        {
            Point point;
            base.Draw(g, r);
            Rectangle trackRectangle = this.TrackRectangle;
            float num = ((float) (base.Value - this.Minimum)) / (this.Maximum - this.Minimum);
            if (base.Orientation == Orientation.Vertical)
            {
                int num2 = (int) (num * trackRectangle.Height);
                point = new Point(trackRectangle.X + (trackRectangle.Width / 2), (trackRectangle.Y + trackRectangle.Height) - num2);
            }
            else
            {
                int num3 = (int) (num * trackRectangle.Width);
                point = new Point(trackRectangle.X + num3, trackRectangle.Y + (trackRectangle.Height / 2));
            }
            this.DrawSlider(g, point);
        }

        private void DrawSlider(Graphics g, Point p)
        {
            Rectangle rectangle2;
            Brush brush2;
            Rectangle workRectangle = this.WorkRectangle;
            if (base.Orientation == Orientation.Vertical)
            {
                rectangle2 = new Rectangle(workRectangle.X, p.Y - 5, workRectangle.Width, 10);
            }
            else
            {
                rectangle2 = new Rectangle(p.X - 5, workRectangle.Y, 10, workRectangle.Height);
            }
            Point location = rectangle2.Location;
            Point point2 = rectangle2.Location + rectangle2.Size;
            Pen pen = SystemPens.ControlLightLight;
            Pen pen2 = SystemPens.ControlDark;
            Rectangle rectangle3 = rectangle2;
            Brush brush = new LinearGradientBrush(rectangle3, Color.White, Color.Gray, 0f);
            g.FillRectangle(brush, rectangle2);
            g.DrawLine(pen, location.X, location.Y, location.X, point2.Y);
            g.DrawLine(pen, location.X, location.Y, point2.X, location.Y);
            g.DrawLine(pen2, location.X, point2.Y, point2.X, point2.Y);
            g.DrawLine(pen2, point2.X, location.Y, point2.X, point2.Y);
            if (base.Capture)
            {
                brush2 = new SolidBrush(this.activeLedColor);
            }
            else
            {
                brush2 = new SolidBrush(this.inactiveLedColor);
            }
            Rectangle rect = rectangle2;
            rect.Inflate(-3, -3);
            g.FillRectangle(brush2, rect);
        }

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

        protected override int PointToValue(Point p)
        {
            double num;
            Rectangle trackRectangle = this.TrackRectangle;
            if (base.Orientation == Orientation.Vertical)
            {
                if (p.Y >= trackRectangle.Bottom)
                {
                    return this.Minimum;
                }
                if (p.Y <= trackRectangle.Top)
                {
                    return this.Maximum;
                }
                num = ((double) ((trackRectangle.Y + trackRectangle.Height) - p.Y)) / ((double) trackRectangle.Height);
            }
            else
            {
                if (p.X <= trackRectangle.Left)
                {
                    return this.Minimum;
                }
                if (p.X >= trackRectangle.Right)
                {
                    return this.Maximum;
                }
                num = ((float) (p.X - trackRectangle.X)) / ((float) trackRectangle.Width);
            }
            return (((int) (num * (this.Maximum - this.Minimum))) + this.Minimum);
        }

        [DefaultValue(typeof(Color), "LightGreen"), Category("Appearance"), Description("Active led color")]
        public Color ActiveLedColor
        {
            get
            {
                return this.activeLedColor;
            }
            set
            {
                if (value != this.activeLedColor)
                {
                    this.activeLedColor = value;
                    base.Invalidate();
                }
            }
        }

        protected override bool Editable
        {
            get
            {
                return true;
            }
        }

        [DefaultValue(typeof(Color), "Green"), Description("Inactive led color"), Category("Appearance")]
        public Color InactiveLedColor
        {
            get
            {
                return this.inactiveLedColor;
            }
            set
            {
                if (value != this.inactiveLedColor)
                {
                    this.inactiveLedColor = value;
                    base.Invalidate();
                }
            }
        }

        private Rectangle TrackRectangle
        {
            get
            {
                Rectangle workRectangle = this.WorkRectangle;
                if (base.Orientation == Orientation.Vertical)
                {
                    workRectangle.Y += 5;
                    workRectangle.Height -= 10;
                    return workRectangle;
                }
                workRectangle.X += 5;
                workRectangle.Width -= 10;
                return workRectangle;
            }
        }
    }
}
