using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class LeSetDefaultPhy : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _AllPhys;

		private uint _TxPhys;

		private uint _RxPhys;

		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 PhyPreferenceUseEnumCollection GetAllPhysAsPhyPreferenceUseEnumCollection()
		{
			return PhyPreferenceUseInfo.ValueToPhyPreferenceUse(_AllPhys);
		}

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

		public TransmitterPhyPreferenceEnumCollection GetTxPhysAsTransmitterPhyPreferenceEnumCollection()
		{
			return TransmitterPhyPreferenceInfo.ValueToTransmitterPhyPreference(_TxPhys);
		}

		public void SetTxPhysAsTransmitterPhyPreferenceEnumCollection(TransmitterPhyPreferenceEnumCollection enumCollection)
		{
			TxPhys = TransmitterPhyPreferenceInfo.TransmitterPhyPreferenceToValue(enumCollection);
		}

		public ReceiverPhyPreferenceEnumCollection GetRxPhysAsReceiverPhyPreferenceEnumCollection()
		{
			return ReceiverPhyPreferenceInfo.ValueToReceiverPhyPreference(_RxPhys);
		}

		public void SetRxPhysAsReceiverPhyPreferenceEnumCollection(ReceiverPhyPreferenceEnumCollection enumCollection)
		{
			RxPhys = ReceiverPhyPreferenceInfo.ReceiverPhyPreferenceToValue(enumCollection);
		}
	}

	private const uint _Opcode = 8241u;

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

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

	public uint Opcode => 8241u;

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