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

namespace QCRPG.TSL.BT.Packets;

public class LeEnhancedReadTransmitPowerLevel : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PhyEnum
		{
			LE_PHY_1M,
			LE_PHY_2M,
			LE_PHY_Coded_S8,
			LE_PHY_Coded_S2,
			InvalidPhyValue
		}

		public static class PhyInfo
		{
			public static PhyEnum ValueToPhy(uint value)
			{
				return value switch
				{
					1u => PhyEnum.LE_PHY_1M, 
					2u => PhyEnum.LE_PHY_2M, 
					3u => PhyEnum.LE_PHY_Coded_S8, 
					4u => PhyEnum.LE_PHY_Coded_S2, 
					_ => value switch
					{
						1u => PhyEnum.LE_PHY_1M, 
						2u => PhyEnum.LE_PHY_2M, 
						3u => PhyEnum.LE_PHY_Coded_S8, 
						4u => PhyEnum.LE_PHY_Coded_S2, 
						_ => PhyEnum.InvalidPhyValue, 
					}, 
				};
			}

			public static uint PhyToValue(PhyEnum enumItem)
			{
				return enumItem switch
				{
					PhyEnum.LE_PHY_1M => 1u, 
					PhyEnum.LE_PHY_2M => 2u, 
					PhyEnum.LE_PHY_Coded_S8 => 3u, 
					PhyEnum.LE_PHY_Coded_S2 => 4u, 
					_ => enumItem switch
					{
						PhyEnum.LE_PHY_1M => 1u, 
						PhyEnum.LE_PHY_2M => 2u, 
						PhyEnum.LE_PHY_Coded_S8 => 3u, 
						PhyEnum.LE_PHY_Coded_S2 => 4u, 
						_ => uint.MaxValue, 
					}, 
				};
			}

			public static string PhyToString(PhyEnum enumItem)
			{
				return enumItem switch
				{
					PhyEnum.LE_PHY_1M => "LE 1M PHY", 
					PhyEnum.LE_PHY_2M => "LE 2M PHY", 
					PhyEnum.LE_PHY_Coded_S8 => "LE Coded PHY with S=8 data coding", 
					PhyEnum.LE_PHY_Coded_S2 => "LE Coded PHY with S=2 data coding", 
					_ => enumItem switch
					{
						PhyEnum.LE_PHY_1M => "LE 1M PHY", 
						PhyEnum.LE_PHY_2M => "LE 2M PHY", 
						PhyEnum.LE_PHY_Coded_S8 => "LE Coded PHY with S=8 data coding", 
						PhyEnum.LE_PHY_Coded_S2 => "LE Coded PHY with S=2 data coding", 
						_ => "Invalid Phy Value", 
					}, 
				};
			}

			public static string PhyToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x1 = LE 1M PHY");
				stringBuilder.AppendLine("0x2 = LE 2M PHY");
				stringBuilder.AppendLine("0x3 = LE Coded PHY with S=8 data coding");
				stringBuilder.AppendLine("0x4 = LE Coded PHY with S=2 data coding");
				stringBuilder.AppendLine("0x1 = LE 1M PHY");
				stringBuilder.AppendLine("0x2 = LE 2M PHY");
				stringBuilder.AppendLine("0x3 = LE Coded PHY with S=8 data coding");
				stringBuilder.AppendLine("0x4 = LE Coded PHY with S=2 data coding");
				return stringBuilder.ToString();
			}
		}

		private uint _ConnectionHandle;

		private uint _Phy;

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

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

		public PhyEnum GetPhyAsPhyEnum()
		{
			return PhyInfo.ValueToPhy(_Phy);
		}

		public void SetPhyAsPhyEnum(PhyEnum enumItem)
		{
			Phy = PhyInfo.PhyToValue(enumItem);
		}
	}

	private const uint _Opcode = 8310u;

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

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

	public uint Opcode => 8310u;

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