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

namespace QCRPG.TSL.BT.Packets;

public class DisconnectLogicalLink : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum LogicalLinkHandleEnum
		{
			LOGICAL_LINK_HANDLE_FOR_THE_LOGICAL_LINK_BEING_DISCONNECTED,
			RESERVED_FOR_FUTURE_USE,
			InvalidLogicalLinkHandleValue
		}

		public static class LogicalLinkHandleInfo
		{
			public static LogicalLinkHandleEnum ValueToLogicalLinkHandle(uint value)
			{
				if (value >= 0 && value <= 3839)
				{
					return LogicalLinkHandleEnum.LOGICAL_LINK_HANDLE_FOR_THE_LOGICAL_LINK_BEING_DISCONNECTED;
				}
				if (value >= 3840 && value <= 4095)
				{
					return LogicalLinkHandleEnum.RESERVED_FOR_FUTURE_USE;
				}
				return LogicalLinkHandleEnum.InvalidLogicalLinkHandleValue;
			}

			public static uint LogicalLinkHandleToValue(LogicalLinkHandleEnum enumItem)
			{
				return enumItem switch
				{
					LogicalLinkHandleEnum.LOGICAL_LINK_HANDLE_FOR_THE_LOGICAL_LINK_BEING_DISCONNECTED => 0u, 
					LogicalLinkHandleEnum.RESERVED_FOR_FUTURE_USE => 3840u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LogicalLinkHandleToString(LogicalLinkHandleEnum enumItem)
			{
				return enumItem switch
				{
					LogicalLinkHandleEnum.LOGICAL_LINK_HANDLE_FOR_THE_LOGICAL_LINK_BEING_DISCONNECTED => "Logical_Link_Handle for the logical link being disconnected", 
					LogicalLinkHandleEnum.RESERVED_FOR_FUTURE_USE => "Reserved for future use", 
					_ => "Invalid LogicalLinkHandle Value", 
				};
			}

			public static string LogicalLinkHandleToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0-0xEFF = Logical_Link_Handle for the logical link being disconnected");
				stringBuilder.AppendLine("0xF00-0xFFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _LogicalLinkHandle;

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

		public LogicalLinkHandleEnum GetLogicalLinkHandleAsLogicalLinkHandleEnum()
		{
			return LogicalLinkHandleInfo.ValueToLogicalLinkHandle(_LogicalLinkHandle);
		}

		public void SetLogicalLinkHandleAsLogicalLinkHandleEnum(LogicalLinkHandleEnum enumItem)
		{
			LogicalLinkHandle = LogicalLinkHandleInfo.LogicalLinkHandleToValue(enumItem);
		}
	}

	private const uint _Opcode = 1082u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForDisconnectLogicalLink),
		typeof(DisconnectionLogicalLinkComplete)
	};

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

	public uint Opcode => 1082u;

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