using System;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class EdlBoardIdResponse : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum VendorEventClassEnum
		{
			HCI_DOWNLOAD_EVENT_CODE,
			InvalidVendorEventClassValue
		}

		public static class VendorEventClassInfo
		{
			public static VendorEventClassEnum ValueToVendorEventClass(uint value)
			{
				if (value == 0)
				{
					return VendorEventClassEnum.HCI_DOWNLOAD_EVENT_CODE;
				}
				return VendorEventClassEnum.InvalidVendorEventClassValue;
			}

			public static uint VendorEventClassToValue(VendorEventClassEnum enumItem)
			{
				if (enumItem == VendorEventClassEnum.HCI_DOWNLOAD_EVENT_CODE)
				{
					return 0u;
				}
				return uint.MaxValue;
			}

			public static string VendorEventClassToString(VendorEventClassEnum enumItem)
			{
				if (enumItem == VendorEventClassEnum.HCI_DOWNLOAD_EVENT_CODE)
				{
					return "HCI_DOWNLOAD_EVENT_CODE";
				}
				return "Invalid VendorEventClass Value";
			}

			public static string VendorEventClassToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = HCI_DOWNLOAD_EVENT_CODE");
				return stringBuilder.ToString();
			}
		}

		public enum SubOpcodeEnum
		{
			EDL_BOARD_ID_RESPONSE,
			InvalidSubOpcodeValue
		}

		public static class SubOpcodeInfo
		{
			public static SubOpcodeEnum ValueToSubOpcode(uint value)
			{
				if (value == 35)
				{
					return SubOpcodeEnum.EDL_BOARD_ID_RESPONSE;
				}
				return SubOpcodeEnum.InvalidSubOpcodeValue;
			}

			public static uint SubOpcodeToValue(SubOpcodeEnum enumItem)
			{
				if (enumItem == SubOpcodeEnum.EDL_BOARD_ID_RESPONSE)
				{
					return 35u;
				}
				return uint.MaxValue;
			}

			public static string SubOpcodeToString(SubOpcodeEnum enumItem)
			{
				if (enumItem == SubOpcodeEnum.EDL_BOARD_ID_RESPONSE)
				{
					return "EDL_BOARD_ID_RESPONSE";
				}
				return "Invalid SubOpcode Value";
			}

			public static string SubOpcodeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x23 = EDL_BOARD_ID_RESPONSE");
				return stringBuilder.ToString();
			}
		}

		public enum StringLengthEnum
		{
			Number_of_Follow_on_Bytes,
			InvalidStringLengthValue
		}

		public static class StringLengthInfo
		{
			public static StringLengthEnum ValueToStringLength(uint value)
			{
				if (value == 2)
				{
					return StringLengthEnum.Number_of_Follow_on_Bytes;
				}
				return StringLengthEnum.InvalidStringLengthValue;
			}

			public static uint StringLengthToValue(StringLengthEnum enumItem)
			{
				if (enumItem == StringLengthEnum.Number_of_Follow_on_Bytes)
				{
					return 2u;
				}
				return uint.MaxValue;
			}

			public static string StringLengthToString(StringLengthEnum enumItem)
			{
				if (enumItem == StringLengthEnum.Number_of_Follow_on_Bytes)
				{
					return "Number_of_Follow_on_Bytes";
				}
				return "Invalid StringLength Value";
			}

			public static string StringLengthToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x2 = Number_of_Follow_on_Bytes");
				return stringBuilder.ToString();
			}
		}

		private const uint _VendorEventClass = 0u;

		private const uint _SubOpcode = 35u;

		private const uint _StringLength = 2u;

		private uint _BoardId;

		public uint VendorEventClass => 0u;

		public uint SubOpcode => 35u;

		public uint StringLength => 2u;

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

		public VendorEventClassEnum GetVendorEventClassAsVendorEventClassEnum()
		{
			return VendorEventClassInfo.ValueToVendorEventClass(0u);
		}

		public SubOpcodeEnum GetSubOpcodeAsSubOpcodeEnum()
		{
			return SubOpcodeInfo.ValueToSubOpcode(35u);
		}

		public StringLengthEnum GetStringLengthAsStringLengthEnum()
		{
			return StringLengthInfo.ValueToStringLength(2u);
		}
	}

	private const uint _EventCode = 255u;

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

	private PacketInfoBase info;

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

	public uint EventCode => 255u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != EventCode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.VendorEventClass)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubOpcode)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.StringLength)
		{
			return false;
		}
		UserFields.BoardId = bitBuilder.RemoveBitsAsUint(16);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(EventCode, 8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.VendorEventClass, 8);
		bitBuilder.AddBitsAsUint(UserFields.SubOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.StringLength, 8);
		bitBuilder.AddBitsAsUint(UserFields.BoardId, 16);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
