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

namespace QCRPG.TSL.BT.Packets;

public class LtLePingTest : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PingTestModeEnum
		{
			TRIGGER,
			IDLE,
			NORMAL,
			NO_RSP,
			RSP_UNKNOWN,
			NO_INIT,
			InvalidPingTestModeValue
		}

		public static class PingTestModeInfo
		{
			public static PingTestModeEnum ValueToPingTestMode(uint value)
			{
				return value switch
				{
					0u => PingTestModeEnum.TRIGGER, 
					1u => PingTestModeEnum.IDLE, 
					2u => PingTestModeEnum.NORMAL, 
					4u => PingTestModeEnum.NO_RSP, 
					8u => PingTestModeEnum.RSP_UNKNOWN, 
					13u => PingTestModeEnum.NO_INIT, 
					_ => PingTestModeEnum.InvalidPingTestModeValue, 
				};
			}

			public static uint PingTestModeToValue(PingTestModeEnum enumItem)
			{
				return enumItem switch
				{
					PingTestModeEnum.TRIGGER => 0u, 
					PingTestModeEnum.IDLE => 1u, 
					PingTestModeEnum.NORMAL => 2u, 
					PingTestModeEnum.NO_RSP => 4u, 
					PingTestModeEnum.RSP_UNKNOWN => 8u, 
					PingTestModeEnum.NO_INIT => 13u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PingTestModeToString(PingTestModeEnum enumItem)
			{
				return enumItem switch
				{
					PingTestModeEnum.TRIGGER => "Trigger LL_PING_REQ", 
					PingTestModeEnum.IDLE => "Report LL_PING_REQ", 
					PingTestModeEnum.NORMAL => "Send LL_PING_REQ, report LL_PING_RSP", 
					PingTestModeEnum.NO_RSP => "Report LL_PING_REQ. Do not send LL_PING_RES ", 
					PingTestModeEnum.RSP_UNKNOWN => "report LL_PING_REQ. Respond with LL_UNKNOWN_REQ", 
					PingTestModeEnum.NO_INIT => "PING_MODE_IDLE | PING_MODE_NORSP | PING_MODE_RSPUNKNOWN", 
					_ => "Invalid PingTestMode Value", 
				};
			}

			public static string PingTestModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Trigger LL_PING_REQ");
				stringBuilder.AppendLine("0x1 = Report LL_PING_REQ");
				stringBuilder.AppendLine("0x2 = Send LL_PING_REQ, report LL_PING_RSP");
				stringBuilder.AppendLine("0x4 = Report LL_PING_REQ. Do not send LL_PING_RES ");
				stringBuilder.AppendLine("0x8 = report LL_PING_REQ. Respond with LL_UNKNOWN_REQ");
				stringBuilder.AppendLine("0xD = PING_MODE_IDLE | PING_MODE_NORSP | PING_MODE_RSPUNKNOWN");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 6u;

		private uint _PingTestMode;

		public uint SubOpcode => 6u;

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

		public PingTestModeEnum GetPingTestModeAsPingTestModeEnum()
		{
			return PingTestModeInfo.ValueToPingTestMode(_PingTestMode);
		}

		public void SetPingTestModeAsPingTestModeEnum(PingTestModeEnum enumItem)
		{
			PingTestMode = PingTestModeInfo.PingTestModeToValue(enumItem);
		}
	}

	private const uint _Opcode = 64536u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForLowerTester),
		typeof(CommandCompleteForNop)
	};

	private PacketInfoBase info;

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

	public uint Opcode => 64536u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != Opcode)
		{
			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.PingTestMode = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

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