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

namespace QCRPG.TSL.BT.Packets;

public class HciVsSetBaudrate : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum BaudRateEnum
		{
			BaudRate_115200_bps,
			BaudRate_57600_bps,
			BaudRate_38400_bps,
			BaudRate_19200_bps,
			BaudRate_9600_bps,
			BaudRate_230400_bps,
			BaudRate_250000_bps,
			BaudRate_460800_bps,
			BaudRate_500000_bps,
			BaudRate_720000_bps,
			BaudRate_921600_bps,
			BaudRate_1000000_bps,
			BaudRate_125000_bps,
			BaudRate_2000000_bps,
			BaudRate_3000000_bps,
			BaudRate_4000000_bps,
			BaudRate_1600000_bps,
			BaudRate_3200000_bps,
			BaudRate_3500000_bps,
			InvalidBaudRateValue
		}

		public static class BaudRateInfo
		{
			public static BaudRateEnum ValueToBaudRate(uint value)
			{
				return value switch
				{
					0u => BaudRateEnum.BaudRate_115200_bps, 
					1u => BaudRateEnum.BaudRate_57600_bps, 
					2u => BaudRateEnum.BaudRate_38400_bps, 
					3u => BaudRateEnum.BaudRate_19200_bps, 
					4u => BaudRateEnum.BaudRate_9600_bps, 
					5u => BaudRateEnum.BaudRate_230400_bps, 
					6u => BaudRateEnum.BaudRate_250000_bps, 
					7u => BaudRateEnum.BaudRate_460800_bps, 
					8u => BaudRateEnum.BaudRate_500000_bps, 
					9u => BaudRateEnum.BaudRate_720000_bps, 
					10u => BaudRateEnum.BaudRate_921600_bps, 
					11u => BaudRateEnum.BaudRate_1000000_bps, 
					12u => BaudRateEnum.BaudRate_125000_bps, 
					13u => BaudRateEnum.BaudRate_2000000_bps, 
					14u => BaudRateEnum.BaudRate_3000000_bps, 
					15u => BaudRateEnum.BaudRate_4000000_bps, 
					16u => BaudRateEnum.BaudRate_1600000_bps, 
					17u => BaudRateEnum.BaudRate_3200000_bps, 
					18u => BaudRateEnum.BaudRate_3500000_bps, 
					_ => BaudRateEnum.InvalidBaudRateValue, 
				};
			}

			public static uint BaudRateToValue(BaudRateEnum enumItem)
			{
				return enumItem switch
				{
					BaudRateEnum.BaudRate_115200_bps => 0u, 
					BaudRateEnum.BaudRate_57600_bps => 1u, 
					BaudRateEnum.BaudRate_38400_bps => 2u, 
					BaudRateEnum.BaudRate_19200_bps => 3u, 
					BaudRateEnum.BaudRate_9600_bps => 4u, 
					BaudRateEnum.BaudRate_230400_bps => 5u, 
					BaudRateEnum.BaudRate_250000_bps => 6u, 
					BaudRateEnum.BaudRate_460800_bps => 7u, 
					BaudRateEnum.BaudRate_500000_bps => 8u, 
					BaudRateEnum.BaudRate_720000_bps => 9u, 
					BaudRateEnum.BaudRate_921600_bps => 10u, 
					BaudRateEnum.BaudRate_1000000_bps => 11u, 
					BaudRateEnum.BaudRate_125000_bps => 12u, 
					BaudRateEnum.BaudRate_2000000_bps => 13u, 
					BaudRateEnum.BaudRate_3000000_bps => 14u, 
					BaudRateEnum.BaudRate_4000000_bps => 15u, 
					BaudRateEnum.BaudRate_1600000_bps => 16u, 
					BaudRateEnum.BaudRate_3200000_bps => 17u, 
					BaudRateEnum.BaudRate_3500000_bps => 18u, 
					_ => uint.MaxValue, 
				};
			}

			public static string BaudRateToString(BaudRateEnum enumItem)
			{
				return enumItem switch
				{
					BaudRateEnum.BaudRate_115200_bps => "BaudRate_115200_bps", 
					BaudRateEnum.BaudRate_57600_bps => "BaudRate_57600_bps", 
					BaudRateEnum.BaudRate_38400_bps => "BaudRate_38400_bps", 
					BaudRateEnum.BaudRate_19200_bps => "BaudRate_19200_bps", 
					BaudRateEnum.BaudRate_9600_bps => "BaudRate_9600_bps", 
					BaudRateEnum.BaudRate_230400_bps => "BaudRate_230400_bps", 
					BaudRateEnum.BaudRate_250000_bps => "BaudRate_250000_bps", 
					BaudRateEnum.BaudRate_460800_bps => "BaudRate_460800_bps", 
					BaudRateEnum.BaudRate_500000_bps => "BaudRate_500000_bps", 
					BaudRateEnum.BaudRate_720000_bps => "BaudRate_720000_bps", 
					BaudRateEnum.BaudRate_921600_bps => "BaudRate_921600_bps", 
					BaudRateEnum.BaudRate_1000000_bps => "BaudRate_1000000_bps", 
					BaudRateEnum.BaudRate_125000_bps => "BaudRate_125000_bps", 
					BaudRateEnum.BaudRate_2000000_bps => "BaudRate_2000000_bps", 
					BaudRateEnum.BaudRate_3000000_bps => "BaudRate_3000000_bps", 
					BaudRateEnum.BaudRate_4000000_bps => "BaudRate_4000000_bps", 
					BaudRateEnum.BaudRate_1600000_bps => "BaudRate_1600000_bps", 
					BaudRateEnum.BaudRate_3200000_bps => "BaudRate_3200000_bps", 
					BaudRateEnum.BaudRate_3500000_bps => "BaudRate_3500000_bps", 
					_ => "Invalid BaudRate Value", 
				};
			}

			public static string BaudRateToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = BaudRate_115200_bps");
				stringBuilder.AppendLine("0x1 = BaudRate_57600_bps");
				stringBuilder.AppendLine("0x2 = BaudRate_38400_bps");
				stringBuilder.AppendLine("0x3 = BaudRate_19200_bps");
				stringBuilder.AppendLine("0x4 = BaudRate_9600_bps");
				stringBuilder.AppendLine("0x5 = BaudRate_230400_bps");
				stringBuilder.AppendLine("0x6 = BaudRate_250000_bps");
				stringBuilder.AppendLine("0x7 = BaudRate_460800_bps");
				stringBuilder.AppendLine("0x8 = BaudRate_500000_bps");
				stringBuilder.AppendLine("0x9 = BaudRate_720000_bps");
				stringBuilder.AppendLine("0xA = BaudRate_921600_bps");
				stringBuilder.AppendLine("0xB = BaudRate_1000000_bps");
				stringBuilder.AppendLine("0xC = BaudRate_125000_bps");
				stringBuilder.AppendLine("0xD = BaudRate_2000000_bps");
				stringBuilder.AppendLine("0xE = BaudRate_3000000_bps");
				stringBuilder.AppendLine("0xF = BaudRate_4000000_bps");
				stringBuilder.AppendLine("0x10 = BaudRate_1600000_bps");
				stringBuilder.AppendLine("0x11 = BaudRate_3200000_bps");
				stringBuilder.AppendLine("0x12 = BaudRate_3500000_bps");
				return stringBuilder.ToString();
			}
		}

		private uint _BaudRate;

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

		public BaudRateEnum GetBaudRateAsBaudRateEnum()
		{
			return BaudRateInfo.ValueToBaudRate(_BaudRate);
		}

		public void SetBaudRateAsBaudRateEnum(BaudRateEnum enumItem)
		{
			BaudRate = BaudRateInfo.BaudRateToValue(enumItem);
		}
	}

	private const uint _Opcode = 64584u;

	private Type[] expectedResponseTypes = new Type[0];

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

	public uint Opcode => 64584u;

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