using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using QCRPG.Framework;

namespace QCRPG.TSL.BT;

public class BTSendRawBytesUserControl : UserControlBase, IConfigurable
{
	private enum ProtocolByte
	{
		COMMAND = 1,
		ACL_DATA,
		SYNCHRONOUS_DATA,
		EVENT,
		ISO_DATA
	}

	private ProtocolByte protocolByte = ProtocolByte.COMMAND;

	private BTMainWorkerThread btMainWorkerThread;

	private IContainer components;

	private Button buttonSendRawBytes;

	private GroupBox groupBoxRawBytesWithID;

	private CheckBox checkBoxNoProtocolID;

	private ComboBox comboBoxBTRawBytes;

	private Label label1;

	private ComboBox comboBoxBTProtocolByte;

	public BTSendRawBytesUserControl(BTMainWorkerThread btMainWorkerThread)
		: base("Send Raw Bluetooth Bytes")
	{
		InitializeComponent();
		this.btMainWorkerThread = btMainWorkerThread;
		comboBoxBTProtocolByte.DataSource = Enum.GetValues(typeof(ProtocolByte));
		comboBoxBTProtocolByte.SelectedIndex = (int)protocolByte;
	}

	public Configuration GetConfiguration()
	{
		Configuration configuration = new Configuration();
		int count = comboBoxBTRawBytes.Items.Count;
		configuration.SetInt("RawBytePacketCount", count);
		for (int i = 1; i <= count; i++)
		{
			string name = $"RawBytePacket{i}";
			configuration.SetString(name, comboBoxBTRawBytes.Items[i - 1].ToString());
		}
		return configuration;
	}

	public void SetConfiguration(Configuration configuration)
	{
		int @int = configuration.GetInt("RawBytePacketCount", 1);
		for (int i = 1; i <= @int; i++)
		{
			string name = $"RawBytePacket{i}";
			string @string = configuration.GetString(name, comboBoxBTRawBytes.Text);
			if (!comboBoxBTRawBytes.Items.Contains(@string))
			{
				comboBoxBTRawBytes.Items.Add(@string);
			}
		}
	}

	private void buttonSendRawBytes_Click(object sender, EventArgs e)
	{
		comboBoxBTRawBytes.Text = comboBoxBTRawBytes.Text.Replace(" ", "");
		if (!ConfirmHex(comboBoxBTRawBytes.Text.ToString()))
		{
			MessageBox.Show("Value is not HEX, command not sent");
			return;
		}
		if (comboBoxBTRawBytes.Text.Length % 2 != 0)
		{
			comboBoxBTRawBytes.Text = "0" + comboBoxBTRawBytes.Text;
		}
		byte[] array = new byte[comboBoxBTRawBytes.Text.Length / 2 + 1];
		if (checkBoxNoProtocolID.CheckState != CheckState.Checked)
		{
			array[0] = (byte)(ProtocolByte)comboBoxBTProtocolByte.SelectedItem;
			int num = 0;
			int num2 = 1;
			while (num < comboBoxBTRawBytes.Text.Length)
			{
				array[num2] = byte.Parse(comboBoxBTRawBytes.Text.Substring(num, 2), NumberStyles.HexNumber);
				num++;
				num++;
				num2++;
			}
		}
		else
		{
			array = new byte[comboBoxBTRawBytes.Text.Length / 2];
			int num3 = 0;
			int num4 = 0;
			while (num3 < comboBoxBTRawBytes.Text.Length)
			{
				array[num4] = byte.Parse(comboBoxBTRawBytes.Text.Substring(num3, 2), NumberStyles.HexNumber);
				num3++;
				num3++;
				num4++;
			}
		}
		if (array.Length != 0)
		{
			btMainWorkerThread.SendRawBytes(array);
		}
		if (!string.IsNullOrWhiteSpace(comboBoxBTRawBytes.Text))
		{
			int num5 = comboBoxBTRawBytes.Items.IndexOf(comboBoxBTRawBytes.Text);
			comboBoxBTRawBytes.Items.Insert(0, comboBoxBTRawBytes.Text);
			if (num5 != -1)
			{
				comboBoxBTRawBytes.Items.RemoveAt(num5 + 1);
				comboBoxBTRawBytes.Text = comboBoxBTRawBytes.Items[0].ToString();
			}
		}
	}

	private bool ConfirmHex(string hexString)
	{
		Regex regex = new Regex("^[0-9A-F]+$", RegexOptions.IgnoreCase);
		return regex.IsMatch(hexString);
	}

