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

namespace QCRPG.TSL.BT.Packets;

public class Disconnect : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ReasonEnum
		{
			AUTHENTICATION_FAILURE,
			REMOTE_USER_TERMINATED_CONNECTION,
			REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_LOW_RESOURCES,
			REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_POWER_OFF,
			UNSUPPORTED_REMOTE_FEATURE,
			PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED,
			UNACCEPTABLE_CONNECTION_PARAMETERS,
			InvalidReasonValue
		}

		public static class ReasonInfo
		{
			public static ReasonEnum ValueToReason(uint value)
			{
				return value switch
				{
					5u => ReasonEnum.AUTHENTICATION_FAILURE, 
					19u => ReasonEnum.REMOTE_USER_TERMINATED_CONNECTION, 
					20u => ReasonEnum.REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_LOW_RESOURCES, 
					21u => ReasonEnum.REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_POWER_OFF, 
					26u => ReasonEnum.UNSUPPORTED_REMOTE_FEATURE, 
					41u => ReasonEnum.PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED, 
					59u => ReasonEnum.UNACCEPTABLE_CONNECTION_PARAMETERS, 
					_ => ReasonEnum.InvalidReasonValue, 
				};
			}

			public static uint ReasonToValue(ReasonEnum enumItem)
			{
				return enumItem switch
				{
					ReasonEnum.AUTHENTICATION_FAILURE => 5u, 
					ReasonEnum.REMOTE_USER_TERMINATED_CONNECTION => 19u, 
					ReasonEnum.REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_LOW_RESOURCES => 20u, 
					ReasonEnum.REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_POWER_OFF => 21u, 
					ReasonEnum.UNSUPPORTED_REMOTE_FEATURE => 26u, 
					ReasonEnum.PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED => 41u, 
					ReasonEnum.UNACCEPTABLE_CONNECTION_PARAMETERS => 59u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ReasonToString(ReasonEnum enumItem)
			{
				return enumItem switch
				{
					ReasonEnum.AUTHENTICATION_FAILURE => "AUTHENTICATION_FAILURE", 
					ReasonEnum.REMOTE_USER_TERMINATED_CONNECTION => "REMOTE_USER_TERMINATED_CONNECTION", 
					ReasonEnum.REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_LOW_RESOURCES => "REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_LOW_RESOURCES", 
					ReasonEnum.REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_POWER_OFF => "REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_POWER_OFF", 
					ReasonEnum.UNSUPPORTED_REMOTE_FEATURE => "UNSUPPORTED_REMOTE_FEATURE", 
					ReasonEnum.PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED => "PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED", 
					ReasonEnum.UNACCEPTABLE_CONNECTION_PARAMETERS => "UNACCEPTABLE_CONNECTION_PARAMETERS", 
					_ => "Invalid Reason Value", 
				};
			}

			public static string ReasonToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x5 = AUTHENTICATION_FAILURE");
				stringBuilder.AppendLine("0x13 = REMOTE_USER_TERMINATED_CONNECTION");
				stringBuilder.AppendLine("0x14 = REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_LOW_RESOURCES");
				stringBuilder.AppendLine("0x15 = REMOTE_DEVICE_TERMINATED_CONNECTION_DUE_TO_POWER_OFF");
				stringBuilder.AppendLine("0x1A = UNSUPPORTED_REMOTE_FEATURE");
				stringBuilder.AppendLine("0x29 = PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED");
				stringBuilder.AppendLine("0x3B = UNACCEPTABLE_CONNECTION_PARAMETERS");
				return stringBuilder.ToString();
			}
		}

		private uint _ConnectionHandle;

		private uint _Reason;

		public uint ConnectionHandle
		{
			get
			{
				return _ConnectionHandle;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for ConnectionHandle is 0xFFFF");
				}
				_ConnectionHandle = 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 = 1030u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForDisconnect),
		typeof(DisconnectionComplete)
	};

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

	public uint Opcode => 1030u;

	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.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		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.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		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();
	}
}
