using System;
using QCRPG.Framework;

namespace QCRPG.TSL.ZigBee.Packets;

public class VsPeekRequest : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _Elementsize;

		private uint _Numelements;

		private uint _Address;

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

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

		public uint Address
		{
			get
			{
				return _Address;
			}
			set
			{
				_Address = value;
			}
		}
	}

	private const uint _Packetcode = 134u;

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

	private PacketInfoBase info;

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

	public uint Packetcode => 134u;

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(8) != Packetcode)
		{
			return false;
		}
		UserFields.Elementsize = bitBuilder.RemoveBitsAsUintLittleEndian(8);
		UserFields.Numelements = bitBuilder.RemoveBitsAsUintLittleEndian(8);
		UserFields.Address = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Packetcode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Elementsize, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Numelements, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Address, 32);
		return bitBuilder.ToString();
	}
}
