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

    [LicenseProvider(typeof(WidgetsLicenseProvider)), ToolboxBitmap(typeof(Thermometer)), DefaultProperty("Value")]
    public class Thermometer : ValuePresenter
    {
        private int largeTickFrequency = 10;
        private int numberFrequency = 20;
        private Color pointerColor = Color.Red;
        private const int r1 = 1;
        private const int r2 = 5;
        private bool showNumbers = true;
        private int smallTickFrequency = 2;

        protected override void Draw(Graphics g, Rectangle r)
        {
            Rectangle workRectangle = this.WorkRectangle;
            base.Draw(g, r);
            Pen pen = new Pen(this.ForeColor);
            Brush brush = new SolidBrush(this.pointerColor);
            Brush brush2 = new SolidBrush(this.ForeColor);
            int num = ((workRectangle.Y + 3) + 1) + 3;
            int num2 = ((workRectangle.Bottom - 3) - 14) - 6;
            int num3 = (((workRectangle.Right - 14) - 5) - 4) - this.Font.get_Height();
            int num4 = ((num2 + 3) + 2) + 5;
            float num5 = ((float) (base.Value - this.Minimum)) / (this.Maximum - this.Minimum);
            int num6 = (int) (num5 * (num2 - num));
            base.Draw(g, r);
            g.DrawArc(pen, new Rectangle((num3 - 1) - 3, (num - 1) - 3, 8, 8), 180f, 180f);
            g.DrawLine(pen, (num3 - 1) - 3, num, (num3 - 1) - 3, num2 + 3);
            g.DrawLine(pen, (num3 + 1) + 3, num, (num3 + 1) + 3, num2 + 3);
            g.FillEllipse(brush, new Rectangle(num3 - 5, num4 - 5, 10, 10));
            g.FillRectangle(brush, new Rectangle(num3 - 1, num2 - num6, 3, num6 + 8));
            g.DrawArc(pen, new Rectangle((num3 - 5) - 3, (num4 - 5) - 3, 0x10, 0x10), -60f, 300f);
            for (int i = this.Minimum; i <= this.Maximum; i += this.smallTickFrequency)
            {
                num5 = ((float) (i - this.Minimum)) / (this.Maximum - this.Minimum);
                int num8 = num2 - ((int) (num5 * (num2 - num)));
                int num9 = 3;
                if ((i % this.largeTickFrequency) == 0)
                {
                    num9 = 7;
                }
                if (this.showNumbers && ((i % this.numberFrequency) == 0))
                {
                    float num10 = (num3 - 2) - 12;
                    float num11 = num8 + 2;
                    string text = base.FormatValue(i);
                    SizeF ef = g.MeasureString(text, this.Font);
                    num10 -= ef.Width;
                    num11 -= ef.Height / 2f;
                    g.DrawString(text, this.Font, brush2, (PointF) new Point((int) num10, (int) num11));
                }
                g.DrawLine(pen, (num3 - 2) - 5, num8, ((num3 - 2) - 5) - num9, num8);
                g.DrawLine(pen, (num3 + 2) + 5, num8, ((num3 + 2) + 5) + num9, num8);
            }
            if (this.Text != "")
            {
                float num12 = (num3 + 2) + 12;
                float num13 = (workRectangle.Bottom - workRectangle.Top) / 2;
                SizeF ef2 = g.MeasureString(this.Text, this.Font);
                num12 += ef2.Height / 2f;
                num13 += ef2.Width / 2f;
                GraphicsContainer container = g.BeginContainer();
                g.RotateTransform(-90f);
                g.DrawString(this.Text, this.Font, brush2, (PointF) new Point(-((int) num13), (int) num12));
                g.EndContainer(container);
            }
            g.Flush();
        }

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

        protected override Size DefaultSize
        {
            get
            {
                return new Size(80, 0xb8);
            }
        }

        [DefaultValue(10), Description("Define frequency of large ticks"), Category("Appearance")]
        public int LargeTickFrequency
        {
            get
            {
                return this.largeTickFrequency;
            }
            set
            {
                if (this.largeTickFrequency != value)
                {
                    this.largeTickFrequency = value;
                    base.Invalidate();
                }
            }
        }

        [Category("Appearance"), Description("Define frequency of numbers"), DefaultValue(20)]
        public int NumberFrequency
        {
            get
            {
                return this.numberFrequency;
            }
            set
            {
                if (this.numberFrequency != value)
                {
                    this.numberFrequency = value;
                    base.Invalidate();
                }
            }
        }

        [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();
                }
            }
        }

        [Category("Appearance"), Description("Indicate if the control paints scale numbers"), DefaultValue(true)]
        public bool ShowNumbers
        {
            get
            {
                return this.showNumbers;
            }
            set
            {
                if (this.showNumbers != value)
                {
                    this.showNumbers = value;
                    base.Invalidate();
                }
            }
        }

        [Category("Appearance"), Description("Define frequency of small ticks"), DefaultValue(2)]
        public int SmallTickFrequency
        {
            get
            {
                return this.smallTickFrequency;
            }
            set
            {
                if (this.smallTickFrequency != value)
                {
                    this.smallTickFrequency = value;
                    base.Invalidate();
                }
            }
        }
    }
}
