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

namespace QCRPG.TSL.BT.Packets;

public class LeSetConnectionSupplementalTransmitParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		public class CteTypesEnumCollection
		{
			private EnableBitEnum _EnableAoaSupplementalResponse;

			private EnableBitEnum _EnableAodSupplementalWith1UsSlotsResponse;

			private EnableBitEnum _EnableAodSupplementalWith2UsSlotsResponse;

			private EnableBitEnum _ReservedForFutureUse;

			public EnableBitEnum EnableAoaSupplementalResponse
			{
				get
				{
					return _EnableAoaSupplementalResponse;
				}
				set
				{
					_EnableAoaSupplementalResponse = value;
				}
			}

			public EnableBitEnum EnableAodSupplementalWith1UsSlotsResponse
			{
				get
				{
					return _EnableAodSupplementalWith1UsSlotsResponse;
				}
				set
				{
					_EnableAodSupplementalWith1UsSlotsResponse = value;
				}
			}

			public EnableBitEnum EnableAodSupplementalWith2UsSlotsResponse
			{
				get
				{
					return _EnableAodSupplementalWith2UsSlotsResponse;
				}
				set
				{
					_EnableAodSupplementalWith2UsSlotsResponse = value;
				}
			}

			public EnableBitEnum ReservedForFutureUse
			{
				get
				{
					return _ReservedForFutureUse;
				}
				set
				{
					_ReservedForFutureUse = value;
				}
			}
		}

		public static class CteTypesInfo
		{
			public static CteTypesEnumCollection ValueToCteTypes(uint value)
			{
				CteTypesEnumCollection cteTypesEnumCollection = new CteTypesEnumCollection();
				cteTypesEnumCollection.EnableAoaSupplementalResponse = EnableBitInfo.ValueToEnableBit(value & 1u);
				cteTypesEnumCollection.EnableAodSupplementalWith1UsSlotsResponse = EnableBitInfo.ValueToEnableBit((value & 2) >> 1);
				cteTypesEnumCollection.EnableAodSupplementalWith2UsSlotsResponse = EnableBitInfo.ValueToEnableBit((value & 4) >> 2);
				cteTypesEnumCollection.ReservedForFutureUse = EnableBitInfo.ValueToEnableBit((value & 0xF8) >> 3);
				return cteTypesEnumCollection;
			}

			public static uint CteTypesToValue(CteTypesEnumCollection enumCollection)
			{
				uint num = 0u;
				num |= EnableBitInfo.EnableBitToValue(enumCollection.EnableAoaSupplementalResponse);
				num |= EnableBitInfo.EnableBitToValue(enumCollection.EnableAodSupplementalWith1UsSlotsResponse) << 1;
				num |= EnableBitInfo.EnableBitToValue(enumCollection.EnableAodSupplementalWith2UsSlotsResponse) << 2;
				return num | (EnableBitInfo.EnableBitToValue(enumCollection.ReservedForFutureUse) << 3);
			}

			public static string CteTypesToString(CteTypesEnumCollection enumCollection)
			{
				string text = "";
				text = text + "Enable AoA Supplemental Response: " + EnableBitInfo.EnableBitToString(enumCollection.EnableAoaSupplementalResponse) + "\r\n";
				text = text + "Enable AoD Supplemental with 1 us slots Response: " + EnableBitInfo.EnableBitToString(enumCollection.EnableAodSupplementalWith1UsSlotsResponse) + "\r\n";
				text = text + "Enable AoD Supplemental with 2 us slots Response: " + EnableBitInfo.EnableBitToString(enumCollection.EnableAodSupplementalWith2UsSlotsResponse) + "\r\n";
				text = text + "RESERVED_FOR_FUTURE_USE: " + EnableBitInfo.EnableBitToString(enumCollection.ReservedForFutureUse) + "\r\n";
				return text.TrimEnd('\r', '\n');
			}

			public static string CteTypesToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Bit 0: Enable AoA Supplemental Response");
				stringBuilder.AppendLine("Bit 1: Enable AoD Supplemental with 1 us slots Response");
				stringBuilder.AppendLine("Bit 2: Enable AoD Supplemental with 2 us slots Response");
				stringBuilder.AppendLine("Bits 7-3: RESERVED_FOR_FUTURE_USE");
				return stringBuilder.ToString();
			}
		}

		private uint _ConnectionHandle;

		private uint _CteTypes;

		private uint _NumberOfAntennaIds = 1u;

		private List<AntennaIdsSetSubField> _AntennaIdsSetList = new List<AntennaIdsSetSubField>();

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

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

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

		public List<AntennaIdsSetSubField> AntennaIdsSetList => _AntennaIdsSetList;

		public CteTypesEnumCollection GetCteTypesAsCteTypesEnumCollection()
		{
			return CteTypesInfo.ValueToCteTypes(_CteTypes);
		}

		public void SetCteTypesAsCteTypesEnumCollection(CteTypesEnumCollection enumCollection)
		{
			CteTypes = CteTypesInfo.CteTypesToValue(enumCollection);
		}
	}

	public class AntennaIdsSetSubField
	{
		private uint _AntennaIds;

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

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

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

	private const uint _Opcode = 8277u;

	private Type[] expectedResponseTypes = new Type[1] { typeof(CommandCompleteForLeSetConnectionSupplementalTransmitParameters) };

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Command;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

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

	public uint Opcode => 8277u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 0u;
		UserFields.NumberOfAntennaIds = 1u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != Opcode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.CteTypes = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfAntennaIds = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.AntennaIdsSetList.Count; i++)
		{
			UserFields.AntennaIdsSetList[i].FromBitBuilder(ref bitBuilder);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Opcode, 16);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.CteTypes, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfAntennaIds, 8);
		for (int i = 0; i < UserFields.AntennaIdsSetList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.AntennaIdsSetList[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 < 0)
		{
			list.Add("Minimum allowed value for Connection_Handle is 0x0");
		}
		if (UserFields.ConnectionHandle > 3839)
		{
			list.Add("Maximum allowed value for Connection_Handle is 0xEFF");
		}
		if (UserFields.NumberOfAntennaIds < 1)
		{
			list.Add("Minimum allowed value for Number_Of_Antenna_IDs is 0x1");
		}
		if (UserFields.NumberOfAntennaIds > 75)
		{
			list.Add("Maximum allowed value for Number_Of_Antenna_IDs is 0x4B");
		}
		return list;
	}
}
