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

namespace QCRPG.TSL.BT.Packets;

public class AcceptSynchronousConnectionRequest : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum TransmitBandwidthEnum
		{
			MAX_POSSIBLE_TX_BANDWIDTH_IN_OCTETS_PER_SECOND,
			DONT_CARE,
			InvalidTransmitBandwidthValue
		}

		public static class TransmitBandwidthInfo
		{
			public static TransmitBandwidthEnum ValueToTransmitBandwidth(uint value)
			{
				if (value >= 0 && value <= 4294967294u)
				{
					return TransmitBandwidthEnum.MAX_POSSIBLE_TX_BANDWIDTH_IN_OCTETS_PER_SECOND;
				}
				if (value == uint.MaxValue)
				{
					return TransmitBandwidthEnum.DONT_CARE;
				}
				return TransmitBandwidthEnum.InvalidTransmitBandwidthValue;
			}

			public static uint TransmitBandwidthToValue(TransmitBandwidthEnum enumItem)
			{
				return enumItem switch
				{
					TransmitBandwidthEnum.MAX_POSSIBLE_TX_BANDWIDTH_IN_OCTETS_PER_SECOND => 0u, 
					TransmitBandwidthEnum.DONT_CARE => uint.MaxValue, 
					_ => uint.MaxValue, 
				};
			}

			public static string TransmitBandwidthToString(TransmitBandwidthEnum enumItem)
			{
				return enumItem switch
				{
					TransmitBandwidthEnum.MAX_POSSIBLE_TX_BANDWIDTH_IN_OCTETS_PER_SECOND => "Maximum possible transmit bandwidth in octets per second", 
					TransmitBandwidthEnum.DONT_CARE => "Don't Care.", 
					_ => "Invalid TransmitBandwidth Value", 
				};
			}

			public static string TransmitBandwidthToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0-0xFFFFFFFE = Maximum possible transmit bandwidth in octets per second");
				stringBuilder.AppendLine("0xFFFFFFFF = Don't Care.");
				return stringBuilder.ToString();
			}
		}

		public enum ReceiveBandwidthEnum
		{
			MAX_POSSIBLE_RX_BANDWIDTH_IN_OCTETS_PER_SECOND,
			DONT_CARE,
			InvalidReceiveBandwidthValue
		}

		public static class ReceiveBandwidthInfo
		{
			public static ReceiveBandwidthEnum ValueToReceiveBandwidth(uint value)
			{
				if (value >= 0 && value <= 4294967294u)
				{
					return ReceiveBandwidthEnum.MAX_POSSIBLE_RX_BANDWIDTH_IN_OCTETS_PER_SECOND;
				}
				if (value == uint.MaxValue)
				{
					return ReceiveBandwidthEnum.DONT_CARE;
				}
				return ReceiveBandwidthEnum.InvalidReceiveBandwidthValue;
			}

			public static uint ReceiveBandwidthToValue(ReceiveBandwidthEnum enumItem)
			{
				return enumItem switch
				{
					ReceiveBandwidthEnum.MAX_POSSIBLE_RX_BANDWIDTH_IN_OCTETS_PER_SECOND => 0u, 
					ReceiveBandwidthEnum.DONT_CARE => uint.MaxValue, 
					_ => uint.MaxValue, 
				};
			}

			public static string ReceiveBandwidthToString(ReceiveBandwidthEnum enumItem)
			{
				return enumItem switch
				{
					ReceiveBandwidthEnum.MAX_POSSIBLE_RX_BANDWIDTH_IN_OCTETS_PER_SECOND => "Maximum possible Recieve bandwidth in octets per second", 
					ReceiveBandwidthEnum.DONT_CARE => "Don't Care", 
					_ => "Invalid ReceiveBandwidth Value", 
				};
			}

			public static string ReceiveBandwidthToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0-0xFFFFFFFE = Maximum possible Recieve bandwidth in octets per second");
				stringBuilder.AppendLine("0xFFFFFFFF = Don't Care");
				return stringBuilder.ToString();
			}
		}

		public enum RetransmissionEffortEnum
		{
			NO_RETRANSMISSIONS,
			AT_LEAST_ONE_RETRANSMISSION_OPTIMIZE_FOR_POWER_CONSUMPTION,
			AT_LEAST_ONE_RETRANSMISSION_OPTIMIZE_FOR_LINK_QUALITY,
			Reserved,
			DONT_CARE,
			InvalidRetransmissionEffortValue
		}

		public static class RetransmissionEffortInfo
		{
			public static RetransmissionEffortEnum ValueToRetransmissionEffort(uint value)
			{
				switch (value)
				{
				case 0u:
					return RetransmissionEffortEnum.NO_RETRANSMISSIONS;
				case 1u:
					return RetransmissionEffortEnum.AT_LEAST_ONE_RETRANSMISSION_OPTIMIZE_FOR_POWER_CONSUMPTION;
				case 2u:
					return RetransmissionEffortEnum.AT_LEAST_ONE_RETRANSMISSION_OPTIMIZE_FOR_LINK_QUALITY;
				default:
					if (value >= 3 && value <= 254)
					{
						return RetransmissionEffortEnum.Reserved;
					}
					if (value == 255)
					{
						return RetransmissionEffortEnum.DONT_CARE;
					}
					return RetransmissionEffortEnum.InvalidRetransmissionEffortValue;
				}
			}

			public static uint RetransmissionEffortToValue(RetransmissionEffortEnum enumItem)
			{
				return enumItem switch
				{
					RetransmissionEffortEnum.NO_RETRANSMISSIONS => 0u, 
					RetransmissionEffortEnum.AT_LEAST_ONE_RETRANSMISSION_OPTIMIZE_FOR_POWER_CONSUMPTION => 1u, 
					RetransmissionEffortEnum.AT_LEAST_ONE_RETRANSMISSION_OPTIMIZE_FOR_LINK_QUALITY => 2u, 
					RetransmissionEffortEnum.Reserved => 3u, 
					RetransmissionEffortEnum.DONT_CARE => 255u, 
					_ => uint.MaxValue, 
				};
			}

			public static string RetransmissionEffortToString(RetransmissionEffortEnum enumItem)
			{
				return enumItem switch
				{
					RetransmissionEffortEnum.NO_RETRANSMISSIONS => "No Retransmissions.", 
					RetransmissionEffortEnum.AT_LEAST_ONE_RETRANSMISSION_OPTIMIZE_FOR_POWER_CONSUMPTION => "At least one retransmission, optimize for power consumption", 
					RetransmissionEffortEnum.AT_LEAST_ONE_RETRANSMISSION_OPTIMIZE_FOR_LINK_QUALITY => "At least one retransmission, optimize for link quality", 
					RetransmissionEffortEnum.Reserved => "Reserved", 
					RetransmissionEffortEnum.DONT_CARE => "Don't Care", 
					_ => "Invalid RetransmissionEffort Value", 
				};
			}

			public static string RetransmissionEffortToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No Retransmissions.");
				stringBuilder.AppendLine("0x1 = At least one retransmission, optimize for power consumption");
				stringBuilder.AppendLine("0x2 = At least one retransmission, optimize for link quality");
				stringBuilder.AppendLine("0x3-0xFE = Reserved");
				stringBuilder.AppendLine("0xFF = Don't Care");
				return stringBuilder.ToString();
			}
		}

		private ulong _BdAddr;

		private uint _TransmitBandwidth;

		private uint _ReceiveBandwidth;

		private uint _MaxLatency;

		private uint _VoiceSetting;

		private uint _RetransmissionEffort;

		private uint _PacketType;

		public ulong BdAddr
		{
			get
			{
				return _BdAddr;
			}
			set
			{
				_BdAddr = 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 TransmitBandwidthEnum GetTransmitBandwidthAsTransmitBandwidthEnum()
		{
			return TransmitBandwidthInfo.ValueToTransmitBandwidth(_TransmitBandwidth);
		}

		public void SetTransmitBandwidthAsTransmitBandwidthEnum(TransmitBandwidthEnum enumItem)
		{
			TransmitBandwidth = TransmitBandwidthInfo.TransmitBandwidthToValue(enumItem);
		}

		public ReceiveBandwidthEnum GetReceiveBandwidthAsReceiveBandwidthEnum()
		{
			return ReceiveBandwidthInfo.ValueToReceiveBandwidth(_ReceiveBandwidth);
		}

		public void SetReceiveBandwidthAsReceiveBandwidthEnum(ReceiveBandwidthEnum enumItem)
		{
			ReceiveBandwidth = ReceiveBandwidthInfo.ReceiveBandwidthToValue(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 = 1065u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForAcceptSynchronousConnectionRequest),
		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 AcceptSynchronousConnectionRequestInfo(this);
			}
			return info;
		}
	}

	public uint Opcode => 1065u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.TransmitBandwidth = uint.MaxValue;
		UserFields.ReceiveBandwidth = uint.MaxValue;
		UserFields.MaxLatency = 65535u;
		UserFields.RetransmissionEffort = 255u;
		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.BdAddr = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		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.AddBitsAsUlongLittleEndian(UserFields.BdAddr, 48);
		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();
	}
}
