using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LeRpaOffloadRemoveIrkFromList : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _RpaOffloadOpcode = 3u;

		private uint _AddressType;

		private ulong _LeDeviceAddress;

		public uint RpaOffloadOpcode => 3u;

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

		public ulong LeDeviceAddress
		{
			get
			{
				return _LeDeviceAddress;
			}
			set
			{
				_LeDeviceAddress = value;
			}
		}

		public DeviceAddressTypeEnum GetAddressTypeAsDeviceAddressTypeEnum()
		{
			return DeviceAddressTypeInfo.ValueToDeviceAddressType(_AddressType);
		}

		public void SetAddressTypeAsDeviceAddressTypeEnum(DeviceAddressTypeEnum enumItem)
		{
			AddressType = DeviceAddressTypeInfo.DeviceAddressTypeToValue(enumItem);
		}
	}

	private const uint _Opcode = 64853u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForLeRpaOffloadRemoveIrkFromList),
		typeof(CommandCompleteForNop)
	};

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

	public uint Opcode => 64853u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.RpaOffloadOpcode)
		{
			return false;
		}
		UserFields.AddressType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.LeDeviceAddress = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		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.RpaOffloadOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.AddressType, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.LeDeviceAddress, 48);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
