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

namespace QCRPG.TSL.BT.Packets;

public class HciAcceptConnectionRequest : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum RoleEnum
		{
			Master,
			Slave,
			InvalidRoleValue
		}

		public static class RoleInfo
		{
			public static RoleEnum ValueToRole(uint value)
			{
				return value switch
				{
					0u => RoleEnum.Master, 
					1u => RoleEnum.Slave, 
					_ => RoleEnum.InvalidRoleValue, 
				};
			}

			public static uint RoleToValue(RoleEnum enumItem)
			{
				return enumItem switch
				{
					RoleEnum.Master => 0u, 
					RoleEnum.Slave => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string RoleToString(RoleEnum enumItem)
			{
				return enumItem switch
				{
					RoleEnum.Master => "Become the Master for this connection. The LM will perform the role switch.", 
					RoleEnum.Slave => "Remain the Slave for this connection. The LM will NOT perform the role switch.", 
					_ => "Invalid Role Value", 
				};
			}

			public static string RoleToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Become the Master for this connection. The LM will perform the role switch.");
				stringBuilder.AppendLine("0x1 = Remain the Slave for this connection. The LM will NOT perform the role switch.");
				return stringBuilder.ToString();
			}
		}

		private ulong _BdAddr;

		private uint _Role;

		public ulong BdAddr
		{
			get
			{
				return _BdAddr;
			}
			set
			{
				_BdAddr = value;
			}
		}

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

		public RoleEnum GetRoleAsRoleEnum()
		{
			return RoleInfo.ValueToRole(_Role);
		}

		public void SetRoleAsRoleEnum(RoleEnum enumItem)
		{
			Role = RoleInfo.RoleToValue(enumItem);
		}
	}

	private const uint _Opcode = 1033u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForHciAcceptConnectionRequest),
		typeof(ConnectionComplete)
	};

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

	public uint Opcode => 1033u;

	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;
		}
		UserFields.BdAddr = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.Role = 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.AddBitsAsUlongLittleEndian(UserFields.BdAddr, 48);
		bitBuilder.AddBitsAsUint(UserFields.Role, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
