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

namespace QCRPG.TSL.BT.Packets;

public class LeSetTransmitPowerReportingEnable : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum LocalEnableEnum
		{
			disabled,
			enabled,
			InvalidLocalEnableValue
		}

		public static class LocalEnableInfo
		{
			public static LocalEnableEnum ValueToLocalEnable(uint value)
			{
				return value switch
				{
					0u => LocalEnableEnum.disabled, 
					1u => LocalEnableEnum.enabled, 
					_ => LocalEnableEnum.InvalidLocalEnableValue, 
				};
			}

			public static uint LocalEnableToValue(LocalEnableEnum enumItem)
			{
				return enumItem switch
				{
					LocalEnableEnum.disabled => 0u, 
					LocalEnableEnum.enabled => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LocalEnableToString(LocalEnableEnum enumItem)
			{
				return enumItem switch
				{
					LocalEnableEnum.disabled => "Disable local transmit power reports", 
					LocalEnableEnum.enabled => "Enable local transmit power reports", 
					_ => "Invalid LocalEnable Value", 
				};
			}

			public static string LocalEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Disable local transmit power reports");
				stringBuilder.AppendLine("0x1 = Enable local transmit power reports");
				return stringBuilder.ToString();
			}
		}

		public enum RemoteEnableEnum
		{
			disabled,
			enabled,
			InvalidRemoteEnableValue
		}

		public static class RemoteEnableInfo
		{
			public static RemoteEnableEnum ValueToRemoteEnable(uint value)
			{
				return value switch
				{
					0u => RemoteEnableEnum.disabled, 
					1u => RemoteEnableEnum.enabled, 
					_ => RemoteEnableEnum.InvalidRemoteEnableValue, 
				};
			}

			public static uint RemoteEnableToValue(RemoteEnableEnum enumItem)
			{
				return enumItem switch
				{
					RemoteEnableEnum.disabled => 0u, 
					RemoteEnableEnum.enabled => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string RemoteEnableToString(RemoteEnableEnum enumItem)
			{
				return enumItem switch
				{
					RemoteEnableEnum.disabled => "Disable remote transmit power reports", 
					RemoteEnableEnum.enabled => "Enable remote transmit power reports", 
					_ => "Invalid RemoteEnable Value", 
				};
			}

			public static string RemoteEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Disable remote transmit power reports");
				stringBuilder.AppendLine("0x1 = Enable remote transmit power reports");
				return stringBuilder.ToString();
			}
		}

		private uint _ConnectionHandle;

		private uint _LocalEnable;

		private uint _RemoteEnable;

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

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

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

		public LocalEnableEnum GetLocalEnableAsLocalEnableEnum()
		{
			return LocalEnableInfo.ValueToLocalEnable(_LocalEnable);
		}

		public void SetLocalEnableAsLocalEnableEnum(LocalEnableEnum enumItem)
		{
			LocalEnable = LocalEnableInfo.LocalEnableToValue(enumItem);
		}

		public RemoteEnableEnum GetRemoteEnableAsRemoteEnableEnum()
		{
			return RemoteEnableInfo.ValueToRemoteEnable(_RemoteEnable);
		}

		public void SetRemoteEnableAsRemoteEnableEnum(RemoteEnableEnum enumItem)
		{
			RemoteEnable = RemoteEnableInfo.RemoteEnableToValue(enumItem);
		}
	}

	private const uint _Opcode = 8314u;

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

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

	public uint Opcode => 8314u;

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