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

    internal class FlatNumeric : Control
    {
        private Color _BaseColor = Color.FromArgb(0x2d, 0x2f, 0x31);
        private Color _ButtonColor = Helpers._FlatColor;
        private long _Max;
        private long _Min;
        private long _Value;
        private bool Bool;
        private int H;
        private MouseState State = MouseState.None;
        private int W;
        private int x;
        private int y;

        public FlatNumeric()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
            this.DoubleBuffered = true;
            this.Font = new Font("Segoe UI", 10f);
            this.BackColor = Color.FromArgb(60, 70, 0x49);
            this.ForeColor = Color.White;
            this._Min = 0L;
            this._Max = 0x98967fL;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.Back)
            {
                this.Value = 0L;
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            try
            {
                if (this.Bool)
                {
                    this._Value = Conversions.ToLong(Conversions.ToString(this._Value) + e.KeyChar.ToString());
                }
                if (this._Value > this._Max)
                {
                    this._Value = this._Max;
                }
                base.Invalidate();
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                ProjectData.ClearProjectError();
            }
        }

        protected override unsafe void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if ((this.x > (base.Width - 0x15)) && (this.x < (base.Width - 3)))
            {
                ref long numRef;
                if (this.y < 15)
                {
                    if ((this.Value + 1L) <= this._Max)
                    {
                        *(numRef = (long) &this._Value) = numRef + 1L;
                    }
                }
                else if ((this.Value - 1L) >= this._Min)
                {
                    *(numRef = (long) &this._Value) = numRef - 1L;
                }
            }
            else
            {
                this.Bool = !this.Bool;
                base.Focus();
            }
            base.Invalidate();
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            this.x = e.Location.X;
            this.y = e.Location.Y;
            base.Invalidate();
            if (e.X < (base.Width - 0x17))
            {
                this.Cursor = Cursors.IBeam;
            }
            else
            {
                this.Cursor = Cursors.Hand;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Helpers.B = new Bitmap(base.Width, base.Height);
            Helpers.G = Graphics.FromImage(Helpers.B);
            this.W = base.Width;
            this.H = base.Height;
            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);
            g.FillRectangle(new SolidBrush(this._BaseColor), rect);
            g.FillRectangle(new SolidBrush(this._ButtonColor), new Rectangle(base.Width - 0x18, 0, 0x18, this.H));
            g.DrawString("+", new Font("Segoe UI", 12f), Brushes.White, (PointF) new Point(base.Width - 12, 8), Helpers.CenterSF);
            g.DrawString("-", new Font("Segoe UI", 10f, FontStyle.Bold), Brushes.White, (PointF) new Point(base.Width - 12, 0x16), Helpers.CenterSF);
            StringFormat format = new StringFormat {
                LineAlignment = StringAlignment.Center
            };
            g.DrawString(Conversions.ToString(this.Value), this.Font, Brushes.White, new Rectangle(5, 1, this.W, this.H), format);
            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)
        {
            base.OnResize(e);
            base.Height = 30;
        }

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

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

        public long Maximum
        {
            get => 
                this._Max
            set
            {
                if (value > this._Min)
                {
                    this._Max = value;
                }
                if (this._Value > this._Max)
                {
                    this._Value = this._Max;
                }
                base.Invalidate();
            }
        }

        public long Minimum
        {
            get => 
                this._Min
            set
            {
                if (value < this._Max)
                {
                    this._Min = value;
                }
                if (this._Value < this._Min)
                {
                    this._Value = this.Minimum;
                }
                base.Invalidate();
            }
        }

        public long Value
        {
            get => 
                this._Value
            set
            {
                if ((value <= this._Max) & (value >= this._Min))
                {
                    this._Value = value;
                }
                base.Invalidate();
            }
        }
    }
}

