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

namespace QCRPG.TSL.BT.Packets;

public class WriteFlowControlMode : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum FlowControlModeEnum
		{
			Packet_based_data_flow_control_mode_default_for_a_BR_EDR_Controller,
			Data_block_based_data_flow_control_mode_default_for_an_AMP_Controller,
			Reserved_for_future_use,
			InvalidFlowControlModeValue
		}

		public static class FlowControlModeInfo
		{
			public static FlowControlModeEnum ValueToFlowControlMode(uint value)
			{
				switch (value)
				{
				case 0u:
					return FlowControlModeEnum.Packet_based_data_flow_control_mode_default_for_a_BR_EDR_Controller;
				case 1u:
					return FlowControlModeEnum.Data_block_based_data_flow_control_mode_default_for_an_AMP_Controller;
				default:
					if (value >= 2 && value <= 255)
					{
						return FlowControlModeEnum.Reserved_for_future_use;
					}
					return FlowControlModeEnum.InvalidFlowControlModeValue;
				}
			}

			public static uint FlowControlModeToValue(FlowControlModeEnum enumItem)
			{
				return enumItem switch
				{
					FlowControlModeEnum.Packet_based_data_flow_control_mode_default_for_a_BR_EDR_Controller => 0u, 
					FlowControlModeEnum.Data_block_based_data_flow_control_mode_default_for_an_AMP_Controller => 1u, 
					FlowControlModeEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string FlowControlModeToString(FlowControlModeEnum enumItem)
			{
				return enumItem switch
				{
					FlowControlModeEnum.Packet_based_data_flow_control_mode_default_for_a_BR_EDR_Controller => "Packet based data flow control mode (default for a BR/EDR Controller)", 
					FlowControlModeEnum.Data_block_based_data_flow_control_mode_default_for_an_AMP_Controller => "Data block based data flow control mode (default for an AMP Controller)", 
					FlowControlModeEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid FlowControlMode Value", 
				};
			}

			public static string FlowControlModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Packet based data flow control mode (default for a BR/EDR Controller)");
				stringBuilder.AppendLine("0x1 = Data block based data flow control mode (default for an AMP Controller)");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _FlowControlMode;

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

		public FlowControlModeEnum GetFlowControlModeAsFlowControlModeEnum()
		{
			return FlowControlModeInfo.ValueToFlowControlMode(_FlowControlMode);
		}

		public void SetFlowControlModeAsFlowControlModeEnum(FlowControlModeEnum enumItem)
		{
			FlowControlMode = FlowControlModeInfo.FlowControlModeToValue(enumItem);
		}
	}

	private const uint _Opcode = 3175u;

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

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

	public uint Opcode => 3175u;

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