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

namespace QCRPG.TSL.BT.Packets;

public class Q2qDebugEvent : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum Q2qOpcodeEnum
		{
			STATE,
			LINKKEY,
			AUTHRES,
			OWNADDR,
			EST_RSP_TO,
			EST_MASTER,
			EST_SLAVE,
			CNT_VAL,
			FEATURE_DONE,
			FEATURE_SET,
			PHY_NOTI,
			PHY_VAL,
			PWR_CTRL_RSP,
			PWR_CTRL_IND,
			PWR_CTRL_REJECT,
			GENE_DEBUG,
			InvalidQ2qOpcodeValue
		}

		public static class Q2qOpcodeInfo
		{
			public static Q2qOpcodeEnum ValueToQ2qOpcode(uint value)
			{
				return value switch
				{
					0u => Q2qOpcodeEnum.STATE, 
					1u => Q2qOpcodeEnum.LINKKEY, 
					2u => Q2qOpcodeEnum.AUTHRES, 
					3u => Q2qOpcodeEnum.OWNADDR, 
					4u => Q2qOpcodeEnum.EST_RSP_TO, 
					5u => Q2qOpcodeEnum.EST_MASTER, 
					6u => Q2qOpcodeEnum.EST_SLAVE, 
					7u => Q2qOpcodeEnum.CNT_VAL, 
					8u => Q2qOpcodeEnum.FEATURE_DONE, 
					9u => Q2qOpcodeEnum.FEATURE_SET, 
					10u => Q2qOpcodeEnum.PHY_NOTI, 
					11u => Q2qOpcodeEnum.PHY_VAL, 
					12u => Q2qOpcodeEnum.PWR_CTRL_RSP, 
					13u => Q2qOpcodeEnum.PWR_CTRL_IND, 
					14u => Q2qOpcodeEnum.PWR_CTRL_REJECT, 
					15u => Q2qOpcodeEnum.GENE_DEBUG, 
					_ => Q2qOpcodeEnum.InvalidQ2qOpcodeValue, 
				};
			}

			public static uint Q2qOpcodeToValue(Q2qOpcodeEnum enumItem)
			{
				return enumItem switch
				{
					Q2qOpcodeEnum.STATE => 0u, 
					Q2qOpcodeEnum.LINKKEY => 1u, 
					Q2qOpcodeEnum.AUTHRES => 2u, 
					Q2qOpcodeEnum.OWNADDR => 3u, 
					Q2qOpcodeEnum.EST_RSP_TO => 4u, 
					Q2qOpcodeEnum.EST_MASTER => 5u, 
					Q2qOpcodeEnum.EST_SLAVE => 6u, 
					Q2qOpcodeEnum.CNT_VAL => 7u, 
					Q2qOpcodeEnum.FEATURE_DONE => 8u, 
					Q2qOpcodeEnum.FEATURE_SET => 9u, 
					Q2qOpcodeEnum.PHY_NOTI => 10u, 
					Q2qOpcodeEnum.PHY_VAL => 11u, 
					Q2qOpcodeEnum.PWR_CTRL_RSP => 12u, 
					Q2qOpcodeEnum.PWR_CTRL_IND => 13u, 
					Q2qOpcodeEnum.PWR_CTRL_REJECT => 14u, 
					Q2qOpcodeEnum.GENE_DEBUG => 15u, 
					_ => uint.MaxValue, 
				};
			}

			public static string Q2qOpcodeToString(Q2qOpcodeEnum enumItem)
			{
				return enumItem switch
				{
					Q2qOpcodeEnum.STATE => "QLL/QLMP State notification", 
					Q2qOpcodeEnum.LINKKEY => "QLL/QLMP link key notification", 
					Q2qOpcodeEnum.AUTHRES => "QLL/QLMP authentication response notification", 
					Q2qOpcodeEnum.OWNADDR => "QLL own device address notification", 
					Q2qOpcodeEnum.EST_RSP_TO => "QLL/QLMP  establishment time out notification", 
					Q2qOpcodeEnum.EST_MASTER => "QLL/QLMP  master establishment notification", 
					Q2qOpcodeEnum.EST_SLAVE => "QLL/QLMP  slave establishment notification", 
					Q2qOpcodeEnum.CNT_VAL => "Auth Time out value", 
					Q2qOpcodeEnum.FEATURE_DONE => "QLL/QLMP  feature exchange finish notification", 
					Q2qOpcodeEnum.FEATURE_SET => "QLL/QLMP  received feature set", 
					Q2qOpcodeEnum.PHY_NOTI => "QLL phy update notification", 
					Q2qOpcodeEnum.PHY_VAL => "QLL phy update val", 
					Q2qOpcodeEnum.PWR_CTRL_RSP => "QLL power control response notification", 
					Q2qOpcodeEnum.PWR_CTRL_IND => "QLL power control indication notification", 
					Q2qOpcodeEnum.PWR_CTRL_REJECT => "QLL power control rejection notification", 
					Q2qOpcodeEnum.GENE_DEBUG => "General debug output", 
					_ => "Invalid Q2qOpcode Value", 
				};
			}

			public static string Q2qOpcodeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = QLL/QLMP State notification");
				stringBuilder.AppendLine("0x1 = QLL/QLMP link key notification");
				stringBuilder.AppendLine("0x2 = QLL/QLMP authentication response notification");
				stringBuilder.AppendLine("0x3 = QLL own device address notification");
				stringBuilder.AppendLine("0x4 = QLL/QLMP  establishment time out notification");
				stringBuilder.AppendLine("0x5 = QLL/QLMP  master establishment notification");
				stringBuilder.AppendLine("0x6 = QLL/QLMP  slave establishment notification");
				stringBuilder.AppendLine("0x7 = Auth Time out value");
				stringBuilder.AppendLine("0x8 = QLL/QLMP  feature exchange finish notification");
				stringBuilder.AppendLine("0x9 = QLL/QLMP  received feature set");
				stringBuilder.AppendLine("0xA = QLL phy update notification");
				stringBuilder.AppendLine("0xB = QLL phy update val");
				stringBuilder.AppendLine("0xC = QLL power control response notification");
				stringBuilder.AppendLine("0xD = QLL power control indication notification");
				stringBuilder.AppendLine("0xE = QLL power control rejection notification");
				stringBuilder.AppendLine("0xF = General debug output");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 155u;

		private uint _Q2qOpcode;

		public uint SubOpcode => 155u;

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

		public Q2qOpcodeEnum GetQ2qOpcodeAsQ2qOpcodeEnum()
		{
			return Q2qOpcodeInfo.ValueToQ2qOpcode(_Q2qOpcode);
		}

		public void SetQ2qOpcodeAsQ2qOpcodeEnum(Q2qOpcodeEnum enumItem)
		{
			Q2qOpcode = Q2qOpcodeInfo.Q2qOpcodeToValue(enumItem);
		}
	}

	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 Q2qDebugEventInfo(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.SubOpcode)
		{
			return false;
		}
		UserFields.Q2qOpcode = bitBuilder.RemoveBitsAsUint(8);
		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.SubOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Q2qOpcode, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
