using System.ComponentModel;

namespace System.Windows.Forms;

public class CustomizedTextBox : TextBox
{
	private Mask m_mask;

	private int digitsAfterDot;

	private int dotNum;

	private ErrorProvider errorProvider1;

	private Container components;

	public Mask Masked
	{
		get
		{
			return m_mask;
		}
		set
		{
			m_mask = value;
			Text = "";
		}
	}

	public CustomizedTextBox()
	{
		InitializeComponent();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
		this.errorProvider1.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.NeverBlink;
		base.KeyPress += new System.Windows.Forms.KeyPressEventHandler(OnKeyPress);
		base.Leave += new System.EventHandler(OnLeave);
	}

	private void OnKeyPress(object sender, KeyPressEventArgs e)
	{
		CustomizedTextBox customizedTextBox = (CustomizedTextBox)sender;
		if (m_mask == Mask.IpAddress)
		{
			customizedTextBox.MaskIpAddr(e);
		}
	}

	private void OnLeave(object sender, EventArgs e)
	{
		CustomizedTextBox customizedTextBox = (CustomizedTextBox)sender;
		if (m_mask != 0)
		{
			return;
		}
		short num = 0;
		int length = customizedTextBox.Text.Length;
		for (short num2 = 0; num2 < length; num2++)
		{
			if (customizedTextBox.Text[num2] == '.')
			{
				num++;
				if (num2 + 1 < length && customizedTextBox.Text[num2 + 1] == '.')
				{
					errorProvider1.SetError(this, "IP address is not valid: 4 section of 0-255 numbers delimit by dots.");
					break;
				}
			}
		}
		if (num < 3 || customizedTextBox.Text[length - 1] == '.')
		{
			errorProvider1.SetError(this, "IP address is not valid: 4 section of 0-255 numbers delimit by dots.");
		}
	}

	private void MaskIpAddr(KeyPressEventArgs e)
	{
		int length = Text.Length;
		int num = Text.LastIndexOf(".");
		if (e.KeyChar == '\u0003' || e.KeyChar == '\u0016' || e.KeyChar == '\u0018' || e.KeyChar == '\u001a')
		{
			e.Handled = false;
		}
		else if (char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == '\b')
		{
			if (SelectionLength == length)
			{
				num = -1;
				digitsAfterDot = 0;
				dotNum = 0;
				Text = null;
			}
			errorProvider1.SetError(this, "");
			if (e.KeyChar != '\b')
			{
				if (e.KeyChar != '.')
				{
					if (num > 0)
					{
						digitsAfterDot = length - num;
					}
					else
					{
						digitsAfterDot++;
					}
					if (digitsAfterDot == 3)
					{
						string s = Text.Substring(num + 1) + e.KeyChar;
						if (int.Parse(s) > 255)
						{
							errorProvider1.SetError(this, "The number can't be bigger than 255");
						}
						else if (dotNum < 3)
						{
							AppendText(e.KeyChar.ToString());
							AppendText(".");
							dotNum++;
							e.Handled = true;
						}
					}
					if (digitsAfterDot == 4)
					{
						if (dotNum < 3)
						{
							AppendText(".");
							dotNum++;
						}
						else
						{
							e.Handled = true;
						}
					}
				}
				else if (dotNum + 1 > 3)
				{
					e.Handled = true;
					Text.TrimEnd(e.KeyChar);
				}
				else if (Text.EndsWith("."))
				{
					e.Handled = true;
				}
				else
				{
					dotNum++;
				}
				return;
			}
			e.Handled = false;
			if (length - num == 1)
			{
				dotNum--;
				if (num > -1)
				{
					digitsAfterDot = length - num;
				}
				else
				{
					digitsAfterDot--;
				}
			}
			else if (num > -1)
			{
				digitsAfterDot = length - num - 1;
			}
			else
			{
				digitsAfterDot = length - 1;
			}
		}
		else
		{
			e.Handled = true;
		}
	}
}
