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

namespace QCRPG.TSL.ZigBee.Packets;

public class MlmeGetConfirmationBeaconPayload : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _EventCode = 35u;

		private uint _Packetlength;

		private uint _Protocolid;

		private uint _Length;

		private uint _Status;

		private uint _Pibattribute = 69u;

		private uint _Pibattributeindex;

		private uint _Pibattributelength;

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

		public uint Packetlength
		{
			get
			{
				return _Packetlength;
			}
			set
			{
				_Packetlength = value;
				if (_Packetlength < 7)
				{
					_Packetlength = 7u;
				}
				while (_PibattributevalueList.Count < _Packetlength - 7)
				{
					_PibattributevalueList.Add(0u);
				}
				while (_PibattributevalueList.Count > _Packetlength - 7)
				{
					_PibattributevalueList.RemoveAt(_PibattributevalueList.Count - 1);
				}
			}
		}

		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 => 35u;

		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 uint Pibattribute
		{
			get
			{
				return _Pibattribute;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Pibattribute is 0xFF");
				}
				if (PibattributebeaconpayloadInfo.ValueToPibattributebeaconpayload(value) == PibattributebeaconpayloadEnum.InvalidPibattributebeaconpayloadValue)
				{
					throw new Exception("Invalid Pibattribute value");
				}
				_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 ProtocolIdEnum GetProtocolidAsProtocolIdEnum()
		{
			return ProtocolIdInfo.ValueToProtocolId(_Protocolid);
		}

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

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

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

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

		public PibattributebeaconpayloadEnum GetPibattributeAsPibattributebeaconpayloadEnum()
		{
			return PibattributebeaconpayloadInfo.ValueToPibattributebeaconpayload(_Pibattribute);
		}

		public void SetPibattributeAsPibattributebeaconpayloadEnum(PibattributebeaconpayloadEnum enumItem)
		{
			Pibattribute = PibattributebeaconpayloadInfo.PibattributebeaconpayloadToValue(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 MlmeGetConfirmationBeaconPayloadInfo(this);
			}
			return info;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Pibattribute = 69u;
	}

	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);
		UserFields.Pibattribute = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Pibattributeindex = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Pibattributelength = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.PibattributevalueList.Count; i++)
		{
			UserFields.PibattributevalueList[i] = 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);
		bitBuilder.AddBitsAsUint(UserFields.Pibattribute, 8);
		bitBuilder.AddBitsAsUint(UserFields.Pibattributeindex, 8);
		bitBuilder.AddBitsAsUint(UserFields.Pibattributelength, 8);
		for (int i = 0; i < UserFields.PibattributevalueList.Count; i++)
		{
			if (UserFields.PibattributevalueList[i] > 255)
			{
				throw new Exception("UserFields.PibattributevalueList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.PibattributevalueList[i], 8);
		}
		return bitBuilder.ToString();
	}

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