using System;
using QCRPG.Framework;

namespace QCRPG.TSL.ZigBee.Packets;

public class MlmeRxEnable : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _Deferpermit;

		private uint _Rxontime;

		private uint _Rxonduration;

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

		public uint Rxontime
		{
			get
			{
				return _Rxontime;
			}
			set
			{
				_Rxontime = value;
			}
		}

		public uint Rxonduration
		{
			get
			{
				return _Rxonduration;
			}
			set
			{
				_Rxonduration = value;
			}
		}
	}

	private const uint _Packetcode = 39u;

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

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

	public uint Packetcode => 39u;

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(8) != Packetcode)
		{
			return false;
		}
		UserFields.Deferpermit = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Rxontime = bitBuilder.RemoveBitsAsUint(32);
		UserFields.Rxonduration = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Packetcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Deferpermit, 8);
		bitBuilder.AddBitsAsUint(UserFields.Rxontime, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Rxonduration, 32);
		return bitBuilder.ToString();
	}
}
