using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class AmpReceiverReport : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _ControllerType;

		private uint _Reason;

		private uint _EventType;

		private uint _NumberOfFrames;

		private uint _NumberOfErrorFrames;

		private uint _NumberOfBits;

		private uint _NumberOfErrorBits;

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

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

		public uint EventType
		{
			get
			{
				return _EventType;
			}
			set
			{
				_EventType = value;
			}
		}

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

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

		public uint NumberOfBits
		{
			get
			{
				return _NumberOfBits;
			}
			set
			{
				_NumberOfBits = value;
			}
		}

		public uint NumberOfErrorBits
		{
			get
			{
				return _NumberOfErrorBits;
			}
			set
			{
				_NumberOfErrorBits = value;
			}
		}
	}

	private const uint _EventCode = 75u;

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

	public uint EventCode => 75u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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;
		}
		UserFields.ControllerType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Reason = bitBuilder.RemoveBitsAsUint(8);
		UserFields.EventType = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.NumberOfFrames = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.NumberOfErrorFrames = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.NumberOfBits = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.NumberOfErrorBits = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		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.ControllerType, 8);
		bitBuilder.AddBitsAsUint(UserFields.Reason, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.EventType, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.NumberOfFrames, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.NumberOfErrorFrames, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.NumberOfBits, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.NumberOfErrorBits, 32);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
