using System;
using QCRPG.Framework;

namespace QCRPG.TSL.FM.Packets;

public class ReadI2cRegister : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _RegisterAddress;

		private uint _Length;

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

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

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

	private PacketInfoBase info;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		UserFields.RegisterAddress = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Length = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(UserFields.RegisterAddress, 8);
		bitBuilder.AddBitsAsUint(UserFields.Length, 8);
		return bitBuilder.ToString();
	}
}
