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

namespace QCRPG.TSL.BT.Packets;

public class LeSendAoaRequestEnable : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum EnableEnum
		{
			Disable_AoA_Request_feature_for_the_connection_defined_in_Connection_Handle_parameter,
			Enable_AoA_Request_feature_for_the_connection_defined_in_Connection_Handle_parameter,
			Reserved_for_future_use,
			InvalidEnableValue
		}

		public static class EnableInfo
		{
			public static EnableEnum ValueToEnable(uint value)
			{
				switch (value)
				{
				case 0u:
					return EnableEnum.Disable_AoA_Request_feature_for_the_connection_defined_in_Connection_Handle_parameter;
				case 1u:
					return EnableEnum.Enable_AoA_Request_feature_for_the_connection_defined_in_Connection_Handle_parameter;
				default:
					if (value >= 2 && value <= 255)
					{
						return EnableEnum.Reserved_for_future_use;
					}
					return EnableEnum.InvalidEnableValue;
				}
			}

			public static uint EnableToValue(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.Disable_AoA_Request_feature_for_the_connection_defined_in_Connection_Handle_parameter => 0u, 
					EnableEnum.Enable_AoA_Request_feature_for_the_connection_defined_in_Connection_Handle_parameter => 1u, 
					EnableEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EnableToString(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.Disable_AoA_Request_feature_for_the_connection_defined_in_Connection_Handle_parameter => "Disable AoA Request feature for the connection defined in Connection_Handle parameter (default).", 
					EnableEnum.Enable_AoA_Request_feature_for_the_connection_defined_in_Connection_Handle_parameter => "Enable AoA Request feature for the connection defined in Connection_Handle parameter.", 
					EnableEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid Enable Value", 
				};
			}

			public static string EnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Disable AoA Request feature for the connection defined in Connection_Handle parameter (default).");
				stringBuilder.AppendLine("0x1 = Enable AoA Request feature for the connection defined in Connection_Handle parameter.");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _ConnectionHandle = 3839u;

		private uint _Enable;

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

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

		public EnableEnum GetEnableAsEnableEnum()
		{
			return EnableInfo.ValueToEnable(_Enable);
		}

		public void SetEnableAsEnableEnum(EnableEnum enumItem)
		{
			Enable = EnableInfo.EnableToValue(enumItem);
		}
	}

	private const uint _Opcode = 8192u;

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

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

	public uint Opcode => 8192u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 3839u;
	}

	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;
		}
		UserFields.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Enable = 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.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.Enable, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.ConnectionHandle < 3839)
		{
			list.Add("Minimum allowed value for Connection_Handle is 0xEFF");
		}
		return list;
	}
}
