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

    [ToolboxBitmap(typeof(SlidingGauge)), LicenseProvider(typeof(WidgetsLicenseProvider))]
    public class SlidingGauge : ValuePresenter
    {
        private int mainInterval = 10;
        private Color pointerColor = Color.Red;
        private bool showGradient = false;
        private int subDivisions = 10;

        protected override void Draw(Graphics g, Rectangle r)
        {
            base.Draw(g, r);
            this.DrawGauge(g);
        }

        private void DrawGauge(Graphics g)
        {
            Rectangle workRectangle = this.WorkRectangle;
            GraphicsContainer container = g.BeginContainer();
            g.IntersectClip(workRectangle);
            if (this.showGradient)
            {
                Rectangle rect = new Rectangle(workRectangle.X, workRectangle.Y, workRectangle.Width, workRectangle.Height / 2);
                Brush brush = new LinearGradientBrush(rect, Color.Black, this.BackColor, 1);
                g.FillRectangle(brush, rect);
                brush.Dispose();
                rect = new Rectangle(workRectangle.X, workRectangle.Y + (workRectangle.Height / 2), workRectangle.Width, workRectangle.Height / 2);
                brush = new LinearGradientBrush(rect, this.BackColor, Color.Black, 1);
                g.FillRectangle(brush, rect);
                brush.Dispose();
            }
            int num = workRectangle.Y + (workRectangle.Height / 2);
            int num2 = 20;
            int num3 = (workRectangle.Height / num2) + 1;
            if ((num3 % 2) != 0)
            {
                num3++;
            }
            int num4 = base.Value / this.mainInterval;
            int num5 = num4 * this.mainInterval;
            int num6 = num5 - (this.mainInterval * (num3 / 2));
            int num7 = num6 + (this.mainInterval * num3);
            Pen pen = new Pen(this.ForeColor);
            Brush brush2 = new SolidBrush(this.ForeColor);
            for (int i = num6; i <= num7; i += this.mainInterval)
            {
                int num9 = num + ((int) ((((double) (base.Value - i)) / ((double) this.mainInterval)) * num2));
                g.DrawLine(pen, workRectangle.Left + 6, num9, workRectangle.Left + 12, num9);
                g.DrawLine(pen, workRectangle.Right - 6, num9, workRectangle.Right - 12, num9);
                for (int j = 1; j < this.subDivisions; j++)
                {
                    int num11 = num9 - ((int) ((((float) num2) / ((float) this.subDivisions)) * j));
                    g.DrawLine(pen, workRectangle.Left + 9, num11, workRectangle.Left + 12, num11);
                    g.DrawLine(pen, workRectangle.Right - 9, num11, workRectangle.Right - 12, num11);
                }
                float num12 = workRectangle.X + (workRectangle.Width / 2);
                float num13 = num9;
                string text = base.FormatValue(i);
                SizeF ef = g.MeasureString(text, this.Font);
                num12 -= ef.Width / 2f;
                num13 -= ef.Height / 2f;
                g.DrawString(text, this.Font, brush2, (PointF) new Point((int) num12, (int) num13));
            }
            g.DrawLine(new Pen(this.pointerColor), workRectangle.Left, num, workRectangle.Right, num);
            g.EndContainer(container);
        }

        [DefaultValue(typeof(Color), "Red"), Description("Color of the  pointer"), Category("Appearance")]
        public Color PointerColor
        {
            get
            {
                return this.pointerColor;
            }
            set
            {
                if (this.pointerColor != value)
                {
                    this.pointerColor = value;
                    base.Invalidate();
                }
            }
        }

        [DefaultValue(false), Category("Appearance"), Description("Gets or sets whether control should use gradient background")]
        public bool ShowGradient
        {
            get
            {
                return this.showGradient;
            }
            set
            {
                if (this.showGradient != value)
                {
                    this.showGradient = value;
                    base.Invalidate();
                }
            }
        }
    }
}
