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

namespace QCRPG.TSL.BT.Packets;

public class SetFeatureBit : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum FeatureSetOrClearEnum
		{
			Clear_related_bit,
			Error_code_for_HCI_command,
			InvalidFeatureSetOrClearValue
		}

		public static class FeatureSetOrClearInfo
		{
			public static FeatureSetOrClearEnum ValueToFeatureSetOrClear(uint value)
			{
				if (value == 0)
				{
					return FeatureSetOrClearEnum.Clear_related_bit;
				}
				if (value >= 1 && value <= 255)
				{
					return FeatureSetOrClearEnum.Error_code_for_HCI_command;
				}
				return FeatureSetOrClearEnum.InvalidFeatureSetOrClearValue;
			}

			public static uint FeatureSetOrClearToValue(FeatureSetOrClearEnum enumItem)
			{
				return enumItem switch
				{
					FeatureSetOrClearEnum.Clear_related_bit => 0u, 
					FeatureSetOrClearEnum.Error_code_for_HCI_command => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string FeatureSetOrClearToString(FeatureSetOrClearEnum enumItem)
			{
				return enumItem switch
				{
					FeatureSetOrClearEnum.Clear_related_bit => "Clear related_ bit", 
					FeatureSetOrClearEnum.Error_code_for_HCI_command => "Error code for HCI command", 
					_ => "Invalid FeatureSetOrClear Value", 
				};
			}

			public static string FeatureSetOrClearToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Clear related_ bit");
				stringBuilder.AppendLine("0x1-0xFF = Error code for HCI command");
				return stringBuilder.ToString();
			}
		}

		private uint _BitNumber;

		private uint _FeatureSetOrClear;

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

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

		public FeatureSetOrClearEnum GetFeatureSetOrClearAsFeatureSetOrClearEnum()
		{
			return FeatureSetOrClearInfo.ValueToFeatureSetOrClear(_FeatureSetOrClear);
		}

		public void SetFeatureSetOrClearAsFeatureSetOrClearEnum(FeatureSetOrClearEnum enumItem)
		{
			FeatureSetOrClear = FeatureSetOrClearInfo.FeatureSetOrClearToValue(enumItem);
		}
	}

	private const uint _Opcode = 64533u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForSetFeatureBit),
		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 SetFeatureBitInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 64533u;

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