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

namespace QCRPG.TSL.BT.Packets;

public class RejectConnectionRequest : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ReasonEnum
		{
			HOST_REJECT_ERROR_CODE,
			InvalidReasonValue
		}

		public static class ReasonInfo
		{
			public static ReasonEnum ValueToReason(uint value)
			{
				if (value >= 13 && value <= 15)
				{
					return ReasonEnum.HOST_REJECT_ERROR_CODE;
				}
				return ReasonEnum.InvalidReasonValue;
			}

			public static uint ReasonToValue(ReasonEnum enumItem)
			{
				if (enumItem == ReasonEnum.HOST_REJECT_ERROR_CODE)
				{
					return 13u;
				}
				return uint.MaxValue;
			}

			public static string ReasonToString(ReasonEnum enumItem)
			{
				if (enumItem == ReasonEnum.HOST_REJECT_ERROR_CODE)
				{
					return "Host Reject Error Code";
				}
				return "Invalid Reason Value";
			}

			public static string ReasonToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0xD-0xF = Host Reject Error Code");
				return stringBuilder.ToString();
			}
		}

		private ulong _BdAddress;

		private uint _Reason;

		public ulong BdAddress
		{
			get
			{
				return _BdAddress;
			}
			set
			{
				_BdAddress = value;
			}
		}

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

		public ReasonEnum GetReasonAsReasonEnum()
		{
			return ReasonInfo.ValueToReason(_Reason);
		}

		public void SetReasonAsReasonEnum(ReasonEnum enumItem)
		{
			Reason = ReasonInfo.ReasonToValue(enumItem);
		}
	}

	private const uint _Opcode = 1034u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForRejectConnectionRequest),
		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 RejectConnectionRequestInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 1034u;

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