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

    [ToolboxBitmap(typeof(AdvancedLevel)), DefaultProperty("Value"), DefaultEvent("ValueChanged")]
    public class AdvancedLevel : AdvancedSliderBase
    {
        private Color endColor = Color.Red;
        private Color middleColor = Color.Yellow;
        private Color startColor = Color.Lime;

        private LinearGradientBrush CreateColorsGradient(Rectangle gradRect, float k)
        {
            Color color = GraphicsUtils.ScaleColor(this.startColor, k);
            Color color2 = GraphicsUtils.ScaleColor(this.middleColor, k);
            Color color3 = GraphicsUtils.ScaleColor(this.endColor, k);
            int num = (this.Orientation == InstrumentationControls.Orientation.Horizontal) ? 180 : 90;
            LinearGradientBrush brush = new LinearGradientBrush(gradRect, color3, color, (float) num);
            ColorBlend blend = new ColorBlend(6);
            blend.Positions[0] = 0f;
            blend.Positions[1] = 0.1f;
            blend.Positions[2] = 0.3f;
            blend.Positions[3] = 0.45f;
            blend.Positions[4] = 0.8f;
            blend.Positions[5] = 1f;
            blend.Colors[0] = color3;
            blend.Colors[1] = color3;
            blend.Colors[2] = color2;
            blend.Colors[3] = color2;
            blend.Colors[4] = color;
            blend.Colors[5] = color;
            brush.InterpolationColors = blend;
            return brush;
        }

        private GraphicsPath CreatePath(Rectangle ir)
        {
            GraphicsPath path = new GraphicsPath();
            if (this.Orientation == InstrumentationControls.Orientation.Horizontal)
            {
                path.StartFigure();
                path.AddLine(ir.X, ir.Y, ir.X + ir.Width, ir.Y);
                path.AddArc((ir.X + ir.Width) - (ir.Height / 2), ir.Y, ir.Height, ir.Height, -90f, 180f);
                path.AddLine(ir.X + ir.Width, ir.Y + ir.Height, ir.X, ir.Y + ir.Height);
                path.AddArc(ir.X - (ir.Height / 2), ir.Y, ir.Height, ir.Height, 90f, 180f);
                path.CloseFigure();
                return path;
            }
            path.StartFigure();
            path.AddLine(ir.X, ir.Y, ir.X, ir.Y + ir.Height);
            path.AddArc(ir.X, (ir.Y - (ir.Width / 2)) + ir.Height, ir.Width, ir.Width, 0f, 180f);
            path.AddLine(ir.X + ir.Width, ir.Y + ir.Height, ir.X + ir.Width, ir.Y);
            path.AddArc(ir.X, ir.Y - (ir.Width / 2), ir.Width, ir.Width, 180f, 180f);
            path.CloseFigure();
            return path;
        }

        private LinearGradientBrush CreateTransparencyGradient(Rectangle gradRect)
        {
            Color color = Color.FromArgb(0x80, 0, 0, 0);
            Color.FromArgb(0x80, 0xff, 0xff, 0xff);
            Color.FromArgb(0, 0, 0, 0);
            Color color2 = Color.FromArgb(100, 0, 0, 0);
            int num = (this.Orientation == InstrumentationControls.Orientation.Horizontal) ? 90 : 0;
            LinearGradientBrush brush = new LinearGradientBrush(gradRect, color2, color, (float) num);
            ColorBlend blend = new ColorBlend(4);
            blend.Positions[0] = 0f;
            blend.Positions[1] = 0.25f;
            blend.Positions[2] = 0.5f;
            blend.Positions[3] = 1f;
            blend.Colors[0] = Color.FromArgb(0x80, 0, 0, 0);
            blend.Colors[1] = Color.FromArgb(0x80, 0xff, 0xff, 0xff);
            blend.Colors[2] = Color.FromArgb(0, 0, 0, 0);
            blend.Colors[3] = Color.FromArgb(100, 0, 0, 0);
            brush.InterpolationColors = blend;
            return brush;
        }

        protected override void DrawBar(Graphics g)
        {
					g.SmoothingMode = (System.Drawing.Drawing2D.SmoothingMode)(4);
            Rectangle barRectangle = base.BarRectangle;
            using (GraphicsPath path = this.CreatePath(barRectangle))
            {
                Rectangle gradRect = barRectangle;
                if (this.Orientation == InstrumentationControls.Orientation.Horizontal)
                {
                    gradRect.Inflate(barRectangle.Height / 2, 0);
                }
                else
                {
                    gradRect.Inflate(0, barRectangle.Width / 2);
                }
                using (LinearGradientBrush brush = this.CreateColorsGradient(gradRect, 0.5f))
                {
                    g.FillPath(brush, path);
                }
                if (base.Value > this.Minimum)
                {
                    Rectangle ir = barRectangle;
                    if (this.Orientation == InstrumentationControls.Orientation.Horizontal)
                    {
                        ir.Width = this.ValueToPoint(base.Value).X - ir.X;
                    }
                    else
                    {
                        ir.Y = this.ValueToPoint(base.Value).Y;
                        ir.Height = (barRectangle.Y + barRectangle.Height) - ir.Y;
                    }
                    using (GraphicsPath path2 = this.CreatePath(ir))
                    {
                        using (LinearGradientBrush brush2 = this.CreateColorsGradient(gradRect, 1f))
                        {
                            g.FillPath(brush2, path2);
                        }
                    }
                }
                using (LinearGradientBrush brush3 = this.CreateTransparencyGradient(gradRect))
                {
                    g.FillPath(brush3, path);
                }
								g.SmoothingMode = (System.Drawing.Drawing2D.SmoothingMode)(0);
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(200, 100);
            }
        }

        [Description("End gauge color"), Category("Appearance"), DefaultValue(typeof(Color), "Red")]
        public Color EndColor
        {
            get
            {
                return this.endColor;
            }
            set
            {
                this.endColor = value;
                base.Invalidate();
            }
        }

        [Category("Appearance"), Description("Middle gauge color"), DefaultValue(typeof(Color), "Yellow")]
        public Color MiddleColor
        {
            get
            {
                return this.middleColor;
            }
            set
            {
                this.middleColor = value;
                base.Invalidate();
            }
        }

        [DefaultValue(typeof(Color), "Lime"), Category("Appearance"), Description("Start gauge color")]
        public Color StartColor
        {
            get
            {
                return this.startColor;
            }
            set
            {
                this.startColor = value;
                base.Invalidate();
            }
        }
    }
}
