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

namespace QCRPG.TSL.BT.Packets;

public class SetControllerToHostFlowControl : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum FlowControlEnableEnum
		{
			Flow_control_off_in_direction_from_Controller_to_Host,
			Flow_control_on_for_HCI_ACL_Data_Packets_and_off_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host,
			Flow_control_off_for_HCI_ACL_Data_Packets_and_on_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host,
			Flow_control_on_both_for_HCI_ACL_Data_Packets_and_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host,
			Reserved,
			InvalidFlowControlEnableValue
		}

		public static class FlowControlEnableInfo
		{
			public static FlowControlEnableEnum ValueToFlowControlEnable(uint value)
			{
				switch (value)
				{
				case 0u:
					return FlowControlEnableEnum.Flow_control_off_in_direction_from_Controller_to_Host;
				case 1u:
					return FlowControlEnableEnum.Flow_control_on_for_HCI_ACL_Data_Packets_and_off_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host;
				case 2u:
					return FlowControlEnableEnum.Flow_control_off_for_HCI_ACL_Data_Packets_and_on_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host;
				case 3u:
					return FlowControlEnableEnum.Flow_control_on_both_for_HCI_ACL_Data_Packets_and_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host;
				default:
					if (value >= 4 && value <= 255)
					{
						return FlowControlEnableEnum.Reserved;
					}
					return FlowControlEnableEnum.InvalidFlowControlEnableValue;
				}
			}

			public static uint FlowControlEnableToValue(FlowControlEnableEnum enumItem)
			{
				return enumItem switch
				{
					FlowControlEnableEnum.Flow_control_off_in_direction_from_Controller_to_Host => 0u, 
					FlowControlEnableEnum.Flow_control_on_for_HCI_ACL_Data_Packets_and_off_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host => 1u, 
					FlowControlEnableEnum.Flow_control_off_for_HCI_ACL_Data_Packets_and_on_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host => 2u, 
					FlowControlEnableEnum.Flow_control_on_both_for_HCI_ACL_Data_Packets_and_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host => 3u, 
					FlowControlEnableEnum.Reserved => 4u, 
					_ => uint.MaxValue, 
				};
			}

			public static string FlowControlEnableToString(FlowControlEnableEnum enumItem)
			{
				return enumItem switch
				{
					FlowControlEnableEnum.Flow_control_off_in_direction_from_Controller_to_Host => "Flow_control_off_in_direction_from_Controller_to_Host", 
					FlowControlEnableEnum.Flow_control_on_for_HCI_ACL_Data_Packets_and_off_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host => "Flow_control_on_for_HCI_ACL_Data_Packets_and_off_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host", 
					FlowControlEnableEnum.Flow_control_off_for_HCI_ACL_Data_Packets_and_on_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host => "Flow_control_off_for_HCI_ACL_Data_Packets_and_on_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host", 
					FlowControlEnableEnum.Flow_control_on_both_for_HCI_ACL_Data_Packets_and_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host => "Flow_control_on_both_for_HCI_ACL_Data_Packets_and_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host", 
					FlowControlEnableEnum.Reserved => "Reserved", 
					_ => "Invalid FlowControlEnable Value", 
				};
			}

			public static string FlowControlEnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Flow_control_off_in_direction_from_Controller_to_Host");
				stringBuilder.AppendLine("0x1 = Flow_control_on_for_HCI_ACL_Data_Packets_and_off_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host");
				stringBuilder.AppendLine("0x2 = Flow_control_off_for_HCI_ACL_Data_Packets_and_on_for_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host");
				stringBuilder.AppendLine("0x3 = Flow_control_on_both_for_HCI_ACL_Data_Packets_and_HCI_synchronous_Data_Packets_in_direction_from_Controller_to_Host");
				stringBuilder.AppendLine("0x4-0xFF = Reserved");
				return stringBuilder.ToString();
			}
		}

		private uint _FlowControlEnable;

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

		public FlowControlEnableEnum GetFlowControlEnableAsFlowControlEnableEnum()
		{
			return FlowControlEnableInfo.ValueToFlowControlEnable(_FlowControlEnable);
		}

		public void SetFlowControlEnableAsFlowControlEnableEnum(FlowControlEnableEnum enumItem)
		{
			FlowControlEnable = FlowControlEnableInfo.FlowControlEnableToValue(enumItem);
		}
	}

	private const uint _Opcode = 3121u;

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

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

	public uint Opcode => 3121u;

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