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

namespace QCRPG.TSL.BT.Packets;

public class HciSetupSynchronousConnection : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum MaxLatencyEnum
		{
			MAX_LATENCY,
			InvalidMaxLatencyValue
		}

		public static class MaxLatencyInfo
		{
			public static MaxLatencyEnum ValueToMaxLatency(uint value)
			{
				if (value >= 4 && value <= 65534)
				{
					return MaxLatencyEnum.MAX_LATENCY;
				}
				return MaxLatencyEnum.InvalidMaxLatencyValue;
			}

			public static uint MaxLatencyToValue(MaxLatencyEnum enumItem)
			{
				if (enumItem == MaxLatencyEnum.MAX_LATENCY)
				{
					return 4u;
				}
				return uint.MaxValue;
			}

			public static string MaxLatencyToString(MaxLatencyEnum enumItem)
			{
				if (enumItem == MaxLatencyEnum.MAX_LATENCY)
				{
					return "Receive bandwidth in octets per second";
				}
				return "Invalid MaxLatency Value";
			}

			public static string MaxLatencyToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x4-0xFFFE = Receive bandwidth in octets per second");
				return stringBuilder.ToString();
			}
		}

		public enum RetransmissionEffortEnum
		{
			No_Retransmissions,
			AT_LEAST_1_Retransmission_OPTIMISE_Power_Consumption,
			AT_LEAST_1_Retransmission_OPTIMISE_LINK_QUALITY,
			DONT_CARE,
			InvalidRetransmissionEffortValue
		}

		public static class RetransmissionEffortInfo
		{
			public static RetransmissionEffortEnum ValueToRetransmissionEffort(uint value)
			{
				return value switch
				{
					0u => RetransmissionEffortEnum.No_Retransmissions, 
					1u => RetransmissionEffortEnum.AT_LEAST_1_Retransmission_OPTIMISE_Power_Consumption, 
					2u => RetransmissionEffortEnum.AT_LEAST_1_Retransmission_OPTIMISE_LINK_QUALITY, 
					255u => RetransmissionEffortEnum.DONT_CARE, 
					_ => RetransmissionEffortEnum.InvalidRetransmissionEffortValue, 
				};
			}

			public static uint RetransmissionEffortToValue(RetransmissionEffortEnum enumItem)
			{
				return enumItem switch
				{
					RetransmissionEffortEnum.No_Retransmissions => 0u, 
					RetransmissionEffortEnum.AT_LEAST_1_Retransmission_OPTIMISE_Power_Consumption => 1u, 
					RetransmissionEffortEnum.AT_LEAST_1_Retransmission_OPTIMISE_LINK_QUALITY => 2u, 
					RetransmissionEffortEnum.DONT_CARE => 255u, 
					_ => uint.MaxValue, 
				};
			}

			public static string RetransmissionEffortToString(RetransmissionEffortEnum enumItem)
			{
				return enumItem switch
				{
					RetransmissionEffortEnum.No_Retransmissions => "No_Retransmissions", 
					RetransmissionEffortEnum.AT_LEAST_1_Retransmission_OPTIMISE_Power_Consumption => "AT_LEAST_1_Retransmission_OPTIMISE_Power_Consumption", 
					RetransmissionEffortEnum.AT_LEAST_1_Retransmission_OPTIMISE_LINK_QUALITY => "AT_LEAST_1_Retransmission_OPTIMISE_LINK_QUALITY", 
					RetransmissionEffortEnum.DONT_CARE => "DONT_CARE", 
					_ => "Invalid RetransmissionEffort Value", 
				};
			}

			public static string RetransmissionEffortToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No_Retransmissions");
				stringBuilder.AppendLine("0x1 = AT_LEAST_1_Retransmission_OPTIMISE_Power_Consumption");
				stringBuilder.AppendLine("0x2 = AT_LEAST_1_Retransmission_OPTIMISE_LINK_QUALITY");
				stringBuilder.AppendLine("0xFF = DONT_CARE");
				return stringBuilder.ToString();
			}
		}

		private uint _ConnectionHandle;

		private uint _TransmitBandwidth;

		private uint _ReceiveBandwidth;

		private uint _MaxLatency;

		private uint _VoiceSetting;

		private uint _RetransmissionEffort;

		private uint _PacketType;

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

		public uint TransmitBandwidth
		{
			get
			{
				return _TransmitBandwidth;
			}
			set
			{
				_TransmitBandwidth = value;
			}
		}

		public uint ReceiveBandwidth
		{
			get
			{
				return _ReceiveBandwidth;
			}
			set
			{
				_ReceiveBandwidth = value;
			}
		}

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

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

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

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

		public MaxLatencyEnum GetMaxLatencyAsMaxLatencyEnum()
		{
			return MaxLatencyInfo.ValueToMaxLatency(_MaxLatency);
		}

		public void SetMaxLatencyAsMaxLatencyEnum(MaxLatencyEnum enumItem)
		{
			MaxLatency = MaxLatencyInfo.MaxLatencyToValue(enumItem);
		}

		public RetransmissionEffortEnum GetRetransmissionEffortAsRetransmissionEffortEnum()
		{
			return RetransmissionEffortInfo.ValueToRetransmissionEffort(_RetransmissionEffort);
		}

		public void SetRetransmissionEffortAsRetransmissionEffortEnum(RetransmissionEffortEnum enumItem)
		{
			RetransmissionEffort = RetransmissionEffortInfo.RetransmissionEffortToValue(enumItem);
		}

		public EnhancedSynchronousPacketTypeEnumCollection GetPacketTypeAsEnhancedSynchronousPacketTypeEnumCollection()
		{
			return EnhancedSynchronousPacketTypeInfo.ValueToEnhancedSynchronousPacketType(_PacketType);
		}

		public void SetPacketTypeAsEnhancedSynchronousPacketTypeEnumCollection(EnhancedSynchronousPacketTypeEnumCollection enumCollection)
		{
			PacketType = EnhancedSynchronousPacketTypeInfo.EnhancedSynchronousPacketTypeToValue(enumCollection);
		}
	}

	private const uint _Opcode = 1064u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForHciSetupSynchronousConnection),
		typeof(SynchronousConnectionCompleteEvent)
	};

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

	public uint Opcode => 1064u;

	public uint PayloadLength
	{
		get
		{
			ToBitString();
			return _PayloadLength;
		}
	}

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.TransmitBandwidth = 8000u;
		UserFields.ReceiveBandwidth = 8000u;
		UserFields.MaxLatency = 7u;
		UserFields.VoiceSetting = 588u;
		UserFields.PacketType = 63u;
	}

	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.TransmitBandwidth = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.ReceiveBandwidth = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.MaxLatency = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.VoiceSetting = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.RetransmissionEffort = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PacketType = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		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.AddBitsAsUintLittleEndian(UserFields.TransmitBandwidth, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ReceiveBandwidth, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MaxLatency, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.VoiceSetting, 16);
		bitBuilder.AddBitsAsUint(UserFields.RetransmissionEffort, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.PacketType, 16);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
