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

namespace QCRPG.TSL.BT.Packets;

public class LeSetConnectionlessSupplementalTransmitParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SupplementalLengthEnum
		{
			No_Supplemental,
			RESERVED,
			Supplemental_Length_In_8_us_Units,
			Reserved_For_Future_Use,
			InvalidSupplementalLengthValue
		}

		public static class SupplementalLengthInfo
		{
			public static SupplementalLengthEnum ValueToSupplementalLength(uint value)
			{
				switch (value)
				{
				case 0u:
					return SupplementalLengthEnum.No_Supplemental;
				case 1u:
					return SupplementalLengthEnum.RESERVED;
				default:
					if (value >= 2 && value <= 20)
					{
						return SupplementalLengthEnum.Supplemental_Length_In_8_us_Units;
					}
					if (value >= 21 && value <= 255)
					{
						return SupplementalLengthEnum.Reserved_For_Future_Use;
					}
					return SupplementalLengthEnum.InvalidSupplementalLengthValue;
				}
			}

			public static uint SupplementalLengthToValue(SupplementalLengthEnum enumItem)
			{
				return enumItem switch
				{
					SupplementalLengthEnum.No_Supplemental => 0u, 
					SupplementalLengthEnum.RESERVED => 1u, 
					SupplementalLengthEnum.Supplemental_Length_In_8_us_Units => 2u, 
					SupplementalLengthEnum.Reserved_For_Future_Use => 21u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SupplementalLengthToString(SupplementalLengthEnum enumItem)
			{
				return enumItem switch
				{
					SupplementalLengthEnum.No_Supplemental => "No_Supplemental", 
					SupplementalLengthEnum.RESERVED => "RESERVED", 
					SupplementalLengthEnum.Supplemental_Length_In_8_us_Units => "Supplemental_Length_In_8_us_Units", 
					SupplementalLengthEnum.Reserved_For_Future_Use => "Reserved for Future Use", 
					_ => "Invalid SupplementalLength Value", 
				};
			}

			public static string SupplementalLengthToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No_Supplemental");
				stringBuilder.AppendLine("0x1 = RESERVED");
				stringBuilder.AppendLine("0x2-0x14 = Supplemental_Length_In_8_us_Units");
				stringBuilder.AppendLine("0x15-0xFF = Reserved for Future Use");
				return stringBuilder.ToString();
			}
		}

		private uint _AdvertisingHandle;

		private uint _SupplementalLength;

		private uint _SupplementalType;

		private uint _CteCount;

		private uint _NumberOfAntennaIds = 1u;

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

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

		public uint SupplementalLength
		{
			get
			{
				return _SupplementalLength;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for SupplementalLength is 0xFF");
				}
				_SupplementalLength = 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 CteCount
		{
			get
			{
				return _CteCount;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for CteCount is 0xFF");
				}
				_CteCount = 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 SupplementalLengthEnum GetSupplementalLengthAsSupplementalLengthEnum()
		{
			return SupplementalLengthInfo.ValueToSupplementalLength(_SupplementalLength);
		}

		public void SetSupplementalLengthAsSupplementalLengthEnum(SupplementalLengthEnum enumItem)
		{
			SupplementalLength = SupplementalLengthInfo.SupplementalLengthToValue(enumItem);
		}

		public AodSupplementalTypeEnum GetSupplementalTypeAsAodSupplementalTypeEnum()
		{
			return AodSupplementalTypeInfo.ValueToAodSupplementalType(_SupplementalType);
		}

		public void SetSupplementalTypeAsAodSupplementalTypeEnum(AodSupplementalTypeEnum enumItem)
		{
			SupplementalType = AodSupplementalTypeInfo.AodSupplementalTypeToValue(enumItem);
		}
	}

	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 = 8273u;

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

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

	public uint Opcode => 8273u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.AdvertisingHandle = 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.AdvertisingHandle = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SupplementalLength = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SupplementalType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.CteCount = 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.AddBitsAsUint(UserFields.AdvertisingHandle, 8);
		bitBuilder.AddBitsAsUint(UserFields.SupplementalLength, 8);
		bitBuilder.AddBitsAsUint(UserFields.SupplementalType, 8);
		bitBuilder.AddBitsAsUint(UserFields.CteCount, 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.AdvertisingHandle < 0)
		{
			list.Add("Minimum allowed value for Advertising_Handle is 0x0");
		}
		if (UserFields.AdvertisingHandle > 239)
		{
			list.Add("Maximum allowed value for Advertising_Handle is 0xEF");
		}
		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;
	}
}
