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

    [ToolboxBitmap(typeof(AnalogClock)), LicenseProvider(typeof(WidgetsLicenseProvider))]
    public class AnalogClock : Clock
    {
        private Color pointerColor = Color.Blue;

        protected override void Draw(Graphics g, Rectangle r)
        {
            base.Draw(g, r);
            Rectangle workRectangle = this.WorkRectangle;
            using (Pen pen = new Pen(this.ForeColor))
            {
                using (Pen pen2 = new Pen(this.pointerColor))
                {
                    using (Pen pen3 = new Pen(this.pointerColor, 2.2f))
                    {
                        using (Pen pen4 = new Pen(this.pointerColor, 4f))
                        {
                            using (Brush brush = new SolidBrush(this.ForeColor))
                            {
                                pen3.set_EndCap(3);
                                pen4.set_StartCap(0x12);
                                pen4.set_EndCap(3);
                                Point center = this.Center;
                                int radius = this.Radius;
                                for (int i = 0; i < 12; i++)
                                {
                                    float num3 = 30 * i;
                                    float num4 = 0.9f * radius;
                                    double num5 = center.X + (Math.Sin((num3 / 180f) * 3.1415926535897931) * num4);
                                    double num6 = center.Y - (Math.Cos((num3 / 180f) * 3.1415926535897931) * num4);
                                    Point point2 = new Point((int) num5, (int) num6);
                                    num4 = 0.75f * radius;
                                    num5 = center.X + (Math.Sin((num3 / 180f) * 3.1415926535897931) * num4);
                                    num6 = center.Y - (Math.Cos((num3 / 180f) * 3.1415926535897931) * num4);
                                    Point point3 = new Point((int) num5, (int) num6);
                                    g.DrawLine(pen, point3, point2);
                                }
                                if (this.Text != "")
                                {
                                    g.DrawString(this.Text, this.Font, brush, (PointF) new Point(center.X - ((int) (g.MeasureString(this.Text, this.Font).Width / 2f)), (center.Y + 4) + (this.Font.get_Height() / 2)));
                                }
                                float angle = ((((base.ClockTime.Hour % 12) * 360f) / 12f) + (((base.ClockTime.Minute * 360f) / 60f) / 12f)) + ((((base.ClockTime.Second * 360f) / 60f) / 60f) / 12f);
                                float num8 = ((base.ClockTime.Minute * 360f) / 60f) + (((base.ClockTime.Second * 360f) / 60f) / 60f);
                                float num9 = (base.ClockTime.Second * 360f) / 60f;
                                g.set_SmoothingMode(2);
                                this.DrawPointer(g, pen4, center, angle, radius * 0.5f, 0f);
                                this.DrawPointer(g, pen3, center, num8, radius * 0.6f, 10f);
                                if (base.ShowSecond)
                                {
                                    this.DrawPointer(g, pen2, center, num9, radius * 0.9f, 18f);
                                }
                                g.DrawEllipse(pen2, center.X - 5, center.Y - 5, 10, 10);
                                g.set_SmoothingMode(0);
                                g.Flush();
                            }
                        }
                    }
                }
            }
        }

        private void DrawPointer(Graphics g, Pen pen, Point center, float angle, float radius, float backRadius)
        {
            double num = center.X + (Math.Sin((angle / 180f) * 3.1415926535897931) * radius);
            double num2 = center.Y - (Math.Cos((angle / 180f) * 3.1415926535897931) * radius);
            double num3 = center.X - (Math.Sin((angle / 180f) * 3.1415926535897931) * backRadius);
            double num4 = center.Y + (Math.Cos((angle / 180f) * 3.1415926535897931) * backRadius);
            PointF tf = new PointF((float) num, (float) num2);
            PointF tf2 = new PointF((float) num3, (float) num4);
            g.DrawLine(pen, tf2, tf);
        }

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

        protected override void UpdateTime(DateTime oldTime, DateTime newTime)
        {
            base.Invalidate();
        }

        private Point Center
        {
            get
            {
                Rectangle workRectangle = this.WorkRectangle;
                return new Point((workRectangle.Left + workRectangle.Right) / 2, (workRectangle.Bottom + workRectangle.Top) / 2);
            }
        }

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

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

        private int Radius
        {
            get
            {
                Rectangle workRectangle = this.WorkRectangle;
                if (workRectangle.Width < workRectangle.Height)
                {
                    return (workRectangle.Width / 2);
                }
                return (workRectangle.Height / 2);
            }
        }
    }
}
