namespace VaioxOnline
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Text;
    using System.Windows.Forms;

    internal class FlatButton : Control
    {
        private Color _BaseColor = Helpers._FlatColor;
        private bool _Rounded = false;
        private Color _TextColor = Color.FromArgb(0xf3, 0xf3, 0xf3);
        private int H;
        private MouseState State = MouseState.None;
        private int W;

        public FlatButton()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
            this.DoubleBuffered = true;
            base.Size = new Size(0x6a, 0x20);
            this.BackColor = Color.Transparent;
            this.Font = new Font("Segoe UI", 12f);
            this.Cursor = Cursors.Hand;
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.State = MouseState.Down;
            base.Invalidate();
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.State = MouseState.Over;
            base.Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.State = MouseState.None;
            base.Invalidate();
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            this.State = MouseState.Over;
            base.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Helpers.B = new Bitmap(base.Width, base.Height);
            Helpers.G = Graphics.FromImage(Helpers.B);
            this.W = base.Width - 1;
            this.H = base.Height - 1;
            GraphicsPath path = new GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, this.W, this.H);
            Graphics g = Helpers.G;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            g.Clear(this.BackColor);
            switch (this.State)
            {
                case MouseState.None:
                    if (!this.Rounded)
                    {
                        g.FillRectangle(new SolidBrush(this._BaseColor), rect);
                        g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
                        break;
                    }
                    path = Helpers.RoundRec(rect, 6);
                    g.FillPath(new SolidBrush(this._BaseColor), path);
                    g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
                    break;

                case MouseState.Over:
                    if (!this.Rounded)
                    {
                        g.FillRectangle(new SolidBrush(this._BaseColor), rect);
                        g.FillRectangle(new SolidBrush(Color.FromArgb(20, Color.White)), rect);
                        g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
                        break;
                    }
                    path = Helpers.RoundRec(rect, 6);
                    g.FillPath(new SolidBrush(this._BaseColor), path);
                    g.FillPath(new SolidBrush(Color.FromArgb(20, Color.White)), path);
                    g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
                    break;

                case MouseState.Down:
                    if (!this.Rounded)
                    {
                        g.FillRectangle(new SolidBrush(this._BaseColor), rect);
                        g.FillRectangle(new SolidBrush(Color.FromArgb(20, Color.Black)), rect);
                        g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
                        break;
                    }
                    path = Helpers.RoundRec(rect, 6);
                    g.FillPath(new SolidBrush(this._BaseColor), path);
                    g.FillPath(new SolidBrush(Color.FromArgb(20, Color.Black)), path);
                    g.DrawString(this.Text, this.Font, new SolidBrush(this._TextColor), rect, Helpers.CenterSF);
                    break;
            }
            g = null;
            base.OnPaint(e);
            Helpers.G.Dispose();
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImageUnscaled(Helpers.B, 0, 0);
            Helpers.B.Dispose();
        }

        [Category("Colors")]
        public Color BaseColor
        {
            get => 
                this._BaseColor
            set
            {
                this._BaseColor = value;
            }
        }

        [Category("Options")]
        public bool Rounded
        {
            get => 
                this._Rounded
            set
            {
                this._Rounded = value;
            }
        }

        [Category("Colors")]
        public Color TextColor
        {
            get => 
                this._TextColor
            set
            {
                this._TextColor = value;
            }
        }
    }
}

