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

    [ToolboxBitmap(typeof(AdvancedTank)), DefaultProperty("Value"), DefaultEvent("ValueChanged")]
    public class AdvancedTank : AdvancedSliderBase
    {
        private int depth = 8;
        private Color liquidColor = Color.Blue;
        private Color tankColor = Color.Gainsboro;
        private int tankWidth = 3;

        public AdvancedTank()
        {
            this.Orientation = InstrumentationControls.Orientation.Vertical;
            base.BarWidth = 30;
            base.IndicatorWidth = 0x23;
            base.BarIndent = 0x19;
        }

        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.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;
            barRectangle.Y -= barRectangle.Width / 3;
            barRectangle.Height += barRectangle.Width / 3;
            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 (Brush brush = new SolidBrush(this.tankColor))
                {
                    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;
                    }
                    ir.Inflate(-this.tankWidth, 0);
                    ir.Height -= this.tankWidth;
                    using (GraphicsPath path2 = this.CreatePath(ir))
                    {
                        using (Brush brush2 = new SolidBrush(GraphicsUtils.ScaleColor(this.liquidColor, 0.7f)))
                        {
                            g.FillPath(brush2, path2);
                        }
                    }
                    if (this.depth > 0)
                    {
                        Point point = this.ValueToPoint(base.Value);
                        Rectangle rect = new Rectangle(barRectangle.X, point.Y - this.depth, barRectangle.Width, this.depth * 2);
                        rect.Inflate(-this.tankWidth, -this.tankWidth);
                        using (Brush brush3 = new SolidBrush(GraphicsUtils.ScaleColor(this.liquidColor, 1f)))
                        {
                            g.FillEllipse(brush3, rect);
                        }
                    }
                }
                using (Brush brush4 = this.CreateTransparencyGradient(gradRect))
                {
                    g.FillPath(brush4, path);
                }
                if (this.depth > 0)
                {
                    Rectangle rectangle5 = new Rectangle(barRectangle.X, barRectangle.Y - this.depth, barRectangle.Width, this.depth * 2);
                    using (Brush brush5 = new SolidBrush(GraphicsUtils.ScaleColor(this.tankColor, 0.8f)))
                    {
                        g.FillEllipse(brush5, rectangle5);
                    }
                    rectangle5.Inflate(-this.tankWidth, -this.tankWidth);
                    using (Brush brush6 = new SolidBrush(GraphicsUtils.ScaleColor(this.tankColor, 1f)))
                    {
                        g.FillEllipse(brush6, rectangle5);
                    }
                }
                g.SmoothingMode=(System.Drawing.Drawing2D.SmoothingMode)0;
            }
        }

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

        [Description("3D depth"), Category("Appearance"), DefaultValue(8)]
        public int Depth
        {
            get
            {
                return this.depth;
            }
            set
            {
                if (value != this.depth)
                {
                    this.depth = value;
                    base.Invalidate();
                }
            }
        }

        [Category("Appearance"), DefaultValue(typeof(Color), "Blue"), Description("Color of the liquid")]
        public Color LiquidColor
        {
            get
            {
                return this.liquidColor;
            }
            set
            {
                if (value != this.liquidColor)
                {
                    this.liquidColor = value;
                    base.Invalidate();
                }
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override Orientation Orientation
        {
            get
            {
                return base.Orientation;
            }
            set
            {
                if (value != Orientation.Vertical)
                {
                    throw new Exception("Horizontal orientation not supported");
                }
                base.Orientation = value;
            }
        }

        [Category("Appearance"), Description("Color of the tank"), DefaultValue(typeof(Color), "Gainsboro")]
        public Color TankColor
        {
            get
            {
                return this.tankColor;
            }
            set
            {
                if (value != this.tankColor)
                {
                    this.tankColor = value;
                    base.Invalidate();
                }
            }
        }

        [Category("Appearance"), Description("Width of the tank"), DefaultValue(3)]
        public int TankWidth
        {
            get
            {
                return this.tankWidth;
            }
            set
            {
                if (value != this.tankWidth)
                {
                    this.tankWidth = value;
                    base.Invalidate();
                }
            }
        }
    }
}
