using System;
using System.Collections.Generic;
using QCRPG.Framework;

namespace QCRPG.TSL.ZigBee.Packets;

public class MlmeSetMacKeyTable : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _Pibattribute = 113u;

		private uint _Pibattributeindex;

		private uint _Pibattributelength;

		private uint _Idmode;

		private ulong _Source;

		private uint _Index;

		private uint _Frameusage;

		private List<uint> _KeyList = new List<uint>(new uint[16]);

		public uint Pibattribute => 113u;

		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 Idmode
		{
			get
			{
				return _Idmode;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Idmode is 0xFF");
				}
				_Idmode = value;
			}
		}

		public ulong Source
		{
			get
			{
				return _Source;
			}
			set
			{
				_Source = value;
			}
		}

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

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

		public List<uint> KeyList => _KeyList;
	}

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

	public uint Packetcode => 42u;

	public UserFieldsWrapper UserFields => _UserFields;

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

	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.Idmode = bitBuilder.RemoveBitsAsUintLittleEndian(8);
		UserFields.Source = bitBuilder.RemoveBitsAsUlongLittleEndian(64);
		UserFields.Index = bitBuilder.RemoveBitsAsUintLittleEndian(8);
		UserFields.Frameusage = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		for (int num = UserFields.KeyList.Count - 1; num >= 0; num--)
		{
			UserFields.KeyList[num] = 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.Idmode, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.Source, 64);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Index, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Frameusage, 16);
		for (int num = UserFields.KeyList.Count - 1; num >= 0; num--)
		{
			if (UserFields.KeyList[num] > 255)
			{
				throw new Exception("UserFields.KeyList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.KeyList[num], 8);
		}
		return bitBuilder.ToString();
	}
}
