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

namespace QCRPG.TSL.NFC.Packets;

public class RfGetListenModeRoutingNtf : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _More;

		private uint _NumberOfRoutingEntries;

		private List<RoutingEntrySubField> _RoutingEntryList = new List<RoutingEntrySubField>();

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

		public uint NumberOfRoutingEntries
		{
			get
			{
				return _NumberOfRoutingEntries;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for NumberOfRoutingEntries is 0xFF");
				}
				_NumberOfRoutingEntries = value;
				while (_RoutingEntryList.Count < _NumberOfRoutingEntries)
				{
					_RoutingEntryList.Add(new RoutingEntrySubField());
				}
				while (_RoutingEntryList.Count > _NumberOfRoutingEntries)
				{
					_RoutingEntryList.RemoveAt(_RoutingEntryList.Count - 1);
				}
			}
		}

		public List<RoutingEntrySubField> RoutingEntryList => _RoutingEntryList;

		public MoreFieldValuesEnum GetMoreAsMoreFieldValuesEnum()
		{
			return MoreFieldValuesInfo.ValueToMoreFieldValues(_More);
		}

		public void SetMoreAsMoreFieldValuesEnum(MoreFieldValuesEnum enumItem)
		{
			More = MoreFieldValuesInfo.MoreFieldValuesToValue(enumItem);
		}
	}

	public class RoutingEntrySubField
	{
		private uint _Type;

		private uint _Length;

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

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

		public uint Length
		{
			get
			{
				return _Length;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Length is 0xFF");
				}
				_Length = value;
				while (_ValueList.Count < _Length)
				{
					_ValueList.Add(0u);
				}
				while (_ValueList.Count > _Length)
				{
					_ValueList.RemoveAt(_ValueList.Count - 1);
				}
			}
		}

		public List<uint> ValueList => _ValueList;

		public TlvCodingForListenModeRoutingEnum GetTypeAsTlvCodingForListenModeRoutingEnum()
		{
			return TlvCodingForListenModeRoutingInfo.ValueToTlvCodingForListenModeRouting(_Type);
		}

		public void SetTypeAsTlvCodingForListenModeRoutingEnum(TlvCodingForListenModeRoutingEnum enumItem)
		{
			Type = TlvCodingForListenModeRoutingInfo.TlvCodingForListenModeRoutingToValue(enumItem);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			Type = bitBuilder.RemoveBitsAsUint(8);
			Length = bitBuilder.RemoveBitsAsUint(8);
			for (int num = ValueList.Count - 1; num >= 0; num--)
			{
				ValueList[num] = bitBuilder.RemoveBitsAsUint(8);
			}
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(Type, 8);
			bitBuilder.AddBitsAsUint(Length, 8);
			for (int num = ValueList.Count - 1; num >= 0; num--)
			{
				if (ValueList[num] > 255)
				{
					throw new Exception("ValueList[i] is > 0xFF");
				}
				bitBuilder.AddBitsAsUint(ValueList[num], 8);
			}
			return bitBuilder.ToString();
		}
	}

	private const uint _Mt = 3u;

	private const uint _Gid = 1u;

	private const uint _Oid = 2u;

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

	private PacketInfoBase info;

	private uint _Pbf;

	private uint _Rfu;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

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

	public uint Mt => 3u;

	public uint Pbf => _Pbf;

	public uint Gid => 1u;

	public uint Rfu => _Rfu;

	public uint Oid => 2u;

	public uint PayloadLength
	{
		get
		{
			ToBitString();
			return _PayloadLength;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(3) != Mt)
		{
			return false;
		}
		_Pbf = bitBuilder.RemoveBitsAsUint(1);
		if (bitBuilder.RemoveBitsAsUint(4) != Gid)
		{
			return false;
		}
		_Rfu = bitBuilder.RemoveBitsAsUint(2);
		if (bitBuilder.RemoveBitsAsUint(6) != Oid)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.More = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfRoutingEntries = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.RoutingEntryList.Count; i++)
		{
			UserFields.RoutingEntryList[i].FromBitBuilder(ref bitBuilder);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(Mt, 3);
		bitBuilder.AddBitsAsUint(Pbf, 1);
		bitBuilder.AddBitsAsUint(Gid, 4);
		bitBuilder.AddBitsAsUint(Rfu, 2);
		bitBuilder.AddBitsAsUint(Oid, 6);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.More, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfRoutingEntries, 8);
		for (int i = 0; i < UserFields.RoutingEntryList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.RoutingEntryList[i].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
