using System;
using QCRPG.Framework;

namespace QCRPG.TSL.ZigBee.Packets;

public class MlmeInvalidCommandResponse : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _EventCode = 255u;

		private uint _Packetlength;

		private uint _Protocolid;

		private uint _Length;

		private uint _Status;

		public uint Packetlength
		{
			get
			{
				return _Packetlength;
			}
			set
			{
				_Packetlength = value;
			}
		}

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

		public uint EventCode => 255u;

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

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

		public ProtocolIdEnum GetProtocolidAsProtocolIdEnum()
		{
			return ProtocolIdInfo.ValueToProtocolId(_Protocolid);
		}

		public void SetProtocolidAsProtocolIdEnum(ProtocolIdEnum enumItem)
		{
			Protocolid = ProtocolIdInfo.ProtocolIdToValue(enumItem);
		}

		public CommandOpcodeEnum GetEventCodeAsCommandOpcodeEnum()
		{
			return CommandOpcodeInfo.ValueToCommandOpcode(255u);
		}

		public ErrorCodeEnum GetStatusAsErrorCodeEnum()
		{
			return ErrorCodeInfo.ValueToErrorCode(_Status);
		}

		public void SetStatusAsErrorCodeEnum(ErrorCodeEnum enumItem)
		{
			Status = ErrorCodeInfo.ErrorCodeToValue(enumItem);
		}
	}

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Response;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new MlmeInvalidCommandResponseInfo(this);
			}
			return info;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		UserFields.Packetlength = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.Protocolid = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.EventCode)
		{
			return false;
		}
		UserFields.Length = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Packetlength, 32);
		bitBuilder.AddBitsAsUint(UserFields.Protocolid, 8);
		bitBuilder.AddBitsAsUint(UserFields.EventCode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Length, 16);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		return bitBuilder.ToString();
	}
}