	private void checkBoxNoProtocolID_CheckedChanged(object sender, EventArgs e)
	{
		if (checkBoxNoProtocolID.Checked)
		{
			comboBoxBTProtocolByte.Enabled = false;
		}
		else
		{
			comboBoxBTProtocolByte.Enabled = true;
		}
	}

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

	private void InitializeComponent()
	{
		this.buttonSendRawBytes = new System.Windows.Forms.Button();
		this.groupBoxRawBytesWithID = new System.Windows.Forms.GroupBox();
		this.label1 = new System.Windows.Forms.Label();
		this.comboBoxBTProtocolByte = new System.Windows.Forms.ComboBox();
		this.comboBoxBTRawBytes = new System.Windows.Forms.ComboBox();
		this.checkBoxNoProtocolID = new System.Windows.Forms.CheckBox();
		this.groupBoxRawBytesWithID.SuspendLayout();
		base.SuspendLayout();
		this.buttonSendRawBytes.Location = new System.Drawing.Point(7, 90);
		this.buttonSendRawBytes.Name = "buttonSendRawBytes";
		this.buttonSendRawBytes.Size = new System.Drawing.Size(393, 60);
		this.buttonSendRawBytes.TabIndex = 1;
		this.buttonSendRawBytes.Text = "Send";
		this.buttonSendRawBytes.UseVisualStyleBackColor = true;
		this.buttonSendRawBytes.Click += new System.EventHandler(buttonSendRawBytes_Click);
		this.groupBoxRawBytesWithID.Controls.Add(this.label1);
		this.groupBoxRawBytesWithID.Controls.Add(this.comboBoxBTProtocolByte);
		this.groupBoxRawBytesWithID.Controls.Add(this.comboBoxBTRawBytes);
		this.groupBoxRawBytesWithID.Controls.Add(this.checkBoxNoProtocolID);
		this.groupBoxRawBytesWithID.Controls.Add(this.buttonSendRawBytes);
		this.groupBoxRawBytesWithID.Location = new System.Drawing.Point(14, 16);
		this.groupBoxRawBytesWithID.Name = "groupBoxRawBytesWithID";
		this.groupBoxRawBytesWithID.Size = new System.Drawing.Size(413, 156);
		this.groupBoxRawBytesWithID.TabIndex = 2;
		this.groupBoxRawBytesWithID.TabStop = false;
		this.groupBoxRawBytesWithID.Text = "Send Raw Bytes";
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(203, 22);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(73, 13);
		this.label1.TabIndex = 5;
		this.label1.Text = "Protocol Byte:";
		this.comboBoxBTProtocolByte.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
		this.comboBoxBTProtocolByte.FormattingEnabled = true;
		this.comboBoxBTProtocolByte.Location = new System.Drawing.Point(279, 19);
		this.comboBoxBTProtocolByte.Name = "comboBoxBTProtocolByte";
		this.comboBoxBTProtocolByte.Size = new System.Drawing.Size(121, 21);
		this.comboBoxBTProtocolByte.TabIndex = 4;
		this.comboBoxBTRawBytes.FormattingEnabled = true;
		this.comboBoxBTRawBytes.Items.AddRange(new object[1] { "030C00" });
		this.comboBoxBTRawBytes.Location = new System.Drawing.Point(7, 50);
		this.comboBoxBTRawBytes.Name = "comboBoxBTRawBytes";
		this.comboBoxBTRawBytes.Size = new System.Drawing.Size(393, 21);
		this.comboBoxBTRawBytes.TabIndex = 3;
		this.comboBoxBTRawBytes.Text = "030C00";
		this.checkBoxNoProtocolID.AutoSize = true;
		this.checkBoxNoProtocolID.Location = new System.Drawing.Point(7, 21);
		this.checkBoxNoProtocolID.Name = "checkBoxNoProtocolID";
		this.checkBoxNoProtocolID.Size = new System.Drawing.Size(123, 17);
		this.checkBoxNoProtocolID.TabIndex = 2;
		this.checkBoxNoProtocolID.Text = "No Protocol ID Byte ";
		this.checkBoxNoProtocolID.UseVisualStyleBackColor = true;
		this.checkBoxNoProtocolID.CheckedChanged += new System.EventHandler(checkBoxNoProtocolID_CheckedChanged);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.groupBoxRawBytesWithID);
		base.Name = "BTSendRawBytesUserControl";
		base.Size = new System.Drawing.Size(515, 356);
		this.groupBoxRawBytesWithID.ResumeLayout(false);
		this.groupBoxRawBytesWithID.PerformLayout();
		base.ResumeLayout(false);
	}
}
