using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

namespace QCRPG.TSL.BT;

public class SniffParameters : Form
{
	private int maxInterval = 2048;

	private int minInterval = 2048;

	private int sniffAttempt = 8;

	private int sniffTimeout = 1;

	private IContainer components;

	private GroupBox groupBox1;

	private Button okButton;

	private Button cancelButton;

	private TextBox sniffTimeoutTextBox;

	private TextBox sniffAttempTextBox;

	private Label label4;

	private Label label3;

	private RadioButton valueInSlotRadioButton;

	private TextBox sniffMinIntervalTextBox;

	private TextBox sniffMaxIntervalTextBox;

	private RadioButton valueInSecRadioButton;

	private Label label2;

	private Label label1;

	public int MaxInterval
	{
		get
		{
			return maxInterval;
		}
		set
		{
			maxInterval = value;
		}
	}

	public int MinInterval
	{
		get
		{
			return minInterval;
		}
		set
		{
			minInterval = value;
		}
	}

	public int SniffAttempt
	{
		get
		{
			return sniffAttempt;
		}
		set
		{
			sniffAttempt = value;
		}
	}

	public int SniffTimeout
	{
		get
		{
			return sniffTimeout;
		}
		set
		{
			sniffTimeout = value;
		}
	}

	public SniffParameters()
	{
		InitializeComponent();
	}

	private void valueInSecRadioButton_CheckedChanged(object sender, EventArgs e)
	{
		if (valueInSecRadioButton.Checked)
		{
			string text = sniffMaxIntervalTextBox.Text.ToString();
			string text2 = sniffMinIntervalTextBox.Text.ToString();
			int.TryParse(text, NumberStyles.HexNumber, null, out maxInterval);
			if (maxInterval > 65534)
			{
				text = "FFFE";
			}
			int.TryParse(text2, NumberStyles.HexNumber, null, out minInterval);
			if (minInterval > 65534)
			{
				text = "FFFE";
			}
			sniffMaxIntervalTextBox.Text = ConvertSlotValueToSecond(text);
			sniffMinIntervalTextBox.Text = ConvertSlotValueToSecond(text2);
		}
	}

	private void valueInSlotRadioButton_CheckedChanged(object sender, EventArgs e)
	{
		if (valueInSlotRadioButton.Checked)
		{
			string second = sniffMaxIntervalTextBox.Text.ToString();
			string second2 = sniffMinIntervalTextBox.Text.ToString();
			second = ConvertSecondValueToSlot(second);
			second2 = ConvertSecondValueToSlot(second2);
			int.TryParse(second, NumberStyles.HexNumber, null, out maxInterval);
			if (maxInterval > 65534)
			{
				second = "FFFE";
			}
			int.TryParse(second2, NumberStyles.HexNumber, null, out minInterval);
			if (minInterval > 65534)
			{
				second2 = "FFFE";
			}
			sniffMaxIntervalTextBox.Text = second;
			sniffMinIntervalTextBox.Text = second2;
		}
	}

	private void okButton_Click(object sender, EventArgs e)
	{
		string text = sniffMaxIntervalTextBox.Text.ToString();
		string text2 = sniffMinIntervalTextBox.Text.ToString();
		string s = sniffAttempTextBox.Text.ToString();
		string s2 = sniffTimeoutTextBox.Text.ToString();
		if (valueInSecRadioButton.Checked)
		{
			text = ConvertSlotValueToSecond(text);
			text2 = ConvertSlotValueToSecond(text2);
		}
		int.TryParse(text, NumberStyles.HexNumber, null, out maxInterval);
		if (maxInterval > 65534)
		{
			maxInterval = 65534;
		}
		int.TryParse(text2, NumberStyles.HexNumber, null, out minInterval);
		if (minInterval > 65534)
		{
			minInterval = 65534;
		}
		int.TryParse(s, NumberStyles.HexNumber, null, out sniffAttempt);
		if (sniffAttempt > 32767)
		{
			sniffAttempt = 32767;
		}
		int.TryParse(s2, NumberStyles.HexNumber, null, out sniffTimeout);
		if (sniffTimeout > 32767)
		{
			sniffTimeout = 32767;
		}
	}

