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

namespace QCRPG.TSL.BT.Packets;

public class LtMep23TestMode : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ModeEnum
		{
			NULL_SENT_AS_PAGE_ID,
			SLAVE_SEND_POLL_PKT,
			DISCONNECT_IN_SNIFF,
			PRI_ADV,
			CONSECUTIVE_FEA_REQ,
			SECOND_VER_IND,
			InvalidModeValue
		}

		public static class ModeInfo
		{
			public static ModeEnum ValueToMode(uint value)
			{
				return value switch
				{
					1u => ModeEnum.NULL_SENT_AS_PAGE_ID, 
					2u => ModeEnum.SLAVE_SEND_POLL_PKT, 
					3u => ModeEnum.DISCONNECT_IN_SNIFF, 
					4u => ModeEnum.PRI_ADV, 
					5u => ModeEnum.CONSECUTIVE_FEA_REQ, 
					6u => ModeEnum.SECOND_VER_IND, 
					_ => ModeEnum.InvalidModeValue, 
				};
			}

			public static uint ModeToValue(ModeEnum enumItem)
			{
				return enumItem switch
				{
					ModeEnum.NULL_SENT_AS_PAGE_ID => 1u, 
					ModeEnum.SLAVE_SEND_POLL_PKT => 2u, 
					ModeEnum.DISCONNECT_IN_SNIFF => 3u, 
					ModeEnum.PRI_ADV => 4u, 
					ModeEnum.CONSECUTIVE_FEA_REQ => 5u, 
					ModeEnum.SECOND_VER_IND => 6u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ModeToString(ModeEnum enumItem)
			{
				return enumItem switch
				{
					ModeEnum.NULL_SENT_AS_PAGE_ID => "Test the case received NULL packet during page response", 
					ModeEnum.SLAVE_SEND_POLL_PKT => "Test the case received POLL packet from slave", 
					ModeEnum.DISCONNECT_IN_SNIFF => "Test the case received disconnect request in sniff mode", 
					ModeEnum.PRI_ADV => "Test the case we received a unexpected packet on Primary Adv channel", 
					ModeEnum.CONSECUTIVE_FEA_REQ => "Test the case we received two consecutive feature request", 
					ModeEnum.SECOND_VER_IND => "Test the case we received second version indication on same connection", 
					_ => "Invalid Mode Value", 
				};
			}

			public static string ModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x1 = Test the case received NULL packet during page response");
				stringBuilder.AppendLine("0x2 = Test the case received POLL packet from slave");
				stringBuilder.AppendLine("0x3 = Test the case received disconnect request in sniff mode");
				stringBuilder.AppendLine("0x4 = Test the case we received a unexpected packet on Primary Adv channel");
				stringBuilder.AppendLine("0x5 = Test the case we received two consecutive feature request");
				stringBuilder.AppendLine("0x6 = Test the case we received second version indication on same connection");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubOpcode = 17u;

		private uint _Mode;

		public uint SubOpcode => 17u;

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

		public ModeEnum GetModeAsModeEnum()
		{
			return ModeInfo.ValueToMode(_Mode);
		}

		public void SetModeAsModeEnum(ModeEnum enumItem)
		{
			Mode = ModeInfo.ModeToValue(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 LtMep23TestModeInfo(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.Mode = 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.Mode, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
