using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QCRPG.Framework;

public class PacketFieldValueForm : Form
{
	public delegate string CustomStringConversionDelegate(string s);

	private const int formHeightDeltaFromLabelHeight = 127;

	private string description;

	private string hexValueString;

	private int hexValueLengthInBytes;

	private DataEntryMode defaultDataEntryMode;

	public CustomStringConversionDelegate ConvertHexStringToDisplayedText;

	public CustomStringConversionDelegate ConvertDisplayedTextToHexString;

	private Dictionary<DataEntryMode, string> dataEntryModeToStringMap = new Dictionary<DataEntryMode, string>();

	private Dictionary<string, DataEntryMode> stringToDataEntryModeMap = new Dictionary<string, DataEntryMode>();

	private Color normalTextBoxBackColor;

	private IContainer components;

	private TextBox textBox;

	private Button buttonOK;

	private Button buttonCancel;

	private ComboBox comboBoxDataEntryMode;

	private TextBox txtBoxDescription;

	public string Title
	{
		get
		{
			return Text;
		}
		set
		{
			Text = value;
		}
	}

	public string Description
	{
		get
		{
			return description;
		}
		set
		{
			description = value;
		}
	}

	public string HexValueString
	{
		get
		{
			return hexValueString;
		}
		set
		{
			hexValueString = value;
		}
	}

	public int HexValueLengthInBytes
	{
		get
		{
			return hexValueLengthInBytes;
		}
		set
		{
			hexValueLengthInBytes = value;
		}
	}

	public DataEntryMode DefaultDataEntryMode
	{
		get
		{
			return defaultDataEntryMode;
		}
		set
		{
			defaultDataEntryMode = value;
		}
	}

	public PacketFieldValueForm()
	{
		InitializeComponent();
		normalTextBoxBackColor = textBox.BackColor;
	}

	private void PacketFieldValueForm_Load(object sender, EventArgs e)
	{
		int num = base.Height;
		description.Replace("\r", "").Split('\n');
		Screen screen = Screen.FromControl(this);
		Size size = TextRenderer.MeasureText(description, Font);
		int num2 = size.Height + 3 * base.FontHeight;
		txtBoxDescription.Height = ((num2 > screen.WorkingArea.Height) ? screen.WorkingArea.Height : num2);
		if (txtBoxDescription.Height + 127 > screen.WorkingArea.Height)
		{
			txtBoxDescription.Height -= 127;
		}
		txtBoxDescription.Width = ((size.Width < base.Width) ? base.Width : size.Width);
		txtBoxDescription.Text = description;
		AdjustSize();
		int num3 = base.Height;
		Point location = new Point(base.Location.X, base.Location.Y);
		location.Y -= (num3 - num) / 2;
		base.Location = location;
		if (ConvertHexStringToDisplayedText != null && ConvertDisplayedTextToHexString != null)
		{
			dataEntryModeToStringMap.Add(DataEntryMode.Custom, "Custom");
			stringToDataEntryModeMap.Add("Custom", DataEntryMode.Custom);
		}
		else
		{
			dataEntryModeToStringMap.Add(DataEntryMode.RightAlignedHex, "Right Aligned Hex");
			stringToDataEntryModeMap.Add("Right Aligned Hex", DataEntryMode.RightAlignedHex);
			dataEntryModeToStringMap.Add(DataEntryMode.LeftAlignedHex, "Left Aligned Hex");
			stringToDataEntryModeMap.Add("Left Aligned Hex", DataEntryMode.LeftAlignedHex);
			if (hexValueLengthInBytes <= 4)
			{
				dataEntryModeToStringMap.Add(DataEntryMode.DecimalInteger, "Decimal Integer");
				stringToDataEntryModeMap.Add("Decimal Integer", DataEntryMode.DecimalInteger);
				dataEntryModeToStringMap.Add(DataEntryMode.DecimalSignedInteger, "Decimal Signed Integer");
				stringToDataEntryModeMap.Add("Decimal Signed Integer", DataEntryMode.DecimalSignedInteger);
			}
			dataEntryModeToStringMap.Add(DataEntryMode.NullPaddedString, "Null Padded String");
			stringToDataEntryModeMap.Add("Null Padded String", DataEntryMode.NullPaddedString);
		}
		foreach (string key in stringToDataEntryModeMap.Keys)
		{
			comboBoxDataEntryMode.Items.Add(key);
		}
		comboBoxDataEntryMode.SelectedItem = dataEntryModeToStringMap[defaultDataEntryMode];
		SetDisplayedTextFromHexValueString();
	}

	protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
	{
		if (keyData == Keys.Escape)
		{
			base.DialogResult = DialogResult.Cancel;
			Close();
		}
		return base.ProcessCmdKey(ref msg, keyData);
	}

	private void buttonOK_Click(object sender, EventArgs e)
	{
		SetHexValueStringFromDisplayedText();
		base.DialogResult = DialogResult.OK;
		Close();
	}

	private void buttonCancel_Click(object sender, EventArgs e)
	{
		base.DialogResult = DialogResult.Cancel;
		Close();
	}

	private void label_SizeChanged(object sender, EventArgs e)
	{
		AdjustSize();
	}

	private void AdjustSize()
	{
		base.Height = txtBoxDescription.Height + 127;
	}

	private void SetHexValueStringFromDisplayedText()
	{
		DataEntryMode selectedDataEntryMode = GetSelectedDataEntryMode();
		bool flag = true;
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.Append(textBox.Text);
		stringBuilder.Replace(" ", "");
		switch (selectedDataEntryMode)
		{
		case DataEntryMode.RightAlignedHex:
			if (stringBuilder.ToString().ToLower().StartsWith("0x"))
			{
				stringBuilder.Remove(0, 2);
			}
			hexValueString = stringBuilder.ToString();
			break;
		case DataEntryMode.LeftAlignedHex:
			if (stringBuilder.ToString().ToLower().StartsWith("0x"))
			{
				stringBuilder.Remove(0, 2);
			}
			hexValueString = stringBuilder.ToString();
			flag = false;
			break;
		case DataEntryMode.DecimalInteger:
			try
			{
				hexValueString = Convert.ToInt32(textBox.Text).ToString("X");
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			break;
		case DataEntryMode.DecimalSignedInteger:
		{
			int num = Convert.ToInt32(textBox.Text);
			if (num < 0)
			{
				if ((double)Math.Abs(num) < Math.Pow(2.0, 8.0))
				{
					string text = num.ToString("X");
					hexValueString = text.Substring(text.Length - 2);
				}
				else if ((double)Math.Abs(num) < Math.Pow(2.0, 16.0))
				{
					string text2 = num.ToString("X");
					hexValueString = text2.Substring(text2.Length - 4);
				}
				else if ((double)Math.Abs(num) < Math.Pow(2.0, 24.0))
				{
					string text3 = num.ToString("X");
					hexValueString = text3.Substring(text3.Length - 6);
				}
				else if ((double)Math.Abs(num) < Math.Pow(2.0, 32.0))
				{
					string text4 = num.ToString("X");
					hexValueString = text4.Substring(text4.Length - 8);
				}
			}
			break;
		}
		case DataEntryMode.NullPaddedString:
			hexValueString = Conversions.ByteArrayToHexString(Conversions.StringToByteArray(textBox.Text));
			flag = false;
			break;
		case DataEntryMode.Custom:
			hexValueString = ConvertDisplayedTextToHexString(textBox.Text);
			break;
		default:
			throw new Exception("Unsupported data entry mode: " + selectedDataEntryMode);
		}
		int num2 = hexValueLengthInBytes * 2;
		while (hexValueString.Length < num2)
		{
			if (flag)
			{
				hexValueString = "0" + hexValueString;
			}
			else
			{
				hexValueString += "0";
			}
		}
	}

	private void SetDisplayedTextFromHexValueString()
	{
		DataEntryMode selectedDataEntryMode = GetSelectedDataEntryMode();
		switch (selectedDataEntryMode)
		{
		case DataEntryMode.RightAlignedHex:
			textBox.Text = hexValueString;
			break;
		case DataEntryMode.LeftAlignedHex:
			textBox.Text = hexValueString;
			break;
		case DataEntryMode.DecimalInteger:
			try
			{
				textBox.Text = Convert.ToInt32(hexValueString, 16).ToString();
				break;
			}
			catch (Exception)
			{
				textBox.Text = "0";
				break;
			}
		case DataEntryMode.NullPaddedString:
			try
			{
				textBox.Text = Conversions.ByteArrayToString(Conversions.HexStringToByteArray(hexValueString));
				break;
			}
			catch (Exception)
			{
				textBox.Text = "";
				break;
			}
		case DataEntryMode.Custom:
			textBox.Text = ConvertHexStringToDisplayedText(hexValueString);
			break;
		default:
			throw new Exception("Unsupported data entry mode: " + selectedDataEntryMode);
		}
	}

	private DataEntryMode GetSelectedDataEntryMode()
	{
		return stringToDataEntryModeMap[(string)comboBoxDataEntryMode.SelectedItem];
	}

	private void comboBoxDataEntryMode_SelectedIndexChanged(object sender, EventArgs e)
	{
		DataEntryMode selectedDataEntryMode = GetSelectedDataEntryMode();
		if (selectedDataEntryMode == DataEntryMode.RightAlignedHex)
		{
			textBox.BackColor = normalTextBoxBackColor;
			AutoSize = false;
			txtBoxDescription.Text = description;
			AutoSize = true;
		}
		else
		{
			textBox.BackColor = Color.Yellow;
			txtBoxDescription.Text = description + "\r\n\r\nPlease note that you are using the " + dataEntryModeToStringMap[selectedDataEntryMode] + " data entry mode.";
		}
	}

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

	private void InitializeComponent()
	{
		this.textBox = new System.Windows.Forms.TextBox();
		this.buttonOK = new System.Windows.Forms.Button();
		this.buttonCancel = new System.Windows.Forms.Button();
		this.comboBoxDataEntryMode = new System.Windows.Forms.ComboBox();
		this.txtBoxDescription = new System.Windows.Forms.TextBox();
		base.SuspendLayout();
		this.textBox.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
		this.textBox.Location = new System.Drawing.Point(18, 51);
		this.textBox.Name = "textBox";
		this.textBox.Size = new System.Drawing.Size(179, 20);
		this.textBox.TabIndex = 1;
		this.buttonOK.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
		this.buttonOK.Location = new System.Drawing.Point(94, 77);
		this.buttonOK.Name = "buttonOK";
		this.buttonOK.Size = new System.Drawing.Size(75, 23);
		this.buttonOK.TabIndex = 2;
		this.buttonOK.Text = "OK";
		this.buttonOK.UseVisualStyleBackColor = true;
		this.buttonOK.Click += new System.EventHandler(buttonOK_Click);
		this.buttonCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
		this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.buttonCancel.Location = new System.Drawing.Point(175, 77);
		this.buttonCancel.Name = "buttonCancel";
		this.buttonCancel.Size = new System.Drawing.Size(75, 23);
		this.buttonCancel.TabIndex = 3;
		this.buttonCancel.Text = "Cancel";
		this.buttonCancel.UseVisualStyleBackColor = true;
		this.buttonCancel.Click += new System.EventHandler(buttonCancel_Click);
		this.comboBoxDataEntryMode.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
		this.comboBoxDataEntryMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.comboBoxDataEntryMode.FormattingEnabled = true;
		this.comboBoxDataEntryMode.Location = new System.Drawing.Point(203, 50);
		this.comboBoxDataEntryMode.Name = "comboBoxDataEntryMode";
		this.comboBoxDataEntryMode.Size = new System.Drawing.Size(129, 21);
		this.comboBoxDataEntryMode.TabIndex = 4;
		this.comboBoxDataEntryMode.SelectedIndexChanged += new System.EventHandler(comboBoxDataEntryMode_SelectedIndexChanged);
		this.txtBoxDescription.BorderStyle = System.Windows.Forms.BorderStyle.None;
		this.txtBoxDescription.Location = new System.Drawing.Point(18, 13);
		this.txtBoxDescription.Multiline = true;
		this.txtBoxDescription.Name = "txtBoxDescription";
		this.txtBoxDescription.ReadOnly = true;
		this.txtBoxDescription.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
		this.txtBoxDescription.Size = new System.Drawing.Size(314, 20);
		this.txtBoxDescription.TabIndex = 5;
		base.AcceptButton = this.buttonOK;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		this.AutoSize = true;
		base.CancelButton = this.buttonCancel;
		base.ClientSize = new System.Drawing.Size(344, 112);
		base.Controls.Add(this.txtBoxDescription);
		base.Controls.Add(this.comboBoxDataEntryMode);
		base.Controls.Add(this.buttonCancel);
		base.Controls.Add(this.buttonOK);
		base.Controls.Add(this.textBox);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "PacketFieldValueForm";
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "PacketFieldValueForm";
		base.Load += new System.EventHandler(PacketFieldValueForm_Load);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