	private string ConvertSlotValueToSecond(string slot)
	{
		int result = 0;
		int.TryParse(slot, NumberStyles.HexNumber, null, out result);
		return ((double)result * 0.625 / 1000.0).ToString("F6");
	}

	private string ConvertSecondValueToSlot(string second)
	{
		double num = Convert.ToDouble(second);
		num = Math.Ceiling(num * 1000.0 / 0.625);
		return ((int)num).ToString("X4");
	}

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

	private void InitializeComponent()
	{
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.sniffTimeoutTextBox = new System.Windows.Forms.TextBox();
		this.sniffAttempTextBox = new System.Windows.Forms.TextBox();
		this.label4 = new System.Windows.Forms.Label();
		this.label3 = new System.Windows.Forms.Label();
		this.valueInSlotRadioButton = new System.Windows.Forms.RadioButton();
		this.sniffMinIntervalTextBox = new System.Windows.Forms.TextBox();
		this.sniffMaxIntervalTextBox = new System.Windows.Forms.TextBox();
		this.valueInSecRadioButton = new System.Windows.Forms.RadioButton();
		this.label2 = new System.Windows.Forms.Label();
		this.label1 = new System.Windows.Forms.Label();
		this.okButton = new System.Windows.Forms.Button();
		this.cancelButton = new System.Windows.Forms.Button();
		this.groupBox1.SuspendLayout();
		base.SuspendLayout();
		this.groupBox1.Controls.Add(this.sniffTimeoutTextBox);
		this.groupBox1.Controls.Add(this.sniffAttempTextBox);
		this.groupBox1.Controls.Add(this.label4);
		this.groupBox1.Controls.Add(this.label3);
		this.groupBox1.Controls.Add(this.valueInSlotRadioButton);
		this.groupBox1.Controls.Add(this.sniffMinIntervalTextBox);
		this.groupBox1.Controls.Add(this.sniffMaxIntervalTextBox);
		this.groupBox1.Controls.Add(this.valueInSecRadioButton);
		this.groupBox1.Controls.Add(this.label2);
		this.groupBox1.Controls.Add(this.label1);
		this.groupBox1.Location = new System.Drawing.Point(13, 13);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Size = new System.Drawing.Size(259, 194);
		this.groupBox1.TabIndex = 0;
		this.groupBox1.TabStop = false;
		this.sniffTimeoutTextBox.Location = new System.Drawing.Point(127, 153);
		this.sniffTimeoutTextBox.MaxLength = 4;
		this.sniffTimeoutTextBox.Name = "sniffTimeoutTextBox";
		this.sniffTimeoutTextBox.Size = new System.Drawing.Size(100, 20);
		this.sniffTimeoutTextBox.TabIndex = 9;
		this.sniffTimeoutTextBox.Text = "0001";
		this.sniffTimeoutTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.sniffAttempTextBox.Location = new System.Drawing.Point(127, 118);
		this.sniffAttempTextBox.MaxLength = 4;
		this.sniffAttempTextBox.Name = "sniffAttempTextBox";
		this.sniffAttempTextBox.Size = new System.Drawing.Size(100, 20);
		this.sniffAttempTextBox.TabIndex = 8;
		this.sniffAttempTextBox.Text = "0008";
		this.sniffAttempTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.label4.AutoSize = true;
		this.label4.Location = new System.Drawing.Point(6, 156);
		this.label4.Name = "label4";
		this.label4.Size = new System.Drawing.Size(97, 13);
		this.label4.TabIndex = 7;
		this.label4.Text = "Sniff Timeout (Hex)";
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(6, 121);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(92, 13);
		this.label3.TabIndex = 6;
		this.label3.Text = "Sniff Attemp (Hex)";
		this.valueInSlotRadioButton.AutoSize = true;
		this.valueInSlotRadioButton.Checked = true;
		this.valueInSlotRadioButton.Location = new System.Drawing.Point(138, 82);
		this.valueInSlotRadioButton.Name = "valueInSlotRadioButton";
		this.valueInSlotRadioButton.Size = new System.Drawing.Size(89, 17);
		this.valueInSlotRadioButton.TabIndex = 5;
		this.valueInSlotRadioButton.TabStop = true;
		this.valueInSlotRadioButton.Text = "Value in Slots";
		this.valueInSlotRadioButton.UseVisualStyleBackColor = true;
		this.valueInSlotRadioButton.CheckedChanged += new System.EventHandler(valueInSlotRadioButton_CheckedChanged);
		this.sniffMinIntervalTextBox.Location = new System.Drawing.Point(127, 45);
		this.sniffMinIntervalTextBox.MaxLength = 8;
		this.sniffMinIntervalTextBox.Name = "sniffMinIntervalTextBox";
		this.sniffMinIntervalTextBox.Size = new System.Drawing.Size(100, 20);
		this.sniffMinIntervalTextBox.TabIndex = 4;
		this.sniffMinIntervalTextBox.Text = "0800";
		this.sniffMinIntervalTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.sniffMaxIntervalTextBox.Location = new System.Drawing.Point(127, 13);
		this.sniffMaxIntervalTextBox.MaxLength = 8;
		this.sniffMaxIntervalTextBox.Name = "sniffMaxIntervalTextBox";
		this.sniffMaxIntervalTextBox.Size = new System.Drawing.Size(100, 20);
		this.sniffMaxIntervalTextBox.TabIndex = 3;
		this.sniffMaxIntervalTextBox.Text = "0800";
		this.sniffMaxIntervalTextBox.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.valueInSecRadioButton.AutoSize = true;
		this.valueInSecRadioButton.Location = new System.Drawing.Point(12, 82);
		this.valueInSecRadioButton.Name = "valueInSecRadioButton";
		this.valueInSecRadioButton.Size = new System.Drawing.Size(108, 17);
		this.valueInSecRadioButton.TabIndex = 2;
		this.valueInSecRadioButton.Text = "Value in Seconds";
		this.valueInSecRadioButton.UseVisualStyleBackColor = true;
		this.valueInSecRadioButton.CheckedChanged += new System.EventHandler(valueInSecRadioButton_CheckedChanged);
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(6, 48);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(114, 13);
		this.label2.TabIndex = 1;
		this.label2.Text = "Sniff Min Interval (Hex)";
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(6, 16);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(117, 13);
		this.label1.TabIndex = 0;
		this.label1.Text = "Sniff Max Interval (Hex)";
		this.okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
		this.okButton.Location = new System.Drawing.Point(36, 223);
		this.okButton.Name = "okButton";
		this.okButton.Size = new System.Drawing.Size(75, 23);
		this.okButton.TabIndex = 1;
		this.okButton.Text = "OK";
		this.okButton.UseVisualStyleBackColor = true;
		this.okButton.Click += new System.EventHandler(okButton_Click);
		this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
		this.cancelButton.Location = new System.Drawing.Point(174, 223);
		this.cancelButton.Name = "cancelButton";
		this.cancelButton.Size = new System.Drawing.Size(75, 23);
		this.cancelButton.TabIndex = 2;
		this.cancelButton.Text = "Cancel";
		this.cancelButton.UseVisualStyleBackColor = true;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(284, 262);
		base.Controls.Add(this.cancelButton);
		base.Controls.Add(this.okButton);
		base.Controls.Add(this.groupBox1);
		base.Name = "SniffParameters";
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "Sniff Parameters";
		this.groupBox1.ResumeLayout(false);
		this.groupBox1.PerformLayout();
		base.ResumeLayout(false);
	}
}
