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

namespace QCRPG.TSL.BT.Packets;

public class ReadStoredLinkKey : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ReadAllFlagEnum
		{
			Return_Link_Key_for_specified_BD_ADDR,
			Return_all_stored_Link_Keys,
			Reserved_for_future_use,
			InvalidReadAllFlagValue
		}

		public static class ReadAllFlagInfo
		{
			public static ReadAllFlagEnum ValueToReadAllFlag(uint value)
			{
				switch (value)
				{
				case 0u:
					return ReadAllFlagEnum.Return_Link_Key_for_specified_BD_ADDR;
				case 1u:
					return ReadAllFlagEnum.Return_all_stored_Link_Keys;
				default:
					if (value >= 2 && value <= 255)
					{
						return ReadAllFlagEnum.Reserved_for_future_use;
					}
					return ReadAllFlagEnum.InvalidReadAllFlagValue;
				}
			}

			public static uint ReadAllFlagToValue(ReadAllFlagEnum enumItem)
			{
				return enumItem switch
				{
					ReadAllFlagEnum.Return_Link_Key_for_specified_BD_ADDR => 0u, 
					ReadAllFlagEnum.Return_all_stored_Link_Keys => 1u, 
					ReadAllFlagEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ReadAllFlagToString(ReadAllFlagEnum enumItem)
			{
				return enumItem switch
				{
					ReadAllFlagEnum.Return_Link_Key_for_specified_BD_ADDR => "Return_Link_Key_for_specified_BD_ADDR", 
					ReadAllFlagEnum.Return_all_stored_Link_Keys => "Return_all_stored_Link_Keys", 
					ReadAllFlagEnum.Reserved_for_future_use => "Reserved_for_future_use", 
					_ => "Invalid ReadAllFlag Value", 
				};
			}

			public static string ReadAllFlagToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Return_Link_Key_for_specified_BD_ADDR");
				stringBuilder.AppendLine("0x1 = Return_all_stored_Link_Keys");
				stringBuilder.AppendLine("0x2-0xFF = Reserved_for_future_use");
				return stringBuilder.ToString();
			}
		}

		private ulong _BdAddress;

		private uint _ReadAllFlag;

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

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

		public ReadAllFlagEnum GetReadAllFlagAsReadAllFlagEnum()
		{
			return ReadAllFlagInfo.ValueToReadAllFlag(_ReadAllFlag);
		}

		public void SetReadAllFlagAsReadAllFlagEnum(ReadAllFlagEnum enumItem)
		{
			ReadAllFlag = ReadAllFlagInfo.ReadAllFlagToValue(enumItem);
		}
	}

	private const uint _Opcode = 3085u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForReadStoredLinkKey),
		typeof(ReturnLinkKeysEvent)
	};

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

	public uint Opcode => 3085u;

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