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

    [DefaultEvent("TextChanged")]
    internal class FlatTextBox : Control
    {
        private Color _BaseColor = Color.FromArgb(0x2d, 0x2f, 0x31);
        private Color _BorderColor = Helpers._FlatColor;
        private int _MaxLength = 0x7fff;
        private bool _Multiline;
        private bool _ReadOnly;
        [CompilerGenerated, DebuggerBrowsable(DebuggerBrowsableState.Never), AccessedThroughProperty("TB")]
        private TextBox _TB;
        private HorizontalAlignment _TextAlign = HorizontalAlignment.Left;
        private Color _TextColor = Color.FromArgb(0xc0, 0xc0, 0xc0);
        private bool _UseSystemPasswordChar;
        private int H;
        private MouseState State = MouseState.None;
        private int W;

        public FlatTextBox()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
            this.DoubleBuffered = true;
            this.BackColor = Color.Transparent;
            this.TB = new TextBox();
            this.TB.Font = new System.Drawing.Font("Segoe UI", 10f);
            this.TB.Text = this.Text;
            this.TB.BackColor = this._BaseColor;
            this.TB.ForeColor = this._TextColor;
            this.TB.MaxLength = this._MaxLength;
            this.TB.Multiline = this._Multiline;
            this.TB.ReadOnly = this._ReadOnly;
            this.TB.UseSystemPasswordChar = this._UseSystemPasswordChar;
            this.TB.BorderStyle = BorderStyle.None;
            this.TB.Location = new Point(5, 5);
            this.TB.Width = base.Width - 10;
            this.TB.Cursor = Cursors.IBeam;
            if (this._Multiline)
            {
                this.TB.Height = base.Height - 11;
            }
            else
            {
                base.Height = this.TB.Height + 11;
            }
            this.TB.TextChanged += new EventHandler(this.OnBaseTextChanged);
            this.TB.KeyDown += new KeyEventHandler(this.OnBaseKeyDown);
        }

        private void OnBaseKeyDown(object s, KeyEventArgs e)
        {
            if (e.Control && (e.KeyCode == Keys.A))
            {
                this.TB.SelectAll();
                e.SuppressKeyPress = true;
            }
            if (e.Control && (e.KeyCode == Keys.C))
            {
                this.TB.Copy();
                e.SuppressKeyPress = true;
            }
        }

        private void OnBaseTextChanged(object s, EventArgs e)
        {
            this.Text = this.TB.Text;
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (!base.Controls.Contains(this.TB))
            {
                base.Controls.Add(this.TB);
            }
        }

        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;
            this.TB.Focus();
            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;
            this.TB.Focus();
            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;
            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);
            this.TB.BackColor = this._BaseColor;
            this.TB.ForeColor = this._TextColor;
            g.FillRectangle(new SolidBrush(this._BaseColor), rect);
            g = null;
            base.OnPaint(e);
            Helpers.G.Dispose();
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImageUnscaled(Helpers.B, 0, 0);
            Helpers.B.Dispose();
        }

        protected override void OnResize(EventArgs e)
        {
            this.TB.Location = new Point(5, 5);
            this.TB.Width = base.Width - 10;
            if (this._Multiline)
            {
                this.TB.Height = base.Height - 11;
            }
            else
            {
                base.Height = this.TB.Height + 11;
            }
            base.OnResize(e);
        }

        [Category("Options")]
        public override System.Drawing.Font Font
        {
            get => 
                base.Font
            set
            {
                base.Font = value;
                if (this.TB > null)
                {
                    this.TB.Font = value;
                    this.TB.Location = new Point(3, 5);
                    this.TB.Width = base.Width - 6;
                    if (!this._Multiline)
                    {
                        base.Height = this.TB.Height + 11;
                    }
                }
            }
        }

        public override Color ForeColor
        {
            get => 
                this._TextColor
            set
            {
                this._TextColor = value;
            }
        }

        [Category("Options")]
        public int MaxLength
        {
            get => 
                this._MaxLength
            set
            {
                this._MaxLength = value;
                if (this.TB > null)
                {
                    this.TB.MaxLength = value;
                }
            }
        }

        [Category("Options")]
        public bool Multiline
        {
            get => 
                this._Multiline
            set
            {
                this._Multiline = value;
                if (this.TB > null)
                {
                    this.TB.Multiline = value;
                    if (value)
                    {
                        this.TB.Height = base.Height - 11;
                    }
                    else
                    {
                        base.Height = this.TB.Height + 11;
                    }
                }
            }
        }

        [Category("Options")]
        public bool ReadOnly
        {
            get => 
                this._ReadOnly
            set
            {
                this._ReadOnly = value;
                if (this.TB > null)
                {
                    this.TB.ReadOnly = value;
                }
            }
        }

        private TextBox TB
        {
            [CompilerGenerated]
            get => 
                this._TB
            [MethodImpl(MethodImplOptions.Synchronized), CompilerGenerated]
            set
            {
                this._TB = value;
            }
        }

        [Category("Options")]
        public override string Text
        {
            get => 
                base.Text
            set
            {
                base.Text = value;
                if (this.TB > null)
                {
                    this.TB.Text = value;
                }
            }
        }

        [Category("Options")]
        public HorizontalAlignment TextAlign
        {
            get => 
                this._TextAlign
            set
            {
                this._TextAlign = value;
                if (this.TB > null)
                {
                    this.TB.TextAlign = value;
                }
            }
        }

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

        [Category("Options")]
        public bool UseSystemPasswordChar
        {
            get => 
                this._UseSystemPasswordChar
            set
            {
                this._UseSystemPasswordChar = value;
                if (this.TB > null)
                {
                    this.TB.UseSystemPasswordChar = value;
                }
            }
        }
    }
}

