using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AGVControl
{
    public partial class MyRadioButton : RadioButton
    {
        public int CheckWidth { get; set; } = 30;
        public Color CheckOnColor { get; set; } = Color.OrangeRed;
        public Color CheckOffColor { get; set; } = Color.DimGray;
        public Color Bordercolor { get; set; } = Color.DimGray;
        public int BorderSize { get; set; } = 2;
        public int BorderRadius { get; set; } = 7;
        public MyRadioButton()
        {
            InitializeComponent();
            // Set Optimized Double Buffer to reduce flickering
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            // Redraw when resized
            this.SetStyle(ControlStyles.ResizeRedraw, true);

            this.Resize += arLabel_Resize;
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            pe.Graphics.InterpolationMode = InterpolationMode.High;
            pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            pe.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

            pe.Graphics.Clear(this.BackColor);

            var XPosition = this.DisplayRectangle.Left;
            var YPosition = this.DisplayRectangle.Top;
            using (GraphicsPath Path = new GraphicsPath())
            {
                Path.AddLine(XPosition + BorderRadius, YPosition, XPosition + Width - (BorderRadius * 2), YPosition);
                Path.AddArc(XPosition + Width - (BorderRadius * 2), YPosition, BorderRadius * 2, BorderRadius * 2, 270, 90);
                Path.AddLine(XPosition + Width, YPosition + BorderRadius, XPosition + Width, YPosition + Height - (BorderRadius * 2));
                Path.AddArc(XPosition + Width - (BorderRadius * 2), YPosition + Height - (BorderRadius * 2), BorderRadius * 2, BorderRadius * 2, 0, 90);
                Path.AddLine(XPosition + Width - (BorderRadius * 2), YPosition + Height, XPosition + BorderRadius, YPosition + Height);
                Path.AddArc(XPosition, YPosition + Height - (BorderRadius * 2), BorderRadius * 2, BorderRadius * 2, 90, 90);
                Path.AddLine(XPosition, YPosition + Height - (BorderRadius * 2), XPosition, YPosition + BorderRadius);
                Path.AddArc(XPosition, YPosition, BorderRadius * 2, BorderRadius * 2, 180, 90);
                Path.CloseFigure();
        



                var r1 = new Rectangle(
                    DisplayRectangle.Left, DisplayRectangle.Top,
                    CheckWidth,
                    DisplayRectangle.Height - 1);
                var r2 = new Rectangle(r1.Right + 3, r1.Top, DisplayRectangle.Width - r1.Width - 3 - Padding.Right, r1.Height);
                var CC = Checked ? CheckOnColor : CheckOffColor;
                pe.Graphics.FillRectangle(new SolidBrush(CC), r1);
                StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };
                if (this.TextAlign == ContentAlignment.MiddleLeft) sf.Alignment = StringAlignment.Near;
                else if (this.TextAlign == ContentAlignment.MiddleRight) sf.Alignment = StringAlignment.Far;
                pe.Graphics.DrawString(this.Text, this.Font, new SolidBrush(ForeColor), r2, sf);
                //pe.Graphics.DrawRectangle(new Pen(this.Bordercolor, this.BorderSize), DisplayRectangle);
                this.Region = new System.Drawing.Region(Path);


                pe.Graphics.DrawPath(new Pen(this.Bordercolor, this.BorderSize), Path);

                

            }
        }
        void arLabel_Resize(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}
