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

namespace QCRPG.TSL.NFC.Packets;

public class CoreConnCreateCmd : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum DestinationTypeEnum
		{
			RFU,
			NFCC_LOOPBACK,
			REMOTE_NFC_ENDPOINT,
			NFCEE,
			PROPRIETARY,
			InvalidDestinationTypeValue
		}

		public static class DestinationTypeInfo
		{
			public static DestinationTypeEnum ValueToDestinationType(uint value)
			{
				switch (value)
				{
				case 0u:
					return DestinationTypeEnum.RFU;
				case 1u:
					return DestinationTypeEnum.NFCC_LOOPBACK;
				case 2u:
					return DestinationTypeEnum.REMOTE_NFC_ENDPOINT;
				case 3u:
					return DestinationTypeEnum.NFCEE;
				default:
					if (value >= 4 && value <= 192)
					{
						return DestinationTypeEnum.RFU;
					}
					if (value >= 193 && value <= 254)
					{
						return DestinationTypeEnum.PROPRIETARY;
					}
					if (value == 255)
					{
						return DestinationTypeEnum.RFU;
					}
					return DestinationTypeEnum.InvalidDestinationTypeValue;
				}
			}

			public static uint DestinationTypeToValue(DestinationTypeEnum enumItem)
			{
				switch (enumItem)
				{
				case DestinationTypeEnum.RFU:
					return 0u;
				case DestinationTypeEnum.NFCC_LOOPBACK:
					return 1u;
				case DestinationTypeEnum.REMOTE_NFC_ENDPOINT:
					return 2u;
				case DestinationTypeEnum.NFCEE:
					return 3u;
				default:
					switch (enumItem)
					{
					case DestinationTypeEnum.RFU:
						return 4u;
					case DestinationTypeEnum.PROPRIETARY:
						return 193u;
					default:
						if (enumItem == DestinationTypeEnum.RFU)
						{
							return 255u;
						}
						return uint.MaxValue;
					}
				}
			}

			public static string DestinationTypeToString(DestinationTypeEnum enumItem)
			{
				switch (enumItem)
				{
				case DestinationTypeEnum.RFU:
					return "RFU";
				case DestinationTypeEnum.NFCC_LOOPBACK:
					return "This is used to create a Logical Connection between the DH and the NFCC for loop-back mode.";
				case DestinationTypeEnum.REMOTE_NFC_ENDPOINT:
					return "This is used to create a Dynamic Logical Connection for communicating with a Remote NFC Endpoint.";
				case DestinationTypeEnum.NFCEE:
					return "This is used to create a Dynamic Logical Connection for communicating with an NFCEE.";
				default:
					switch (enumItem)
					{
					case DestinationTypeEnum.RFU:
						return "RFU";
					case DestinationTypeEnum.PROPRIETARY:
						return "This is for proprietary use; e.g., to create a Logical Connection between the DH and the NFCC for firmware update purposes.";
					default:
						if (enumItem == DestinationTypeEnum.RFU)
						{
							return "RFU";
						}
						return "Invalid DestinationType Value";
					}
				}
			}

			public static string DestinationTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = RFU");
				stringBuilder.AppendLine("0x1 = This is used to create a Logical Connection between the DH and the NFCC for loop-back mode.");
				stringBuilder.AppendLine("0x2 = This is used to create a Dynamic Logical Connection for communicating with a Remote NFC Endpoint.");
				stringBuilder.AppendLine("0x3 = This is used to create a Dynamic Logical Connection for communicating with an NFCEE.");
				stringBuilder.AppendLine("0x4-0xC0 = RFU");
				stringBuilder.AppendLine("0xC1-0xFE = This is for proprietary use; e.g., to create a Logical Connection between the DH and the NFCC for firmware update purposes.");
				stringBuilder.AppendLine("0xFF = RFU");
				return stringBuilder.ToString();
			}
		}

		private uint _DestinationType;

		private uint _NumberOfDestinationSpecificParameters;

		private List<DestinationSpecificParameterSubField> _DestinationSpecificParameterList = new List<DestinationSpecificParameterSubField>();

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

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

		public List<DestinationSpecificParameterSubField> DestinationSpecificParameterList => _DestinationSpecificParameterList;

		public DestinationTypeEnum GetDestinationTypeAsDestinationTypeEnum()
		{
			return DestinationTypeInfo.ValueToDestinationType(_DestinationType);
		}

		public void SetDestinationTypeAsDestinationTypeEnum(DestinationTypeEnum enumItem)
		{
			DestinationType = DestinationTypeInfo.DestinationTypeToValue(enumItem);
		}
	}

	public class DestinationSpecificParameterSubField
	{
		public enum TypeEnum
		{
			RF_DISC_ID_AND_PROTOCOL,
			NFCEE_ID_AND_PROTOCOL,
			RFU,
			PROPRIETARY,
			InvalidTypeValue
		}

		public static class TypeInfo
		{
			public static TypeEnum ValueToType(uint value)
			{
				switch (value)
				{
				case 0u:
					return TypeEnum.RF_DISC_ID_AND_PROTOCOL;
				case 1u:
					return TypeEnum.NFCEE_ID_AND_PROTOCOL;
				default:
					if (value >= 2 && value <= 159)
					{
						return TypeEnum.RFU;
					}
					if (value >= 160 && value <= 255)
					{
						return TypeEnum.PROPRIETARY;
					}
					return TypeEnum.InvalidTypeValue;
				}
			}

			public static uint TypeToValue(TypeEnum enumItem)
			{
				return enumItem switch
				{
					TypeEnum.RF_DISC_ID_AND_PROTOCOL => 0u, 
					TypeEnum.NFCEE_ID_AND_PROTOCOL => 1u, 
					TypeEnum.RFU => 2u, 
					TypeEnum.PROPRIETARY => 160u, 
					_ => uint.MaxValue, 
				};
			}

			public static string TypeToString(TypeEnum enumItem)
			{
				return enumItem switch
				{
					TypeEnum.RF_DISC_ID_AND_PROTOCOL => "First Octet: An RF Discovery ID.  Second Octet: RF Protocol.", 
					TypeEnum.NFCEE_ID_AND_PROTOCOL => "First Octet: NFCEE ID (00 invalid).  Second Octet: NFCEE Interface Protocol.", 
					TypeEnum.RFU => "RFU", 
					TypeEnum.PROPRIETARY => "PROPRIETARY", 
					_ => "Invalid Type Value", 
				};
			}

			public static string TypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = First Octet: An RF Discovery ID.  Second Octet: RF Protocol.");
				stringBuilder.AppendLine("0x1 = First Octet: NFCEE ID (00 invalid).  Second Octet: NFCEE Interface Protocol.");
				stringBuilder.AppendLine("0x2-0x9F = RFU");
				stringBuilder.AppendLine("0xA0-0xFF = PROPRIETARY");
				return stringBuilder.ToString();
			}
		}

		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 TypeEnum GetTypeAsTypeEnum()
		{
			return TypeInfo.ValueToType(_Type);
		}

		public void SetTypeAsTypeEnum(TypeEnum enumItem)
		{
			Type = TypeInfo.TypeToValue(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 = 1u;

	private const uint _Gid = 0u;

	private const uint _Oid = 4u;

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

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

	public uint Mt => 1u;

	public uint Pbf => _Pbf;

	public uint Gid => 0u;

	public uint Rfu => _Rfu;

	public uint Oid => 4u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.DestinationType = 3u;
		UserFields.NumberOfDestinationSpecificParameters = 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.DestinationType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfDestinationSpecificParameters = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.DestinationSpecificParameterList.Count; i++)
		{
			UserFields.DestinationSpecificParameterList[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.DestinationType, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfDestinationSpecificParameters, 8);
		for (int i = 0; i < UserFields.DestinationSpecificParameterList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.DestinationSpecificParameterList[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();
	}
}
