using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using QCRPG.Framework;
using QCRPG.TSL.BT.Packets;

namespace QCRPG.TSL.BT;

public class SCOoverHCIUserControl : UserControlBase
{
	private const int SCO_DUMMYPACKETS_THRESHOLD = 10;

	private int aclConnectionHandle;

	private int currentSCOConnectionHandler;

	private string remoteBDAddr = "";

	private Queue<SynchronousData> packetBuffer;

	private uint frequency = 1000u;

	private uint amplitude = 100u;

	private uint sampleSize = 16u;

	private uint sampleRate = 8000u;

	private uint durationSec = 30u;

	private bool saveTxToFile;

	private bool saveDummyPackets = true;

	private BTAPI btApi;

	private BTGooier btGooier;

	private BTMainWorkerThread btMainWorkerThread;

	private Logger logger;

	private SpecificPacketNotifier specificPacketNotifier;

	private SCODataQueue scoDataQueue;

	private IContainer components;

	private Button scoHciOptionsButton;

	private Button buttonStartTx;

	private Button buttonStopTx;

	private Button buttonEnableRx;

	public int AclConnectionHandle
	{
		set
		{
			aclConnectionHandle = value;
		}
	}

	public int CurrentSCOConnectionHandler
	{
		set
		{
			currentSCOConnectionHandler = value;
		}
	}

	public string RemoteBDAddress
	{
		set
		{
			remoteBDAddr = value;
		}
	}

	public SCOoverHCIUserControl(BTMainWorkerThread btMainWorkerThread, SpecificPacketNotifier specificPacketNotifier, Logger logger, BTAPI btApi, BTGooier btGooier)
		: base("SCO over HCI")
	{
		this.btMainWorkerThread = btMainWorkerThread;
		this.logger = logger;
		this.specificPacketNotifier = specificPacketNotifier;
		InitializeComponent();
		packetBuffer = new Queue<SynchronousData>();
		this.btApi = btApi;
		this.btGooier = btGooier;
		scoDataQueue = new SCODataQueue(0, specificPacketNotifier, btApi, logger);
	}

	private void scoHciOptionsButton_Click(object sender, EventArgs e)
	{
		SCOoverHCIOptions sCOoverHCIOptions = new SCOoverHCIOptions();
		sCOoverHCIOptions.Frequency = frequency;
		sCOoverHCIOptions.Amplitude = amplitude;
		sCOoverHCIOptions.SampleRate = sampleRate;
		sCOoverHCIOptions.SampleSize = sampleSize;
		sCOoverHCIOptions.SaveTxFile = saveTxToFile;
		sCOoverHCIOptions.SaveDummyPackets = saveDummyPackets;
		sCOoverHCIOptions.DurationSec = durationSec;
		sCOoverHCIOptions.ShowDialog();
		if (sCOoverHCIOptions.DialogResult == DialogResult.OK)
		{
			frequency = sCOoverHCIOptions.Frequency;
			amplitude = sCOoverHCIOptions.Amplitude;
			sampleRate = sCOoverHCIOptions.SampleRate;
			sampleSize = sCOoverHCIOptions.SampleSize;
			saveTxToFile = sCOoverHCIOptions.SaveTxFile;
			saveDummyPackets = sCOoverHCIOptions.SaveDummyPackets;
			durationSec = sCOoverHCIOptions.DurationSec;
		}
	}

	private void buttonStartTx_Click(object sender, EventArgs e)
	{
		SetupxSCOConnection();
		btApi.SetDecodePacket(decodePacket: false, packetResponse: false, conciseLog: true);
		while (currentSCOConnectionHandler == 0)
		{
			Thread.Sleep(50);
		}
		scoDataQueue.CurrentSCOConnectionHandler = currentSCOConnectionHandler;
		logger.Enqueue(new LogMessage(LogMessageType.Info, "Starting SCO data to handle: " + currentSCOConnectionHandler));
		buttonStartTx.Enabled = false;
		buttonStopTx.Enabled = true;
		scoDataQueue.RemoteBDAddress = remoteBDAddr;
		scoDataQueue.StartScoOverHciTx(frequency, amplitude, sampleSize, sampleRate, durationSec);
	}

