using System;
using System.Collections.Generic;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LinkKeyNotification : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum KeyTypeEnum
		{
			COMBINATION_KEY,
			LOCAL_UNIT_KEY,
			REMOTE_KEY,
			DEBUG_COMBINATION_KEY,
			UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_192,
			AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_192,
			CHANGED_COMBINATION_KEY,
			UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_256,
			AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_256,
			Reserved,
			InvalidKeyTypeValue
		}

		public static class KeyTypeInfo
		{
			public static KeyTypeEnum ValueToKeyType(uint value)
			{
				switch (value)
				{
				case 0u:
					return KeyTypeEnum.COMBINATION_KEY;
				case 1u:
					return KeyTypeEnum.LOCAL_UNIT_KEY;
				case 2u:
					return KeyTypeEnum.REMOTE_KEY;
				case 3u:
					return KeyTypeEnum.DEBUG_COMBINATION_KEY;
				case 4u:
					return KeyTypeEnum.UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_192;
				case 5u:
					return KeyTypeEnum.AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_192;
				case 6u:
					return KeyTypeEnum.CHANGED_COMBINATION_KEY;
				case 7u:
					return KeyTypeEnum.UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_256;
				case 8u:
					return KeyTypeEnum.AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_256;
				default:
					if (value >= 9 && value <= 255)
					{
						return KeyTypeEnum.Reserved;
					}
					return KeyTypeEnum.InvalidKeyTypeValue;
				}
			}

			public static uint KeyTypeToValue(KeyTypeEnum enumItem)
			{
				return enumItem switch
				{
					KeyTypeEnum.COMBINATION_KEY => 0u, 
					KeyTypeEnum.LOCAL_UNIT_KEY => 1u, 
					KeyTypeEnum.REMOTE_KEY => 2u, 
					KeyTypeEnum.DEBUG_COMBINATION_KEY => 3u, 
					KeyTypeEnum.UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_192 => 4u, 
					KeyTypeEnum.AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_192 => 5u, 
					KeyTypeEnum.CHANGED_COMBINATION_KEY => 6u, 
					KeyTypeEnum.UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_256 => 7u, 
					KeyTypeEnum.AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_256 => 8u, 
					KeyTypeEnum.Reserved => 9u, 
					_ => uint.MaxValue, 
				};
			}

			public static string KeyTypeToString(KeyTypeEnum enumItem)
			{
				return enumItem switch
				{
					KeyTypeEnum.COMBINATION_KEY => "Combination Key", 
					KeyTypeEnum.LOCAL_UNIT_KEY => "Local Unit Key", 
					KeyTypeEnum.REMOTE_KEY => "Remote Key", 
					KeyTypeEnum.DEBUG_COMBINATION_KEY => "Debug Combination Key", 
					KeyTypeEnum.UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_192 => "Unauthenticated Combination Key generated from P-192", 
					KeyTypeEnum.AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_192 => "Authenticated Combination Key generated from P-192", 
					KeyTypeEnum.CHANGED_COMBINATION_KEY => "Changed Combination Key", 
					KeyTypeEnum.UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_256 => "Unauthenticated Combination Key generated from P-256", 
					KeyTypeEnum.AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P_256 => "Authenticated Combination Key generated from P-256", 
					KeyTypeEnum.Reserved => "Reserved", 
					_ => "Invalid KeyType Value", 
				};
			}

			public static string KeyTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Combination Key");
				stringBuilder.AppendLine("0x1 = Local Unit Key");
				stringBuilder.AppendLine("0x2 = Remote Key");
				stringBuilder.AppendLine("0x3 = Debug Combination Key");
				stringBuilder.AppendLine("0x4 = Unauthenticated Combination Key generated from P-192");
				stringBuilder.AppendLine("0x5 = Authenticated Combination Key generated from P-192");
				stringBuilder.AppendLine("0x6 = Changed Combination Key");
				stringBuilder.AppendLine("0x7 = Unauthenticated Combination Key generated from P-256");
				stringBuilder.AppendLine("0x8 = Authenticated Combination Key generated from P-256");
				stringBuilder.AppendLine("0x9-0xFF = Reserved");
				return stringBuilder.ToString();
			}
		}

		private ulong _BdAddr;

		private List<uint> _LinkKeyList = new List<uint>(new uint[16]);

		private uint _KeyType;

		public ulong BdAddr
		{
			get
			{
				return _BdAddr;
			}
			set
			{
				_BdAddr = value;
			}
		}

		public List<uint> LinkKeyList => _LinkKeyList;

		public uint KeyType
		{
			get
			{
				return _KeyType;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for KeyType is 0xFF");
				}
				_KeyType = value;
			}
		}

		public KeyTypeEnum GetKeyTypeAsKeyTypeEnum()
		{
			return KeyTypeInfo.ValueToKeyType(_KeyType);
		}

		public void SetKeyTypeAsKeyTypeEnum(KeyTypeEnum enumItem)
		{
			KeyType = KeyTypeInfo.KeyTypeToValue(enumItem);
		}
	}

	private const uint _EventCode = 24u;

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new LinkKeyNotificationInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 24u;

	public uint PayloadLength
	{
		get
		{
			ToBitString();
			return _PayloadLength;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		for (int num = UserFields.LinkKeyList.Count - 1; num >= 0; num--)
		{
		}
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != EventCode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.BdAddr = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		for (int num = UserFields.LinkKeyList.Count - 1; num >= 0; num--)
		{
			UserFields.LinkKeyList[num] = bitBuilder.RemoveBitsAsUint(8);
		}
		UserFields.KeyType = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(EventCode, 8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.BdAddr, 48);
		for (int num = UserFields.LinkKeyList.Count - 1; num >= 0; num--)
		{
			if (UserFields.LinkKeyList[num] > 255)
			{
				throw new Exception("UserFields.LinkKeyList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.LinkKeyList[num], 8);
		}
		bitBuilder.AddBitsAsUint(UserFields.KeyType, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
