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

namespace QCRPG.TSL.BT.Packets;

public class DeleteStoredLinkKey : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum DeleteAllFlagEnum
		{
			Delete_only_the_Link_Key_for_specified_BD_ADDR,
			Delete_all_stored_Link_Keys,
			Reserved_for_future_use,
			InvalidDeleteAllFlagValue
		}

		public static class DeleteAllFlagInfo
		{
			public static DeleteAllFlagEnum ValueToDeleteAllFlag(uint value)
			{
				switch (value)
				{
				case 0u:
					return DeleteAllFlagEnum.Delete_only_the_Link_Key_for_specified_BD_ADDR;
				case 1u:
					return DeleteAllFlagEnum.Delete_all_stored_Link_Keys;
				default:
					if (value >= 2 && value <= 255)
					{
						return DeleteAllFlagEnum.Reserved_for_future_use;
					}
					return DeleteAllFlagEnum.InvalidDeleteAllFlagValue;
				}
			}

			public static uint DeleteAllFlagToValue(DeleteAllFlagEnum enumItem)
			{
				return enumItem switch
				{
					DeleteAllFlagEnum.Delete_only_the_Link_Key_for_specified_BD_ADDR => 0u, 
					DeleteAllFlagEnum.Delete_all_stored_Link_Keys => 1u, 
					DeleteAllFlagEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string DeleteAllFlagToString(DeleteAllFlagEnum enumItem)
			{
				return enumItem switch
				{
					DeleteAllFlagEnum.Delete_only_the_Link_Key_for_specified_BD_ADDR => "Delete_only_the_Link_Key_for_specified_BD_ADDR", 
					DeleteAllFlagEnum.Delete_all_stored_Link_Keys => "Delete_all_stored_Link_Keys", 
					DeleteAllFlagEnum.Reserved_for_future_use => "Reserved_for_future_use", 
					_ => "Invalid DeleteAllFlag Value", 
				};
			}

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

		private ulong _BdAddress;

		private uint _DeleteAllFlag;

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

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

		public DeleteAllFlagEnum GetDeleteAllFlagAsDeleteAllFlagEnum()
		{
			return DeleteAllFlagInfo.ValueToDeleteAllFlag(_DeleteAllFlag);
		}

		public void SetDeleteAllFlagAsDeleteAllFlagEnum(DeleteAllFlagEnum enumItem)
		{
			DeleteAllFlag = DeleteAllFlagInfo.DeleteAllFlagToValue(enumItem);
		}
	}

	private const uint _Opcode = 3090u;

	private Type[] expectedResponseTypes = new Type[1] { typeof(CommandCompleteForDeleteStoredLinkKey) };

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

	public uint Opcode => 3090u;

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