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

namespace QCRPG.TSL.BT.Packets;

public class SetParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _TestMode;

		private uint _PacketType;

		private uint _DataLength;

		private uint _HopMode;

		private uint _TxFrequency;

		private uint _TxPower;

		private uint _RxFrequency;

		private ulong _BluetoothDeviceAddress;

		private uint _SkipRxSlot;

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

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

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

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

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

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

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

		public ulong BluetoothDeviceAddress
		{
			get
			{
				return _BluetoothDeviceAddress;
			}
			set
			{
				_BluetoothDeviceAddress = value;
			}
		}

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

		public Ar3002TestModeEnum GetTestModeAsAr3002TestModeEnum()
		{
			return Ar3002TestModeInfo.ValueToAr3002TestMode(_TestMode);
		}

		public void SetTestModeAsAr3002TestModeEnum(Ar3002TestModeEnum enumItem)
		{
			TestMode = Ar3002TestModeInfo.Ar3002TestModeToValue(enumItem);
		}

		public Ar3002PacketTypeEnum GetPacketTypeAsAr3002PacketTypeEnum()
		{
			return Ar3002PacketTypeInfo.ValueToAr3002PacketType(_PacketType);
		}

		public void SetPacketTypeAsAr3002PacketTypeEnum(Ar3002PacketTypeEnum enumItem)
		{
			PacketType = Ar3002PacketTypeInfo.Ar3002PacketTypeToValue(enumItem);
		}
	}

	private const uint _Opcode = 64606u;

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

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

	public uint Opcode => 64606u;

	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.TestMode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PacketType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.DataLength = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.HopMode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TxFrequency = bitBuilder.RemoveBitsAsUint(8);
		UserFields.TxPower = bitBuilder.RemoveBitsAsUint(8);
		UserFields.RxFrequency = bitBuilder.RemoveBitsAsUint(8);
		UserFields.BluetoothDeviceAddress = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.SkipRxSlot = 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.TestMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.PacketType, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.DataLength, 16);
		bitBuilder.AddBitsAsUint(UserFields.HopMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.TxFrequency, 8);
		bitBuilder.AddBitsAsUint(UserFields.TxPower, 8);
		bitBuilder.AddBitsAsUint(UserFields.RxFrequency, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.BluetoothDeviceAddress, 48);
		bitBuilder.AddBitsAsUint(UserFields.SkipRxSlot, 8);
		_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.TestMode > 11)
		{
			list.Add("Maximum allowed value for Test Mode is 0xB");
		}
		if (UserFields.PacketType > 13)
		{
			list.Add("Maximum allowed value for Packet Type is 0xD");
		}
		return list;
	}
}
