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

namespace QCRPG.TSL.BT.Packets;

public class CsrRxPktStatsEvent : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _Reserved = 195u;

		private const uint _Type = 2u;

		private const uint _Length = 19u;

		private const uint _Varid = 4097u;

		private uint _Seqno;

		private uint _Status;

		private uint _PktsReceived;

		private uint _GoodPktsReceived;

		private uint _CorrectedPkts;

		private uint _RssiValid;

		private List<uint> _RsvdList = new List<uint>(new uint[20]);

		public uint Reserved => 195u;

		public uint Type => 2u;

		public uint Length => 19u;

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

		public uint Varid => 4097u;

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

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

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

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

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

		public List<uint> RsvdList => _RsvdList;
	}

	private const uint _EventCode = 255u;

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

	public uint EventCode => 255u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Seqno = 1u;
		UserFields.Status = 0u;
	}

	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.Reserved)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.Type)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.Length)
		{
			return false;
		}
		UserFields.Seqno = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.Varid)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(16);
		UserFields.PktsReceived = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.GoodPktsReceived = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.CorrectedPkts = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.RssiValid = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		for (int i = 0; i < UserFields.RsvdList.Count; i++)
		{
			UserFields.RsvdList[i] = bitBuilder.RemoveBitsAsUint(8);
		}
		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.Reserved, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Type, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Length, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Seqno, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Varid, 16);
		bitBuilder.AddBitsAsUint(UserFields.Status, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.PktsReceived, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.GoodPktsReceived, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.CorrectedPkts, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.RssiValid, 16);
		for (int i = 0; i < UserFields.RsvdList.Count; i++)
		{
			if (UserFields.RsvdList[i] > 255)
			{
				throw new Exception("UserFields.RsvdList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.RsvdList[i], 8);
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
