using System;
using System.Collections.Generic;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class QbceQleSetPhy : PacketBase
{
	public class UserFieldsWrapper
	{
		public class PhyOptionsEnumCollection
		{
			public enum LeCodedPhyCodingPreferenceEnum
			{
				The_Host_has_no_preferred_coding_when_transmitting_on_the_LE_Coded_PHY,
				The_Host_prefers_that_S_equals_2_coding_be_used_when_transmitting_on_the_LE_Coded_PHY,
				The_Host_prefers_that_S_equals_8_coding_be_used_when_transmitting_on_the_LE_Coded_PHY,
				Reserved_for_future_use,
				InvalidLeCodedPhyCodingPreferenceValue
			}

			public static class LeCodedPhyCodingPreferenceInfo
			{
				public static LeCodedPhyCodingPreferenceEnum ValueToLeCodedPhyCodingPreference(uint value)
				{
					return value switch
					{
						0u => LeCodedPhyCodingPreferenceEnum.The_Host_has_no_preferred_coding_when_transmitting_on_the_LE_Coded_PHY, 
						1u => LeCodedPhyCodingPreferenceEnum.The_Host_prefers_that_S_equals_2_coding_be_used_when_transmitting_on_the_LE_Coded_PHY, 
						2u => LeCodedPhyCodingPreferenceEnum.The_Host_prefers_that_S_equals_8_coding_be_used_when_transmitting_on_the_LE_Coded_PHY, 
						3u => LeCodedPhyCodingPreferenceEnum.Reserved_for_future_use, 
						_ => LeCodedPhyCodingPreferenceEnum.InvalidLeCodedPhyCodingPreferenceValue, 
					};
				}

				public static uint LeCodedPhyCodingPreferenceToValue(LeCodedPhyCodingPreferenceEnum enumItem)
				{
					return enumItem switch
					{
						LeCodedPhyCodingPreferenceEnum.The_Host_has_no_preferred_coding_when_transmitting_on_the_LE_Coded_PHY => 0u, 
						LeCodedPhyCodingPreferenceEnum.The_Host_prefers_that_S_equals_2_coding_be_used_when_transmitting_on_the_LE_Coded_PHY => 1u, 
						LeCodedPhyCodingPreferenceEnum.The_Host_prefers_that_S_equals_8_coding_be_used_when_transmitting_on_the_LE_Coded_PHY => 2u, 
						LeCodedPhyCodingPreferenceEnum.Reserved_for_future_use => 3u, 
						_ => uint.MaxValue, 
					};
				}

				public static string LeCodedPhyCodingPreferenceToString(LeCodedPhyCodingPreferenceEnum enumItem)
				{
					return enumItem switch
					{
						LeCodedPhyCodingPreferenceEnum.The_Host_has_no_preferred_coding_when_transmitting_on_the_LE_Coded_PHY => "The Host has no preferred coding on LE Coded PHY", 
						LeCodedPhyCodingPreferenceEnum.The_Host_prefers_that_S_equals_2_coding_be_used_when_transmitting_on_the_LE_Coded_PHY => "The Host prefers S=2 coding on LE Coded PHY", 
						LeCodedPhyCodingPreferenceEnum.The_Host_prefers_that_S_equals_8_coding_be_used_when_transmitting_on_the_LE_Coded_PHY => "The Host prefers S=8 coding on LE Coded PHY", 
						LeCodedPhyCodingPreferenceEnum.Reserved_for_future_use => "Reserved for future use", 
						_ => "Invalid LeCodedPhyCodingPreference Value", 
					};
				}

				public static string LeCodedPhyCodingPreferenceToDescription()
				{
					StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.AppendLine("0x0 = The Host has no preferred coding on LE Coded PHY");
					stringBuilder.AppendLine("0x1 = The Host prefers S=2 coding on LE Coded PHY");
					stringBuilder.AppendLine("0x2 = The Host prefers S=8 coding on LE Coded PHY");
					stringBuilder.AppendLine("0x3 = Reserved for future use");
					return stringBuilder.ToString();
				}
			}

			private LeCodedPhyCodingPreferenceEnum _LeCodedPhyCodingPreference;

			public LeCodedPhyCodingPreferenceEnum LeCodedPhyCodingPreference
			{
				get
				{
					return _LeCodedPhyCodingPreference;
				}
				set
				{
					_LeCodedPhyCodingPreference = value;
				}
			}
		}

		public static class PhyOptionsInfo
		{
			public static PhyOptionsEnumCollection ValueToPhyOptions(uint value)
			{
				PhyOptionsEnumCollection phyOptionsEnumCollection = new PhyOptionsEnumCollection();
				phyOptionsEnumCollection.LeCodedPhyCodingPreference = PhyOptionsEnumCollection.LeCodedPhyCodingPreferenceInfo.ValueToLeCodedPhyCodingPreference(value & 3u);
				return phyOptionsEnumCollection;
			}

			public static uint PhyOptionsToValue(PhyOptionsEnumCollection enumCollection)
			{
				uint num = 0u;
				return num | PhyOptionsEnumCollection.LeCodedPhyCodingPreferenceInfo.LeCodedPhyCodingPreferenceToValue(enumCollection.LeCodedPhyCodingPreference);
			}

			public static string PhyOptionsToString(PhyOptionsEnumCollection enumCollection)
			{
				string text = "";
				text = text + "LE Coded PHY Coding Preference: " + PhyOptionsEnumCollection.LeCodedPhyCodingPreferenceInfo.LeCodedPhyCodingPreferenceToString(enumCollection.LeCodedPhyCodingPreference) + "\r\n";
				return text.TrimEnd('\r', '\n');
			}

			public static string PhyOptionsToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Bits 1-0: LE Coded PHY Coding Preference");
				stringBuilder.AppendLine(PhyOptionsEnumCollection.LeCodedPhyCodingPreferenceInfo.LeCodedPhyCodingPreferenceToDescription());
				stringBuilder.AppendLine("Bits 15-2: Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private const uint _Subopcode = 3u;

		private uint _ConnectionHandle;

		private uint _AllPhys;

		private uint _TxPhys;

		private uint _RxPhys;

		private uint _PhyOptions;

		public uint Subopcode => 3u;

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

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

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

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

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

		public PhyPreferenceUseEnumCollection GetAllPhysAsPhyPreferenceUseEnumCollection()
		{
			return PhyPreferenceUseInfo.ValueToPhyPreferenceUse(_AllPhys);
		}

		public void SetAllPhysAsPhyPreferenceUseEnumCollection(PhyPreferenceUseEnumCollection enumCollection)
		{
			AllPhys = PhyPreferenceUseInfo.PhyPreferenceUseToValue(enumCollection);
		}

		public QbceTransmitterPhyPreferenceEnumCollection GetTxPhysAsQbceTransmitterPhyPreferenceEnumCollection()
		{
			return QbceTransmitterPhyPreferenceInfo.ValueToQbceTransmitterPhyPreference(_TxPhys);
		}

		public void SetTxPhysAsQbceTransmitterPhyPreferenceEnumCollection(QbceTransmitterPhyPreferenceEnumCollection enumCollection)
		{
			TxPhys = QbceTransmitterPhyPreferenceInfo.QbceTransmitterPhyPreferenceToValue(enumCollection);
		}

		public QbceReceiverPhyPreferenceEnumCollection GetRxPhysAsQbceReceiverPhyPreferenceEnumCollection()
		{
			return QbceReceiverPhyPreferenceInfo.ValueToQbceReceiverPhyPreference(_RxPhys);
		}

		public void SetRxPhysAsQbceReceiverPhyPreferenceEnumCollection(QbceReceiverPhyPreferenceEnumCollection enumCollection)
		{
			RxPhys = QbceReceiverPhyPreferenceInfo.QbceReceiverPhyPreferenceToValue(enumCollection);
		}

		public PhyOptionsEnumCollection GetPhyOptionsAsPhyOptionsEnumCollection()
		{
			return PhyOptionsInfo.ValueToPhyOptions(_PhyOptions);
		}

		public void SetPhyOptionsAsPhyOptionsEnumCollection(PhyOptionsEnumCollection enumCollection)
		{
			PhyOptions = PhyOptionsInfo.PhyOptionsToValue(enumCollection);
		}
	}

	private const uint _Opcode = 64593u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForQbceQleSetPhy),
		typeof(QlePhyUpdateComplete)
	};

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

	public uint Opcode => 64593u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 0u;
	}

	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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.Subopcode)
		{
			return false;
		}
		UserFields.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.AllPhys = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TxPhys = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RxPhys = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PhyOptions = 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.AddBitsAsUint(UserFields.Subopcode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.AllPhys, 8);
		bitBuilder.AddBitsAsUint(UserFields.TxPhys, 8);
		bitBuilder.AddBitsAsUint(UserFields.RxPhys, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.PhyOptions, 16);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.ConnectionHandle < 0)
		{
			list.Add("Minimum allowed value for Connection_Handle is 0x0");
		}
		if (UserFields.ConnectionHandle > 3839)
		{
			list.Add("Maximum allowed value for Connection_Handle is 0xEFF");
		}
		return list;
	}
}
