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

namespace QCRPG.TSL.BT.Packets;

public class LeConnectionlessIQReport : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PacketStatusEnum
		{
			CRC_Correct,
			CRCIncorrect_Length,
			CRCIncorrect_Controller,
			Reserved,
			InvalidPacketStatusValue
		}

		public static class PacketStatusInfo
		{
			public static PacketStatusEnum ValueToPacketStatus(uint value)
			{
				switch (value)
				{
				case 0u:
					return PacketStatusEnum.CRC_Correct;
				case 1u:
					return PacketStatusEnum.CRCIncorrect_Length;
				case 2u:
					return PacketStatusEnum.CRCIncorrect_Controller;
				default:
					if (value >= 3 && value <= 255)
					{
						return PacketStatusEnum.Reserved;
					}
					return PacketStatusEnum.InvalidPacketStatusValue;
				}
			}

			public static uint PacketStatusToValue(PacketStatusEnum enumItem)
			{
				return enumItem switch
				{
					PacketStatusEnum.CRC_Correct => 0u, 
					PacketStatusEnum.CRCIncorrect_Length => 1u, 
					PacketStatusEnum.CRCIncorrect_Controller => 2u, 
					PacketStatusEnum.Reserved => 3u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PacketStatusToString(PacketStatusEnum enumItem)
			{
				return enumItem switch
				{
					PacketStatusEnum.CRC_Correct => "CRC was correct", 
					PacketStatusEnum.CRCIncorrect_Length => "CRC incorrect. Length and SupplementalTime used to determine sampling points.", 
					PacketStatusEnum.CRCIncorrect_Controller => "CRC incorrect. Controller has determined the position and length of Supplemental in some other way.", 
					PacketStatusEnum.Reserved => "Reserved for future use.", 
					_ => "Invalid PacketStatus Value", 
				};
			}

			public static string PacketStatusToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = CRC was correct");
				stringBuilder.AppendLine("0x1 = CRC incorrect. Length and SupplementalTime used to determine sampling points.");
				stringBuilder.AppendLine("0x2 = CRC incorrect. Controller has determined the position and length of Supplemental in some other way.");
				stringBuilder.AppendLine("0x3-0xFF = Reserved for future use.");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubeventCode = 21u;

		private uint _SyncHandle;

		private uint _ChannelIndex;

		private uint _Rssi;

		private uint _RssiAntennaId;

		private uint _CteType;

		private uint _SlotDurations;

		private uint _PacketStatus;

		private uint _Paeventcounter;

		private uint _SampleCount = 9u;

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

		public uint SubeventCode => 21u;

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

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

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

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

		public uint CteType
		{
			get
			{
				return _CteType;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for CteType is 0xFF");
				}
				_CteType = 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 PacketStatus
		{
			get
			{
				return _PacketStatus;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for PacketStatus is 0xFF");
				}
				_PacketStatus = value;
			}
		}

		public uint Paeventcounter
		{
			get
			{
				return _Paeventcounter;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for Paeventcounter is 0xFFFF");
				}
				_Paeventcounter = 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 AodSupplementalTypeEnum GetCteTypeAsAodSupplementalTypeEnum()
		{
			return AodSupplementalTypeInfo.ValueToAodSupplementalType(_CteType);
		}

		public void SetCteTypeAsAodSupplementalTypeEnum(AodSupplementalTypeEnum enumItem)
		{
			CteType = AodSupplementalTypeInfo.AodSupplementalTypeToValue(enumItem);
		}

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

		public void SetSlotDurationsAsSlotDurationsEnum(SlotDurationsEnum enumItem)
		{
			SlotDurations = SlotDurationsInfo.SlotDurationsToValue(enumItem);
		}

		public PacketStatusEnum GetPacketStatusAsPacketStatusEnum()
		{
			return PacketStatusInfo.ValueToPacketStatus(_PacketStatus);
		}

		public void SetPacketStatusAsPacketStatusEnum(PacketStatusEnum enumItem)
		{
			PacketStatus = PacketStatusInfo.PacketStatusToValue(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 LeConnectionlessIQReportInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 62u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.SyncHandle = 0u;
		UserFields.ChannelIndex = 0u;
		UserFields.SampleCount = 9u;
	}

	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.SyncHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.ChannelIndex = bitBuilder.RemoveBitsAsUintLittleEndian(8);
		UserFields.Rssi = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.RssiAntennaId = bitBuilder.RemoveBitsAsUint(8);
		UserFields.CteType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.SlotDurations = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PacketStatus = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Paeventcounter = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		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.SyncHandle, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ChannelIndex, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Rssi, 16);
		bitBuilder.AddBitsAsUint(UserFields.RssiAntennaId, 8);
		bitBuilder.AddBitsAsUint(UserFields.CteType, 8);
		bitBuilder.AddBitsAsUint(UserFields.SlotDurations, 8);
		bitBuilder.AddBitsAsUint(UserFields.PacketStatus, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Paeventcounter, 16);
		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.SyncHandle < 0)
		{
			list.Add("Minimum allowed value for Sync_Handle is 0x0");
		}
		if (UserFields.SyncHandle > 3839)
		{
			list.Add("Maximum allowed value for Sync_Handle is 0xEFF");
		}
		if (UserFields.ChannelIndex < 0)
		{
			list.Add("Minimum allowed value for Channel_Index is 0x0");
		}
		if (UserFields.ChannelIndex > 36)
		{
			list.Add("Maximum allowed value for Channel_Index is 0x24");
		}
		if (UserFields.SampleCount < 9)
		{
			list.Add("Minimum allowed value for Sample_Count is 0x9");
		}
		if (UserFields.SampleCount > 82)
		{
			list.Add("Maximum allowed value for Sample_Count is 0x52");
		}
		return list;
	}
}
