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

namespace QCRPG.TSL.BT.Packets;

public class LeSetConnectionSupplementalReceiveParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SamplingEnableEnum
		{
			IQ_Disabled,
			IQ_Enabled,
			RFU,
			InvalidSamplingEnableValue
		}

		public static class SamplingEnableInfo
		{
			public static SamplingEnableEnum ValueToSamplingEnable(uint value)
			{
				switch (value)
				{
				case 0u:
					return SamplingEnableEnum.IQ_Disabled;
				case 1u:
					return SamplingEnableEnum.IQ_Enabled;
				default:
					if (value >= 2 && value <= 255)
					{
						return SamplingEnableEnum.RFU;
					}
					return SamplingEnableEnum.InvalidSamplingEnableValue;
				}
			}

			public static uint SamplingEnableToValue(SamplingEnableEnum enumItem)
			{
				return enumItem switch
				{
					SamplingEnableEnum.IQ_Disabled => 0u, 
					SamplingEnableEnum.IQ_Enabled => 1u, 
					SamplingEnableEnum.RFU => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SamplingEnableToString(SamplingEnableEnum enumItem)
			{
				return enumItem switch
				{
					SamplingEnableEnum.IQ_Disabled => "Connection IQ sampling is disabled (default)", 
					SamplingEnableEnum.IQ_Enabled => "Connection IQ sampling is enabled", 
					SamplingEnableEnum.RFU => "Reserved", 
					_ => "Invalid SamplingEnable Value", 
				};
			}

			public static string SamplingEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Connection IQ sampling is disabled (default)");
				stringBuilder.AppendLine("0x1 = Connection IQ sampling is enabled");
				stringBuilder.AppendLine("0x2-0xFF = Reserved");
				return stringBuilder.ToString();
			}
		}

		private uint _ConnectionHandle = 3839u;

		private uint _SamplingEnable;

		private uint _SlotDurations;

		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 SamplingEnable
		{
			get
			{
				return _SamplingEnable;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for SamplingEnable is 0xFF");
				}
				_SamplingEnable = value;
			}
		}

		public uint SlotDurations
		{
			get
			{
				return _SlotDurations;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for SlotDurations is 0xFF");
				}
				_SlotDurations = 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 SamplingEnableEnum GetSamplingEnableAsSamplingEnableEnum()
		{
			return SamplingEnableInfo.ValueToSamplingEnable(_SamplingEnable);
		}

		public void SetSamplingEnableAsSamplingEnableEnum(SamplingEnableEnum enumItem)
		{
			SamplingEnable = SamplingEnableInfo.SamplingEnableToValue(enumItem);
		}

		public SlotDurationsEnum GetSlotDurationsAsSlotDurationsEnum()
		{
			return SlotDurationsInfo.ValueToSlotDurations(_SlotDurations);
		}

		public void SetSlotDurationsAsSlotDurationsEnum(SlotDurationsEnum enumItem)
		{
			SlotDurations = SlotDurationsInfo.SlotDurationsToValue(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 = 8276u;

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

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

	public uint Opcode => 8276u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 3839u;
		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.SamplingEnable = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SlotDurations = 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.SamplingEnable, 8);
		bitBuilder.AddBitsAsUint(UserFields.SlotDurations, 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 < 3839)
		{
			list.Add("Minimum 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;
	}
}
