using System;
using QCRPG.Framework;

namespace QCRPG.TSL.ZigBee.Packets;

public class MlmeSetMacDeviceTable : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _Pibattribute = 115u;

		private uint _Pibattributeindex;

		private uint _Pibattributelength;

		private uint _Panid;

		private uint _Shortaddress;

		private ulong _Extaddress;

		private uint _Framecounter;

		private uint _Flags;

		private uint _Keytableindex;

		public uint Pibattribute => 115u;

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

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

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

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

		public ulong Extaddress
		{
			get
			{
				return _Extaddress;
			}
			set
			{
				_Extaddress = value;
			}
		}

		public uint Framecounter
		{
			get
			{
				return _Framecounter;
			}
			set
			{
				_Framecounter = value;
			}
		}

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

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

	private const uint _Packetcode = 42u;

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

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

	public uint Packetcode => 42u;

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Pibattributelength = 19u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(8) != Packetcode)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.Pibattribute)
		{
			return false;
		}
		UserFields.Pibattributeindex = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Pibattributelength = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Panid = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Shortaddress = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Extaddress = bitBuilder.RemoveBitsAsUlongLittleEndian(64);
		UserFields.Framecounter = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.Flags = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Keytableindex = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Packetcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Pibattribute, 8);
		bitBuilder.AddBitsAsUint(UserFields.Pibattributeindex, 8);
		bitBuilder.AddBitsAsUint(UserFields.Pibattributelength, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Panid, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Shortaddress, 16);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.Extaddress, 64);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Framecounter, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Flags, 16);
		bitBuilder.AddBitsAsUint(UserFields.Keytableindex, 8);
		return bitBuilder.ToString();
	}
}