	private void SetupxSCOConnection()
	{
		int result = 0;
		int.TryParse(btGooier.voiceSetting, NumberStyles.HexNumber, null, out result);
		int num = btGooier.CalculatePacketTypeForESCOConnection();
		int retransmissionEffort = btGooier.CalculateRetransmissionEffortForESCOConnection();
		btGooier.AddESCOConnectionHandler(aclConnectionHandle, result, retransmissionEffort, num);
		btGooier.CurrentBTPacketType = (uint)num;
	}

	private void buttonStopTx_Click(object sender, EventArgs e)
	{
		Disconnect disconnect = new Disconnect();
		disconnect._UserFields.ConnectionHandle = (uint)currentSCOConnectionHandler;
		disconnect.UserFields.SetReasonAsReasonEnum(Disconnect.UserFieldsWrapper.ReasonEnum.REMOTE_USER_TERMINATED_CONNECTION);
		btApi.Send(disconnect);
		buttonStartTx.Enabled = true;
		buttonStopTx.Enabled = false;
		scoDataQueue.StopScoOverHciTx();
		currentSCOConnectionHandler = 0;
	}

	private void buttonEnableRx_Click(object sender, EventArgs e)
	{
		scoDataQueue.EnableRx();
	}

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

	private void InitializeComponent()
	{
		this.scoHciOptionsButton = new System.Windows.Forms.Button();
		this.buttonStartTx = new System.Windows.Forms.Button();
		this.buttonStopTx = new System.Windows.Forms.Button();
		this.buttonEnableRx = new System.Windows.Forms.Button();
		base.SuspendLayout();
		this.scoHciOptionsButton.Location = new System.Drawing.Point(79, 235);
		this.scoHciOptionsButton.Name = "scoHciOptionsButton";
		this.scoHciOptionsButton.Size = new System.Drawing.Size(75, 23);
		this.scoHciOptionsButton.TabIndex = 0;
		this.scoHciOptionsButton.Text = "Options...";
		this.scoHciOptionsButton.UseVisualStyleBackColor = true;
		this.scoHciOptionsButton.Click += new System.EventHandler(scoHciOptionsButton_Click);
		this.buttonStartTx.Location = new System.Drawing.Point(396, 235);
		this.buttonStartTx.Name = "buttonStartTx";
		this.buttonStartTx.Size = new System.Drawing.Size(75, 23);
		this.buttonStartTx.TabIndex = 1;
		this.buttonStartTx.Text = "Start Tx";
		this.buttonStartTx.UseVisualStyleBackColor = true;
		this.buttonStartTx.Click += new System.EventHandler(buttonStartTx_Click);
		this.buttonStopTx.Enabled = false;
		this.buttonStopTx.Location = new System.Drawing.Point(494, 235);
		this.buttonStopTx.Name = "buttonStopTx";
		this.buttonStopTx.Size = new System.Drawing.Size(75, 23);
		this.buttonStopTx.TabIndex = 2;
		this.buttonStopTx.Text = "Stop Tx";
		this.buttonStopTx.UseVisualStyleBackColor = true;
		this.buttonStopTx.Click += new System.EventHandler(buttonStopTx_Click);
		this.buttonEnableRx.Location = new System.Drawing.Point(244, 235);
		this.buttonEnableRx.Name = "buttonEnableRx";
		this.buttonEnableRx.Size = new System.Drawing.Size(75, 23);
		this.buttonEnableRx.TabIndex = 3;
		this.buttonEnableRx.Text = "Enable Rx";
		this.buttonEnableRx.UseVisualStyleBackColor = true;
		this.buttonEnableRx.Click += new System.EventHandler(buttonEnableRx_Click);
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.buttonEnableRx);
		base.Controls.Add(this.buttonStopTx);
		base.Controls.Add(this.buttonStartTx);
		base.Controls.Add(this.scoHciOptionsButton);
		base.Name = "SCOoverHCIUserControl";
		base.Size = new System.Drawing.Size(646, 310);
		base.ResumeLayout(false);
	}
}
