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

namespace QCRPG.TSL.NFC.Packets;

public class RfDiscoverMapCmd : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _NumberOfMappingConfigurations;

		private List<MappingConfigurationSubField> _MappingConfigurationList = new List<MappingConfigurationSubField>();

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

		public List<MappingConfigurationSubField> MappingConfigurationList => _MappingConfigurationList;
	}

	public class MappingConfigurationSubField
	{
		public class ModeEnumCollection
		{
			private AssertBitEnum _TheRfInterfaceIsMappedToTheRfProtocolInPollMode;

			private AssertBitEnum _TheRfInterfaceIsMappedToTheRfProtocolInListenMode;

			public AssertBitEnum TheRfInterfaceIsMappedToTheRfProtocolInPollMode
			{
				get
				{
					return _TheRfInterfaceIsMappedToTheRfProtocolInPollMode;
				}
				set
				{
					_TheRfInterfaceIsMappedToTheRfProtocolInPollMode = value;
				}
			}

			public AssertBitEnum TheRfInterfaceIsMappedToTheRfProtocolInListenMode
			{
				get
				{
					return _TheRfInterfaceIsMappedToTheRfProtocolInListenMode;
				}
				set
				{
					_TheRfInterfaceIsMappedToTheRfProtocolInListenMode = value;
				}
			}
		}

		public static class ModeInfo
		{
			public static ModeEnumCollection ValueToMode(uint value)
			{
				ModeEnumCollection modeEnumCollection = new ModeEnumCollection();
				modeEnumCollection.TheRfInterfaceIsMappedToTheRfProtocolInPollMode = AssertBitInfo.ValueToAssertBit(value & 1u);
				modeEnumCollection.TheRfInterfaceIsMappedToTheRfProtocolInListenMode = AssertBitInfo.ValueToAssertBit((value & 2) >> 1);
				return modeEnumCollection;
			}

			public static uint ModeToValue(ModeEnumCollection enumCollection)
			{
				uint num = 0u;
				num |= AssertBitInfo.AssertBitToValue(enumCollection.TheRfInterfaceIsMappedToTheRfProtocolInPollMode);
				return num | (AssertBitInfo.AssertBitToValue(enumCollection.TheRfInterfaceIsMappedToTheRfProtocolInListenMode) << 1);
			}

			public static string ModeToString(ModeEnumCollection enumCollection)
			{
				string text = "";
				text = text + "The RF Interface is mapped to the RF Protocol in Poll Mode: " + AssertBitInfo.AssertBitToString(enumCollection.TheRfInterfaceIsMappedToTheRfProtocolInPollMode) + "\r\n";
				text = text + "The RF Interface is mapped to the RF Protocol in Listen Mode: " + AssertBitInfo.AssertBitToString(enumCollection.TheRfInterfaceIsMappedToTheRfProtocolInListenMode) + "\r\n";
				return text.TrimEnd('\r', '\n');
			}

			public static string ModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Bit 0: The RF Interface is mapped to the RF Protocol in Poll Mode");
				stringBuilder.AppendLine(AssertBitInfo.AssertBitToDescription());
				stringBuilder.AppendLine("Bit 1: The RF Interface is mapped to the RF Protocol in Listen Mode");
				stringBuilder.AppendLine(AssertBitInfo.AssertBitToDescription());
				stringBuilder.AppendLine("Bits 7-2: RFU");
				return stringBuilder.ToString();
			}
		}

		private uint _RfProtocol;

		private uint _Mode;

		private uint _RfInterface;

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

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

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

		public RfProtocolsEnum GetRfProtocolAsRfProtocolsEnum()
		{
			return RfProtocolsInfo.ValueToRfProtocols(_RfProtocol);
		}

		public void SetRfProtocolAsRfProtocolsEnum(RfProtocolsEnum enumItem)
		{
			RfProtocol = RfProtocolsInfo.RfProtocolsToValue(enumItem);
		}

		public ModeEnumCollection GetModeAsModeEnumCollection()
		{
			return ModeInfo.ValueToMode(_Mode);
		}

		public void SetModeAsModeEnumCollection(ModeEnumCollection enumCollection)
		{
			Mode = ModeInfo.ModeToValue(enumCollection);
		}

		public RfInterfacesEnum GetRfInterfaceAsRfInterfacesEnum()
		{
			return RfInterfacesInfo.ValueToRfInterfaces(_RfInterface);
		}

		public void SetRfInterfaceAsRfInterfacesEnum(RfInterfacesEnum enumItem)
		{
			RfInterface = RfInterfacesInfo.RfInterfacesToValue(enumItem);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			RfProtocol = bitBuilder.RemoveBitsAsUint(8);
			Mode = bitBuilder.RemoveBitsAsUint(8);
			RfInterface = bitBuilder.RemoveBitsAsUint(8);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(RfProtocol, 8);
			bitBuilder.AddBitsAsUint(Mode, 8);
			bitBuilder.AddBitsAsUint(RfInterface, 8);
			return bitBuilder.ToString();
		}
	}

	private const uint _Mt = 1u;

	private const uint _Gid = 1u;

	private const uint _Oid = 0u;

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

	private PacketInfoBase info;

	private uint _Pbf;

	private uint _Rfu;

	private uint _PayloadLength;

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

	public uint Mt => 1u;

	public uint Pbf => _Pbf;

	public uint Gid => 1u;

	public uint Rfu => _Rfu;

	public uint Oid => 0u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.NumberOfMappingConfigurations = 1u;
	}

	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.NumberOfMappingConfigurations = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.MappingConfigurationList.Count; i++)
		{
			UserFields.MappingConfigurationList[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.NumberOfMappingConfigurations, 8);
		for (int i = 0; i < UserFields.MappingConfigurationList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.MappingConfigurationList[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();
	}
}
