using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class SetMwsSignaling : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _MwsRxAssertOffset;

		private uint _MwsRxAssertJitter;

		private uint _MwsRxDeassertOffset;

		private uint _MwsRxDeassertJitter;

		private uint _MwsTxAssertOffset;

		private uint _MwsTxAssertJitter;

		private uint _MwsTxDeassertOffset;

		private uint _MwsTxDeassertJitter;

		private uint _MwsPatternAssertOffset;

		private uint _MwsPatternAssertJitter;

		private uint _MwsInactivityDurationAssertOffset;

		private uint _MwsInactivityDurationAssertJitter;

		private uint _MwsScanFrequencyAssertOffset;

		private uint _MwsScanFrequencyAssertJitter;

		private uint _MwsPriorityAssertOffsetRequest;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	private const uint _Opcode = 3184u;

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

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

	public uint Opcode => 3184u;

	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.MwsRxAssertOffset = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsRxAssertJitter = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsRxDeassertOffset = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsRxDeassertJitter = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsTxAssertOffset = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsTxAssertJitter = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsTxDeassertOffset = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsTxDeassertJitter = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsPatternAssertOffset = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsPatternAssertJitter = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsInactivityDurationAssertOffset = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsInactivityDurationAssertJitter = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsScanFrequencyAssertOffset = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsScanFrequencyAssertJitter = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.MwsPriorityAssertOffsetRequest = 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.MwsRxAssertOffset, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsRxAssertJitter, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsRxDeassertOffset, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsRxDeassertJitter, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsTxAssertOffset, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsTxAssertJitter, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsTxDeassertOffset, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsTxDeassertJitter, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsPatternAssertOffset, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsPatternAssertJitter, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsInactivityDurationAssertOffset, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsInactivityDurationAssertJitter, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsScanFrequencyAssertOffset, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsScanFrequencyAssertJitter, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.MwsPriorityAssertOffsetRequest, 16);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
