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

namespace QCRPG.TSL.ZigBee.Packets;

public class VsPokeRequest : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _Elementsize;

		private uint _Numelements;

		private uint _Address;

		private uint _Datalength;

		private List<uint> _DataList = new List<uint>();

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

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

		public uint Address
		{
			get
			{
				return _Address;
			}
			set
			{
				_Address = value;
			}
		}

		public uint Datalength
		{
			get
			{
				return _Datalength;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for Datalength is 0xFF");
				}
				_Datalength = value;
				while (_DataList.Count < _Datalength)
				{
					_DataList.Add(0u);
				}
				while (_DataList.Count > _Datalength)
				{
					_DataList.RemoveAt(_DataList.Count - 1);
				}
			}
		}

		public List<uint> DataList => _DataList;
	}

	private const uint _Packetcode = 135u;

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

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

	public uint Packetcode => 135u;

	public UserFieldsWrapper UserFields => _UserFields;

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(8) != Packetcode)
		{
			return false;
		}
		UserFields.Elementsize = bitBuilder.RemoveBitsAsUintLittleEndian(8);
		UserFields.Numelements = bitBuilder.RemoveBitsAsUintLittleEndian(8);
		UserFields.Address = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.Datalength = (uint)(bitBuilder.GetNumberOfBits() / 8);
		for (int num = UserFields.DataList.Count - 1; num >= 0; num--)
		{
			UserFields.DataList[num] = bitBuilder.RemoveBitsAsUint(8);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Packetcode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Elementsize, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Numelements, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Address, 32);
		for (int num = UserFields.DataList.Count - 1; num >= 0; num--)
		{
			if (UserFields.DataList[num] > 255)
			{
				throw new Exception("UserFields.DataList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.DataList[num], 8);
		}
		return bitBuilder.ToString();
	}
}
