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

namespace QCRPG.TSL.BT.Packets;

public class LeAoaReport : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum RxPhyEnum
		{
			The_receiver_PHY_for_the_connection_is_1_Msps,
			The_receiver_PHY_for_the_connection_is_2_Msps,
			Reserved_for_future_use,
			InvalidRxPhyValue
		}

		public static class RxPhyInfo
		{
			public static RxPhyEnum ValueToRxPhy(uint value)
			{
				switch (value)
				{
				case 0u:
					return RxPhyEnum.The_receiver_PHY_for_the_connection_is_1_Msps;
				case 1u:
					return RxPhyEnum.The_receiver_PHY_for_the_connection_is_2_Msps;
				default:
					if (value >= 2 && value <= 255)
					{
						return RxPhyEnum.Reserved_for_future_use;
					}
					return RxPhyEnum.InvalidRxPhyValue;
				}
			}

			public static uint RxPhyToValue(RxPhyEnum enumItem)
			{
				return enumItem switch
				{
					RxPhyEnum.The_receiver_PHY_for_the_connection_is_1_Msps => 0u, 
					RxPhyEnum.The_receiver_PHY_for_the_connection_is_2_Msps => 1u, 
					RxPhyEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string RxPhyToString(RxPhyEnum enumItem)
			{
				return enumItem switch
				{
					RxPhyEnum.The_receiver_PHY_for_the_connection_is_1_Msps => "The receiver PHY for the connection is 1 Ms/s", 
					RxPhyEnum.The_receiver_PHY_for_the_connection_is_2_Msps => "The receiver PHY for the connection is 2 Ms/s", 
					RxPhyEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid RxPhy Value", 
				};
			}

			public static string RxPhyToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = The receiver PHY for the connection is 1 Ms/s");
				stringBuilder.AppendLine("0x1 = The receiver PHY for the connection is 2 Ms/s");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		public enum SupplementalTypeEnum
		{
			AoA_Supplemental,
			Reserved_for_future_use,
			InvalidSupplementalTypeValue
		}

		public static class SupplementalTypeInfo
		{
			public static SupplementalTypeEnum ValueToSupplementalType(uint value)
			{
				if (value == 0)
				{
					return SupplementalTypeEnum.AoA_Supplemental;
				}
				if (value >= 1 && value <= 255)
				{
					return SupplementalTypeEnum.Reserved_for_future_use;
				}
				return SupplementalTypeEnum.InvalidSupplementalTypeValue;
			}

			public static uint SupplementalTypeToValue(SupplementalTypeEnum enumItem)
			{
				return enumItem switch
				{
					SupplementalTypeEnum.AoA_Supplemental => 0u, 
					SupplementalTypeEnum.Reserved_for_future_use => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SupplementalTypeToString(SupplementalTypeEnum enumItem)
			{
				return enumItem switch
				{
					SupplementalTypeEnum.AoA_Supplemental => "AoA Supplemental", 
					SupplementalTypeEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid SupplementalType Value", 
				};
			}

			public static string SupplementalTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = AoA Supplemental");
				stringBuilder.AppendLine("0x1-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubeventCode = 0u;

		private uint _ConnectionHandle = 3839u;

		private uint _RxPhy;

		private uint _Rssi;

		private uint _SupplementalType;

		private uint _DataChannelIndex = 36u;

		private uint _SampleCount;

		private List<SamplesSubField> _SamplesList = new List<SamplesSubField>();

		public uint SubeventCode => 0u;

		public uint ConnectionHandle
		{
			get
			{
				return _ConnectionHandle;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for ConnectionHandle is 0xFFFF");
				}
				_ConnectionHandle = value;
			}
		}

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

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

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

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

		public uint SampleCount
		{
			get
			{
				return _SampleCount;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for SampleCount is 0xFF");
				}
				_SampleCount = value;
				while (_SamplesList.Count < _SampleCount)
				{
					_SamplesList.Add(new SamplesSubField());
				}
				while (_SamplesList.Count > _SampleCount)
				{
					_SamplesList.RemoveAt(_SamplesList.Count - 1);
				}
			}
		}

		public List<SamplesSubField> SamplesList => _SamplesList;

		public RxPhyEnum GetRxPhyAsRxPhyEnum()
		{
			return RxPhyInfo.ValueToRxPhy(_RxPhy);
		}

		public void SetRxPhyAsRxPhyEnum(RxPhyEnum enumItem)
		{
			RxPhy = RxPhyInfo.RxPhyToValue(enumItem);
		}

		public AdvertisingRssiEnum GetRssiAsAdvertisingRssiEnum()
		{
			return AdvertisingRssiInfo.ValueToAdvertisingRssi(_Rssi);
		}

		public void SetRssiAsAdvertisingRssiEnum(AdvertisingRssiEnum enumItem)
		{
			Rssi = AdvertisingRssiInfo.AdvertisingRssiToValue(enumItem);
		}

		public SupplementalTypeEnum GetSupplementalTypeAsSupplementalTypeEnum()
		{
			return SupplementalTypeInfo.ValueToSupplementalType(_SupplementalType);
		}

		public void SetSupplementalTypeAsSupplementalTypeEnum(SupplementalTypeEnum enumItem)
		{
			SupplementalType = SupplementalTypeInfo.SupplementalTypeToValue(enumItem);
		}
	}

	public class SamplesSubField
	{
		private uint _ISample;

		private uint _QSample;

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

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

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			ISample = bitBuilder.RemoveBitsAsUint(8);
			QSample = bitBuilder.RemoveBitsAsUint(8);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(ISample, 8);
			bitBuilder.AddBitsAsUint(QSample, 8);
			return bitBuilder.ToString();
		}
	}

	private const uint _EventCode = 62u;

	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 LeAoaReportInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 62u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 3839u;
		UserFields.DataChannelIndex = 36u;
	}

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubeventCode)
		{
			return false;
		}
		UserFields.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.RxPhy = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Rssi = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SupplementalType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.DataChannelIndex = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SampleCount = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.SamplesList.Count; i++)
		{
			UserFields.SamplesList[i].FromBitBuilder(ref bitBuilder);
		}
		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.AddBitsAsUint(UserFields.SubeventCode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.RxPhy, 8);
		bitBuilder.AddBitsAsUint(UserFields.Rssi, 8);
		bitBuilder.AddBitsAsUint(UserFields.SupplementalType, 8);
		bitBuilder.AddBitsAsUint(UserFields.DataChannelIndex, 8);
		bitBuilder.AddBitsAsUint(UserFields.SampleCount, 8);
		for (int i = 0; i < UserFields.SamplesList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.SamplesList[i].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.ConnectionHandle < 3839)
		{
			list.Add("Minimum allowed value for Connection_Handle is 0xEFF");
		}
		if (UserFields.DataChannelIndex < 36)
		{
			list.Add("Minimum allowed value for Data_Channel_Index is 0x24");
		}
		return list;
	}
}
