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.ZigBee;

public class SendRawBytesUserControl : UserControlBase, IConfigurable
{
	private ZigBeeMainWorkerThread zigBeeMainWorkerThread;

	private IContainer components;

	private Button buttonSendRawBytes;

	private GroupBox groupBoxRawBytesWithID;

	private ComboBox comboBoxRawBytes;

	public SendRawBytesUserControl(ZigBeeMainWorkerThread zigBeeMainWorkerThread)
		: base("Send Raw 15.4 Bytes")
	{
		InitializeComponent();
		this.zigBeeMainWorkerThread = zigBeeMainWorkerThread;
	}

	public Configuration GetConfiguration()
	{
		Configuration configuration = new Configuration();
		int count = comboBoxRawBytes.Items.Count;
		configuration.SetInt("RawBytePacketCount", count);
		for (int i = 1; i <= count; i++)
		{
			string name = $"RawBytePacket{i}";
			configuration.SetString(name, comboBoxRawBytes.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, comboBoxRawBytes.Text);
			if (!comboBoxRawBytes.Items.Contains(@string))
			{
				comboBoxRawBytes.Items.Add(@string);
			}
		}
	}

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

	private void textBoxBTRawBytes_TextChanged(object sender, EventArgs e)
	{
		if (!string.IsNullOrEmpty(comboBoxRawBytes.Text) && !ConfirmHex(comboBoxRawBytes.Text.ToString().Replace(" ", "")))
		{
			MessageBox.Show("Value is not HEX");
		}
		comboBoxRawBytes.Text = comboBoxRawBytes.Text.Replace(" ", "");
	}

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

	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.comboBoxRawBytes = new System.Windows.Forms.ComboBox();
		this.groupBoxRawBytesWithID.SuspendLayout();
		base.SuspendLayout();
		this.buttonSendRawBytes.Location = new System.Drawing.Point(6, 73);
		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.comboBoxRawBytes);
		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, 141);
		this.groupBoxRawBytesWithID.TabIndex = 2;
		this.groupBoxRawBytesWithID.TabStop = false;
		this.groupBoxRawBytesWithID.Text = "Send Raw Bytes";
		this.comboBoxRawBytes.FormattingEnabled = true;
		this.comboBoxRawBytes.Items.AddRange(new object[1] { "10000000050000000126010000" });
		this.comboBoxRawBytes.Location = new System.Drawing.Point(7, 36);
		this.comboBoxRawBytes.Name = "comboBoxRawBytes";
		this.comboBoxRawBytes.Size = new System.Drawing.Size(393, 21);
		this.comboBoxRawBytes.TabIndex = 4;
		this.comboBoxRawBytes.Text = "10000000050000000126010000";
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.groupBoxRawBytesWithID);
		base.Name = "SendRawBytesUserControl";
		base.Size = new System.Drawing.Size(515, 356);
		this.groupBoxRawBytesWithID.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
