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

namespace QCRPG.TSL.ZigBee.Packets;

public class MlmeSet : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _Pibattribute;

		private uint _Pibattributeindex;

		private uint _Pibattributelength;

		private List<uint> _PibattributevalueList = new List<uint>();

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

		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;
				while (_PibattributevalueList.Count < _Pibattributelength)
				{
					_PibattributevalueList.Add(0u);
				}
				while (_PibattributevalueList.Count > _Pibattributelength)
				{
					_PibattributevalueList.RemoveAt(_PibattributevalueList.Count - 1);
				}
			}
		}

		public List<uint> PibattributevalueList => _PibattributevalueList;

		public PibattributeEnum GetPibattributeAsPibattributeEnum()
		{
			return PibattributeInfo.ValueToPibattribute(_Pibattribute);
		}

		public void SetPibattributeAsPibattributeEnum(PibattributeEnum enumItem)
		{
			Pibattribute = PibattributeInfo.PibattributeToValue(enumItem);
		}
	}

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

	public uint Packetcode => 42u;

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(8) != Packetcode)
		{
			return false;
		}
		UserFields.Pibattribute = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Pibattributeindex = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Pibattributelength = bitBuilder.RemoveBitsAsUintLittleEndian(8);
		for (int num = UserFields.PibattributevalueList.Count - 1; num >= 0; num--)
		{
			UserFields.PibattributevalueList[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.AddBitsAsUintLittleEndian(UserFields.Pibattributelength, 8);
		for (int num = UserFields.PibattributevalueList.Count - 1; num >= 0; num--)
		{
			if (UserFields.PibattributevalueList[num] > 255)
			{
				throw new Exception("UserFields.PibattributevalueList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.PibattributevalueList[num], 8);
		}
		return bitBuilder.ToString();
	}
}
