using System;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class CSRConfigBitErr : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _Reserved = 194u;

		private const uint _Type = 2u;

		private const uint _Length = 9u;

		private const uint _Seqno = 1u;

		private const uint _Varid = 20484u;

		private const uint _Status = 0u;

		private const uint _Testid = 24u;

		private uint _Bitcount;

		private uint _Reset;

		public uint Reserved => 194u;

		public uint Type => 2u;

		public uint Length => 9u;

		public uint Seqno => 1u;

		public uint Varid => 20484u;

		public uint Status => 0u;

		public uint Testid => 24u;

		public uint Bitcount
		{
			get
			{
				return _Bitcount;
			}
			set
			{
				_Bitcount = value;
			}
		}

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

	private const uint _Opcode = 64512u;

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

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

	public uint Opcode => 64512u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.Bitcount = 65536u;
		UserFields.Reset = 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.Reserved)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.Type)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.Length)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.Seqno)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.Varid)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(16) != UserFields.Status)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != UserFields.Testid)
		{
			return false;
		}
		UserFields.Bitcount = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.Reset = 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.Reserved, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Type, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Length, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Seqno, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Varid, 16);
		bitBuilder.AddBitsAsUint(UserFields.Status, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Testid, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Bitcount, 32);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Reset, 16);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
