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

namespace QCRPG.TSL.ZigBee.Packets;

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

		private uint _Pibattributeindex;

		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 PibattributeEnum GetPibattributeAsPibattributeEnum()
		{
			return PibattributeInfo.ValueToPibattribute(_Pibattribute);
		}

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

	private const uint _Packetcode = 35u;

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

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

	public uint Packetcode => 35u;

	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);
		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);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.Pibattribute > 255)
		{
			list.Add("Maximum allowed value for PIBAttribute is 0xFF");
		}
		return list;
	}
}
