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

    internal class FlatClose : Control
    {
        private Color _BaseColor = Color.FromArgb(0xa8, 0x23, 0x23);
        private Color _TextColor = Color.FromArgb(0xf3, 0xf3, 0xf3);
        private MouseState State = MouseState.None;
        private int x;

        public FlatClose()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
            this.DoubleBuffered = true;
            this.BackColor = Color.White;
            base.Size = new Size(0x12, 0x12);
            this.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.Font = new Font("Marlett", 10f);
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            Environment.Exit(0);
        }

        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 OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            this.x = e.X;
            base.Invalidate();
        }

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

        protected override void OnPaint(PaintEventArgs e)
        {
            Bitmap image = new Bitmap(base.Width, base.Height);
            Graphics graphics = Graphics.FromImage(image);
            Rectangle rect = new Rectangle(0, 0, base.Width, base.Height);
            Graphics graphics2 = graphics;
            graphics2.SmoothingMode = SmoothingMode.HighQuality;
            graphics2.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphics2.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            graphics2.Clear(this.BackColor);
            graphics2.FillRectangle(new SolidBrush(this._BaseColor), rect);
            graphics2.DrawString("r", this.Font, new SolidBrush(this.TextColor), new Rectangle(0, 0, base.Width, base.Height), Helpers.CenterSF);
            switch (this.State)
            {
                case MouseState.Over:
                    graphics2.FillRectangle(new SolidBrush(Color.FromArgb(30, Color.White)), rect);
                    break;

                case MouseState.Down:
                    graphics2.FillRectangle(new SolidBrush(Color.FromArgb(30, Color.Black)), rect);
                    break;
            }
            graphics2 = null;
            base.OnPaint(e);
            graphics.Dispose();
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImageUnscaled(image, 0, 0);
            image.Dispose();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            base.Size = new Size(0x12, 0x12);
        }

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

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

