using System;
using System.Collections.Generic;
using System.ComponentModel;
using QCRPG.Framework;
using QCRPG.TSL.BT.Packets;

namespace QCRPG.TSL.BT;

public class BTScriptOverrideParams
{
	protected enum BTOverrideVariables
	{
		[Description("BD_ADDR")]
		BDAddress,
		[Description("connectionHandle")]
		ConnectionHandle,
		[Description("PageScanRepMode")]
		PageScanRepMode,
		[Description("PageScanMode")]
		PageScanMode,
		[Description("Packet_Type")]
		PacketType,
		[Description("pinCode")]
		PinCode,
		[Description("keyCode")]
		KeyCode,
		[Description("numResponses")]
		NumResponses,
		[Description("lap")]
		Lap,
		[Description("inqLength")]
		InqLength,
		[Description("maxPeriodLen")]
		MaxPeriodLen,
		[Description("minPeriodLen")]
		MinPeriodLen
	}

	protected Dictionary<int, string> overrideVariableNameMap = new Dictionary<int, string>();

	private ScripterBase scripter;

	private BTAPI btapi;

	private SpecificPacketNotifier txPacketNotifier = new SpecificPacketNotifier();

	public BTScriptOverrideParams(ScripterBase scripter, BTAPI btapi)
	{
		this.scripter = scripter;
		this.btapi = btapi;
		btapi.RegisterSendPacketObserver(txPacketNotifier);
		AddOverrideVariablesFromEnum(typeof(BTOverrideVariables));
		txPacketNotifier.AddPacketNotification(typeof(ReadRemoteVersionInformation), txPacketsStoreOverrideParams);
		txPacketNotifier.AddPacketNotification(typeof(ChangeConnectionPacketType), txPacketsStoreOverrideParams);
		txPacketNotifier.AddPacketNotification(typeof(CreateConnection), txPacketsStoreOverrideParams);
		txPacketNotifier.AddPacketNotification(typeof(PinCodeRequestReply), txPacketsStoreOverrideParams);
		txPacketNotifier.AddPacketNotification(typeof(LinkKeyRequestReply), txPacketsStoreOverrideParams);
		txPacketNotifier.AddPacketNotification(typeof(Inquiry), txPacketsStoreOverrideParams);
		txPacketNotifier.AddPacketNotification(typeof(PeriodicInquiryMode), txPacketsStoreOverrideParams);
	}

	private void AddOverrideVariablesFromEnum(Type enumType)
	{
		Array values = Enum.GetValues(enumType);
		foreach (int item in values)
		{
			object obj = Enum.Parse(enumType, Enum.GetName(enumType, item));
			string descriptionOfEnum = HelperUtils.GetDescriptionOfEnum((Enum)obj);
			overrideVariableNameMap.Add(item, descriptionOfEnum);
		}
	}

	public void rxPacketsStoreOverrideParams(PacketBase pkt)
	{
	}

	public void txPacketsStoreOverrideParams(PacketBase pkt)
	{
		if (pkt is ReadRemoteVersionInformation)
		{
			ReadRemoteVersionInformation readRemoteVersionInformation = (ReadRemoteVersionInformation)pkt;
			scripter.SetOverrideParamVariable(overrideVariableNameMap[1], "0x" + readRemoteVersionInformation.UserFields.ConnectionHandle.ToString("X4"));
		}
		if (pkt is ChangeConnectionPacketType)
		{
			ChangeConnectionPacketType changeConnectionPacketType = (ChangeConnectionPacketType)pkt;
			scripter.SetOverrideParamVariable(overrideVariableNameMap[1], "0x" + changeConnectionPacketType.UserFields.ConnectionHandle.ToString("X4"));
		}
		else if (pkt is CreateConnection)
		{
			CreateConnection createConnection = (CreateConnection)pkt;
			ulong bdAddress = createConnection.UserFields.BdAddress;
			string value = "\"" + ((byte)((bdAddress & 0xFF0000000000L) >> 40)).ToString("X2") + " " + ((byte)((bdAddress & 0xFF00000000L) >> 32)).ToString("X2") + " " + ((byte)((bdAddress & 0xFF000000u) >> 24)).ToString("X2") + " " + ((byte)((bdAddress & 0xFF0000) >> 16)).ToString("X2") + " " + ((byte)((bdAddress & 0xFF00) >> 8)).ToString("X2") + " " + ((byte)(bdAddress & 0xFF)).ToString("X2") + "\"";
			scripter.SetOverrideParamVariable(overrideVariableNameMap[0], value);
			scripter.SetOverrideParamVariable(overrideVariableNameMap[2], "0x" + createConnection.UserFields.PageScanRepetitionMode.ToString("X2"));
			scripter.SetOverrideParamVariable(overrideVariableNameMap[3], "0x" + createConnection.UserFields.Reserved.ToString("X2"));
			scripter.SetOverrideParamVariable(overrideVariableNameMap[4], "0x" + createConnection.UserFields.PacketTypeMask.ToString("X2"));
		}
		else if (pkt is PinCodeRequestReply)
		{
			PinCodeRequestReply pinCodeRequestReply = (PinCodeRequestReply)pkt;
			string text = "\"";
			for (int i = 0; i < pinCodeRequestReply.UserFields.PinCodeLength; i++)
			{
				text += pinCodeRequestReply.UserFields.PinCodeList[i].ToString("X2 ");
			}
			text += "\"";
			scripter.SetOverrideParamVariable(overrideVariableNameMap[5], text);
		}
		else if (pkt is LinkKeyRequestReply)
		{
			LinkKeyRequestReply linkKeyRequestReply = (LinkKeyRequestReply)pkt;
			string text2 = "\"";
			for (int num = linkKeyRequestReply.UserFields.LinkKeyList.Count - 1; num >= 0; num--)
			{
				text2 += linkKeyRequestReply.UserFields.LinkKeyList[num].ToString("X2 ");
			}
			text2 += "\"";
			scripter.SetOverrideParamVariable(overrideVariableNameMap[6], text2);
		}
		else if (pkt is Inquiry)
		{
			Inquiry inquiry = (Inquiry)pkt;
			string value2 = "\"" + ((inquiry.UserFields.Lap & 0xFF0000) >> 16).ToString("X2") + " " + ((inquiry.UserFields.Lap & 0xFF00) >> 8).ToString("X2") + " " + (inquiry.UserFields.Lap & 0xFFu).ToString("X2") + "\"";
			scripter.SetOverrideParamVariable(overrideVariableNameMap[7], "0x" + inquiry.UserFields.NumResponses.ToString("X2"));
			scripter.SetOverrideParamVariable(overrideVariableNameMap[8], value2);
			scripter.SetOverrideParamVariable(overrideVariableNameMap[9], "0x" + inquiry.UserFields.InquiryLength.ToString("X2"));
		}
		else if (pkt is PeriodicInquiryMode)
		{
			PeriodicInquiryMode periodicInquiryMode = (PeriodicInquiryMode)pkt;
			scripter.SetOverrideParamVariable(overrideVariableNameMap[10], "0x" + periodicInquiryMode.UserFields.MaxPeriodLength.ToString("X2"));
			scripter.SetOverrideParamVariable(overrideVariableNameMap[11], "0x" + periodicInquiryMode.UserFields.MinPeriodLength.ToString("X2"));
		}
	}
}
