namespace PerpetuumSoft.Widgets
{
    using PerpetuumSoft.Widgets.Licensing;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    [ToolboxBitmap(typeof(LevelBar)), DefaultProperty("Value"), LicenseProvider(typeof(WidgetsLicenseProvider))]
    public class LevelBar : ValuePresenter
    {
        private Color endColor = Color.Red;
        private int ledHeight = 3;
        private Color middleColor = Color.Yellow;
        private PerpetuumSoft.Widgets.Orientation orientation = PerpetuumSoft.Widgets.Orientation.Vertical;
        private bool smooth = false;
        private Color startColor = Color.Lime;

        private ColorBlend CreateBlend()
        {
            ColorBlend blend = new ColorBlend(6);
            blend.get_Positions()[0] = 0f;
            blend.get_Positions()[1] = 0.1f;
            blend.get_Positions()[2] = 0.3f;
            blend.get_Positions()[3] = 0.45f;
            blend.get_Positions()[4] = 0.8f;
            blend.get_Positions()[5] = 1f;
            return blend;
        }

        private Color Darking(Color c)
        {
            return Color.FromArgb(c.R / 2, c.G / 2, c.B / 2);
        }

        protected override void Draw(Graphics g, Rectangle r)
        {
            int num2;
            int num3;
            LinearGradientBrush brush;
            LinearGradientBrush brush2;
            base.Draw(g, r);
            Rectangle workRectangle = this.WorkRectangle;
            Rectangle rectangle2 = workRectangle;
            rectangle2.Inflate(1, 1);
            float num = (base.Value - this.Minimum) / (this.Maximum - this.Minimum);
            if (this.Orientation == PerpetuumSoft.Widgets.Orientation.Vertical)
            {
                num2 = (int) (num * workRectangle.Height);
                num3 = workRectangle.Height - num2;
            }
            else
            {
                num2 = (int) (num * workRectangle.Width);
                num3 = workRectangle.Width - num2;
            }
            if (this.Orientation == PerpetuumSoft.Widgets.Orientation.Vertical)
            {
                brush = new LinearGradientBrush(rectangle2, this.endColor, this.startColor, 1);
            }
            else
            {
                brush = new LinearGradientBrush(rectangle2, this.startColor, this.endColor, 180f);
            }
            ColorBlend blend = this.CreateBlend();
            blend.get_Colors()[0] = this.endColor;
            blend.get_Colors()[1] = this.endColor;
            blend.get_Colors()[2] = this.middleColor;
            blend.get_Colors()[3] = this.middleColor;
            blend.get_Colors()[4] = this.startColor;
            blend.get_Colors()[5] = this.startColor;
            brush.set_InterpolationColors(blend);
            if (this.Orientation == PerpetuumSoft.Widgets.Orientation.Vertical)
            {
                brush2 = new LinearGradientBrush(rectangle2, this.endColor, this.startColor, 1);
            }
            else
            {
                brush2 = new LinearGradientBrush(rectangle2, this.startColor, this.endColor, 180f);
            }
            blend = this.CreateBlend();
            blend.get_Colors()[0] = this.Darking(this.endColor);
            blend.get_Colors()[1] = this.Darking(this.endColor);
            blend.get_Colors()[2] = this.Darking(this.middleColor);
            blend.get_Colors()[3] = this.Darking(this.middleColor);
            blend.get_Colors()[4] = this.Darking(this.startColor);
            blend.get_Colors()[5] = this.Darking(this.startColor);
            brush2.set_InterpolationColors(blend);
            if (this.smooth)
            {
                if (base.Value > this.Minimum)
                {
                    Rectangle rect = workRectangle;
                    if (this.Orientation == PerpetuumSoft.Widgets.Orientation.Vertical)
                    {
                        rect.Y += num3;
                        rect.Height -= num3;
                    }
                    else
                    {
                        rect.Width -= num3;
                    }
                    g.FillRectangle(brush, rect);
                }
                if (base.Value < this.Maximum)
                {
                    Rectangle rectangle4 = workRectangle;
                    if (this.Orientation == PerpetuumSoft.Widgets.Orientation.Vertical)
                    {
                        rectangle4.Height -= num2;
                    }
                    else
                    {
                        rectangle4.Width -= num2;
                        rectangle4.X += num2;
                    }
                    g.FillRectangle(brush2, rectangle4);
                }
            }
            else
            {
                g.FillRectangle(Brushes.get_Black(), workRectangle);
                if (this.Orientation == PerpetuumSoft.Widgets.Orientation.Vertical)
                {
                    for (int i = 0; i < workRectangle.Height; i += this.ledHeight + 1)
                    {
                        Brush brush3;
                        int height = i + this.ledHeight;
                        if (height > workRectangle.Height)
                        {
                            height = workRectangle.Height;
                        }
                        if ((((i + height) + 1) / 2) >= num2)
                        {
                            brush3 = brush2;
                        }
                        else
                        {
                            brush3 = brush;
                        }
                        Rectangle rectangle5 = new Rectangle(workRectangle.Left, workRectangle.Bottom - height, workRectangle.Width, height - i);
                        g.FillRectangle(brush3, rectangle5);
                    }
                }
                else
                {
                    for (int j = 0; j < workRectangle.Width; j += this.ledHeight + 1)
                    {
                        Brush brush4;
                        int width = j + this.ledHeight;
                        if (width > workRectangle.Width)
                        {
                            width = workRectangle.Width;
                        }
                        if ((((j + width) + 1) / 2) <= (workRectangle.Width - num2))
                        {
                            brush4 = brush2;
                        }
                        else
                        {
                            brush4 = brush;
                        }
                        Rectangle rectangle6 = new Rectangle(workRectangle.Right - width, workRectangle.Top, width - j, workRectangle.Height);
                        g.FillRectangle(brush4, rectangle6);
                    }
                }
            }
            g.Flush();
        }

        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        public override Color BackColor
        {
            get
            {
                return base.BackColor;
            }
            set
            {
                base.BackColor = value;
            }
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override Image BackgroundImage
        {
            get
            {
                return base.get_BackgroundImage();
            }
            set
            {
                base.set_BackgroundImage(value);
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(0x18, 0x80);
            }
        }

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

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        public override System.Drawing.Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
            }
        }

        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
            }
        }

        [Description("Height of leds"), Category("Appearance"), DefaultValue(3)]
        public int LedHeight
        {
            get
            {
                return this.ledHeight;
            }
            set
            {
                this.ledHeight = value;
                base.Invalidate();
            }
        }

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

        [Category("Appearance"), Description("Control's orientation"), DefaultValue(0)]
        public PerpetuumSoft.Widgets.Orientation Orientation
        {
            get
            {
                return this.orientation;
            }
            set
            {
                this.orientation = value;
                base.Invalidate();
            }
        }

        [Category("Appearance"), DefaultValue(false), Description("Smooth indicator visualization style")]
        public bool Smooth
        {
            get
            {
                return this.smooth;
            }
            set
            {
                this.smooth = value;
                base.Invalidate();
            }
        }

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

        [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
            }
        }
    }
}
