using System;
using System.Collections.Generic;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LeApcfSolicitationUuid : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum ApcfActionEnum
		{
			Add,
			Delete,
			Clear,
			InvalidApcfActionValue
		}

		public static class ApcfActionInfo
		{
			public static ApcfActionEnum ValueToApcfAction(uint value)
			{
				return value switch
				{
					0u => ApcfActionEnum.Add, 
					1u => ApcfActionEnum.Delete, 
					2u => ApcfActionEnum.Clear, 
					_ => ApcfActionEnum.InvalidApcfActionValue, 
				};
			}

			public static uint ApcfActionToValue(ApcfActionEnum enumItem)
			{
				return enumItem switch
				{
					ApcfActionEnum.Add => 0u, 
					ApcfActionEnum.Delete => 1u, 
					ApcfActionEnum.Clear => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ApcfActionToString(ApcfActionEnum enumItem)
			{
				return enumItem switch
				{
					ApcfActionEnum.Add => "Add", 
					ApcfActionEnum.Delete => "Delete will delete the solicitation UUID address in the specified filter", 
					ApcfActionEnum.Clear => "Clear will clear all the solicitation UUIDs in the specified filter", 
					_ => "Invalid ApcfAction Value", 
				};
			}

			public static string ApcfActionToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Add");
				stringBuilder.AppendLine("0x1 = Delete will delete the solicitation UUID address in the specified filter");
				stringBuilder.AppendLine("0x2 = Clear will clear all the solicitation UUIDs in the specified filter");
				return stringBuilder.ToString();
			}
		}

		public enum ApcfUuidLengthEnum
		{
			UUID_16_bit,
			UUID_32_bit,
			UUID_128_bit,
			InvalidApcfUuidLengthValue
		}

		public static class ApcfUuidLengthInfo
		{
			public static ApcfUuidLengthEnum ValueToApcfUuidLength(uint value)
			{
				return value switch
				{
					2u => ApcfUuidLengthEnum.UUID_16_bit, 
					4u => ApcfUuidLengthEnum.UUID_32_bit, 
					16u => ApcfUuidLengthEnum.UUID_128_bit, 
					_ => ApcfUuidLengthEnum.InvalidApcfUuidLengthValue, 
				};
			}

			public static uint ApcfUuidLengthToValue(ApcfUuidLengthEnum enumItem)
			{
				return enumItem switch
				{
					ApcfUuidLengthEnum.UUID_16_bit => 2u, 
					ApcfUuidLengthEnum.UUID_32_bit => 4u, 
					ApcfUuidLengthEnum.UUID_128_bit => 16u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ApcfUuidLengthToString(ApcfUuidLengthEnum enumItem)
			{
				return enumItem switch
				{
					ApcfUuidLengthEnum.UUID_16_bit => "16-bit", 
					ApcfUuidLengthEnum.UUID_32_bit => "32-bit", 
					ApcfUuidLengthEnum.UUID_128_bit => "128-bit", 
					_ => "Invalid ApcfUuidLength Value", 
				};
			}

			public static string ApcfUuidLengthToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x2 = 16-bit");
				stringBuilder.AppendLine("0x4 = 32-bit");
				stringBuilder.AppendLine("0x10 = 128-bit");
				return stringBuilder.ToString();
			}
		}

		private const uint _ApcfOpcode = 4u;

		private uint _ApcfAction;

		private uint _ApcfFilterIndex;

		private uint _ApcfUuidLength;

		private List<uint> _ApcfUuidAndMaskList = new List<uint>();

		public uint ApcfOpcode => 4u;

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

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

		public uint ApcfUuidLength
		{
			get
			{
				return _ApcfUuidLength;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for ApcfUuidLength is 0xFF");
				}
				_ApcfUuidLength = value;
				while (_ApcfUuidAndMaskList.Count < _ApcfUuidLength)
				{
					_ApcfUuidAndMaskList.Add(0u);
				}
				while (_ApcfUuidAndMaskList.Count > _ApcfUuidLength)
				{
					_ApcfUuidAndMaskList.RemoveAt(_ApcfUuidAndMaskList.Count - 1);
				}
			}
		}

		public List<uint> ApcfUuidAndMaskList => _ApcfUuidAndMaskList;

		public ApcfActionEnum GetApcfActionAsApcfActionEnum()
		{
			return ApcfActionInfo.ValueToApcfAction(_ApcfAction);
		}

		public void SetApcfActionAsApcfActionEnum(ApcfActionEnum enumItem)
		{
			ApcfAction = ApcfActionInfo.ApcfActionToValue(enumItem);
		}

		public ApcfUuidLengthEnum GetApcfUuidLengthAsApcfUuidLengthEnum()
		{
			return ApcfUuidLengthInfo.ValueToApcfUuidLength(_ApcfUuidLength);
		}

		public void SetApcfUuidLengthAsApcfUuidLengthEnum(ApcfUuidLengthEnum enumItem)
		{
			ApcfUuidLength = ApcfUuidLengthInfo.ApcfUuidLengthToValue(enumItem);
		}
	}

	private const uint _Opcode = 64855u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForLeApcfSolicitationUuid),
		typeof(CommandCompleteForNop)
	};

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

	public uint Opcode => 64855u;

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.ApcfOpcode)
		{
			return false;
		}
		UserFields.ApcfAction = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ApcfFilterIndex = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ApcfUuidLength = (uint)(bitBuilder.GetNumberOfBits() / 8);
		for (int i = 0; i < UserFields.ApcfUuidAndMaskList.Count; i++)
		{
			UserFields.ApcfUuidAndMaskList[i] = 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.AddBitsAsUint(UserFields.ApcfOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.ApcfAction, 8);
		bitBuilder.AddBitsAsUint(UserFields.ApcfFilterIndex, 8);
		for (int i = 0; i < UserFields.ApcfUuidAndMaskList.Count; i++)
		{
			if (UserFields.ApcfUuidAndMaskList[i] > 255)
			{
				throw new Exception("UserFields.ApcfUuidAndMaskList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.ApcfUuidAndMaskList[i], 8);
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
