using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace Qualcomm.GUI.Controls;

public class LogTextBox : RichTextBox
{
	private int _posBanner;

	private Font _fontBanner;

	private int _inc = 3;

	private string _strBanner = "";

	private bool _bBannerRunning;

	private Color _colorBanner = Color.Blue;

	private bool _bMouseIsDown;

	private bool setScrollbar;

	private bool moveBannerVertical;

	public string BannerText
	{
		get
		{
			return _strBanner;
		}
		set
		{
			_strBanner = value;
		}
	}

	public Color BannerColor
	{
		get
		{
			return _colorBanner;
		}
		set
		{
			_colorBanner = value;
		}
	}

	public bool SetScrollBar
	{
		get
		{
			return setScrollbar;
		}
		set
		{
			setScrollbar = value;
			if (setScrollbar)
			{
				base.ScrollBars = RichTextBoxScrollBars.Vertical;
			}
			else
			{
				base.ScrollBars = RichTextBoxScrollBars.None;
			}
		}
	}

	public float BannerFontSize
	{
		get
		{
			return _fontBanner.Size;
		}
		set
		{
			_fontBanner = new Font("Arial", value);
		}
	}

	public bool MoveBannerVertical
	{
		get
		{
			return moveBannerVertical;
		}
		set
		{
			moveBannerVertical = value;
		}
	}

	public LogTextBox()
	{
		_fontBanner = new Font("Arial", 70f);
		SetStyle(ControlStyles.UserPaint, value: false);
		SetStyle(ControlStyles.AllPaintingInWmPaint, value: true);
		SetStyle(ControlStyles.DoubleBuffer, value: true);
		SetStyle(ControlStyles.OptimizedDoubleBuffer, value: true);
		base.ScrollBars = RichTextBoxScrollBars.None;
		Multiline = true;
		base.ReadOnly = true;
		BackColor = Color.White;
	}

	public void StartBanner(string BannerText, Color BannerColor)
	{
		_colorBanner = BannerColor;
		StartBanner(BannerText);
	}

	public void StartBanner(string BannerText)
	{
		_strBanner = BannerText;
		StartBanner();
	}

	public void StartBanner()
	{
		_bBannerRunning = true;
		SetStyle(ControlStyles.UserPaint, value: true);
		_posBanner = 0;
		Invalidate();
	}

	public void StopBanner()
	{
		_bBannerRunning = false;
		SetStyle(ControlStyles.UserPaint, value: false);
		Invalidate();
	}

	public void SetBannerRate(int Rate)
	{
		_inc = Rate;
	}

	public void UpdateBanner()
	{
		if (!_bBannerRunning)
		{
			return;
		}
		int num = base.Width;
		if (moveBannerVertical)
		{
			num = base.Height;
			if (num < 100)
			{
				num = 100;
			}
		}
		_posBanner += _inc;
		if (_posBanner >= num && _inc > 0)
		{
			_inc *= -1;
		}
		if (_posBanner <= -num && _inc < 0)
		{
			_inc *= -1;
		}
		Invalidate();
	}

	public void UpdateBanner(uint Counter)
	{
		if (!_bBannerRunning)
		{
			return;
		}
		int num = base.Width;
		if (moveBannerVertical)
		{
			num = base.Height;
			if (num < 100)
			{
				num = 100;
			}
		}
		_posBanner = (int)(Counter % 400);
		if (_posBanner > 200)
		{
			_posBanner = 400 - _posBanner;
		}
		_posBanner -= 100;
		_posBanner = _posBanner * num / 100;
		Invalidate();
	}

	protected override void OnTextChanged(EventArgs e)
	{
		Invalidate();
		WindowsMessaging.ScrollToBottom(this);
	}

	protected override void OnMouseDown(MouseEventArgs e)
	{
		Invalidate();
		base.OnMouseDown(e);
		_bMouseIsDown = true;
	}

	protected override void OnMouseUp(MouseEventArgs e)
	{
		Invalidate();
		base.OnMouseUp(e);
		_bMouseIsDown = false;
	}

	protected override void OnMouseMove(MouseEventArgs e)
	{
		if (_bMouseIsDown)
		{
			Invalidate();
		}
		base.OnMouseMove(e);
	}

	protected override void OnKeyDown(KeyEventArgs e)
	{
		Invalidate();
		base.OnKeyDown(e);
	}

	protected override void OnPaint(PaintEventArgs e)
	{
		Bitmap image = new Bitmap(base.ClientRectangle.Width, base.ClientRectangle.Height);
		Graphics g = Graphics.FromImage(image);
		IntPtr hdc = g.GetHdc();
		long num = 4L;
		WindowsMessaging.PostMessageToControl(this, 791, hdc, (IntPtr)num);
		g.ReleaseHdc(hdc);
		if (_bBannerRunning)
		{
			DrawBanner(ref g);
		}
		e.Graphics.DrawImageUnscaled(image, 0, 0);
		g.Dispose();
	}

	private void DrawBanner(ref Graphics g)
	{
		int num = base.Width;
		if (moveBannerVertical)
		{
			num = base.Height;
			if (num < 100)
			{
				num = 100;
			}
		}
		int num2 = 230 - Math.Abs(_posBanner) * 230 / num;
		if (num2 < 0)
		{
			num2 = 0;
		}
		SizeF sizeF = g.MeasureString(_strBanner, _fontBanner);
		int num3 = (int)((float)(base.Width / 2) - sizeF.Width / 2f);
		int num4 = (int)((float)(base.Height / 2) - sizeF.Height / 2f);
		Brush brush = new SolidBrush(Color.FromArgb(num2, 255, 255, 255));
		Brush brush2 = new SolidBrush(Color.FromArgb(num2, _colorBanner.R, _colorBanner.G, _colorBanner.B));
		if (moveBannerVertical)
		{
			num4 = (int)((float)(num / 2) - sizeF.Height / 2f);
			g.FillRectangle(brush2, 0, _posBanner, base.Width, num);
			g.DrawString(_strBanner, _fontBanner, brush, new PointF(num3, _posBanner + num4));
		}
		else
		{
			g.FillRectangle(brush2, _posBanner, 0, base.Width, base.Height);
			g.DrawString(_strBanner, _fontBanner, brush, new PointF(_posBanner + num3, num4));
		}
	}

	protected override void OnLinkClicked(LinkClickedEventArgs e)
	{
		Process.Start(e.LinkText);
	}
}
