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

namespace QCRPG.TSL.NFC.Packets;

public class MemoryAccessPokeCmd : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _AccessType = 1u;

		private const uint _Rfu2 = 0u;

		private uint _ItemSize;

		private uint _StartAddress;

		private uint _NumberOfItems;

		private uint _AddressDelta;

		private uint _AccessDelay;

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

		public uint AccessType => 1u;

		public uint Rfu2 => 0u;

		public uint ItemSize
		{
			get
			{
				return _ItemSize;
			}
			set
			{
				if (value > 3)
				{
					throw new Exception("Maximum possible value for ItemSize is 0x3");
				}
				_ItemSize = value;
				int num = 0;
				if (ItemSize == 0)
				{
					num = 1;
				}
				else if (ItemSize == 1)
				{
					num = 2;
				}
				else if (ItemSize == 2)
				{
					num = 4;
				}
				foreach (List<uint> dataListOfList in DataListOfLists)
				{
					while (dataListOfList.Count < num)
					{
						dataListOfList.Add(0u);
					}
					while (dataListOfList.Count > num)
					{
						dataListOfList.RemoveAt(dataListOfList.Count - 1);
					}
				}
			}
		}

		public uint StartAddress
		{
			get
			{
				return _StartAddress;
			}
			set
			{
				_StartAddress = value;
			}
		}

		public uint NumberOfItems
		{
			get
			{
				return _NumberOfItems;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for NumberOfItems is 0xFF");
				}
				_NumberOfItems = value;
				while (DataListOfLists.Count < _NumberOfItems)
				{
					DataListOfLists.Add(new List<uint>());
				}
				while (DataListOfLists.Count > _NumberOfItems)
				{
					DataListOfLists.RemoveAt(_DataListOfLists.Count - 1);
				}
				ItemSize = _ItemSize;
			}
		}

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

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

		public List<List<uint>> DataListOfLists => _DataListOfLists;

		public MemoryAccessItemSizeEnum GetItemSizeAsMemoryAccessItemSizeEnum()
		{
			return MemoryAccessItemSizeInfo.ValueToMemoryAccessItemSize(_ItemSize);
		}

		public void SetItemSizeAsMemoryAccessItemSizeEnum(MemoryAccessItemSizeEnum enumItem)
		{
			ItemSize = MemoryAccessItemSizeInfo.MemoryAccessItemSizeToValue(enumItem);
		}
	}

	private const uint _Mt = 1u;

	private const uint _Gid = 15u;

	private const uint _Oid = 0u;

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

	private PacketInfoBase info;

	private uint _Pbf;

	private uint _Rfu;

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

	public uint Mt => 1u;

	public uint Pbf => _Pbf;

	public uint Gid => 15u;

	public uint Rfu => _Rfu;

	public uint Oid => 0u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ItemSize = 0u;
		UserFields.StartAddress = 507904u;
		UserFields.NumberOfItems = 1u;
		UserFields.AddressDelta = 1u;
		UserFields.AccessDelay = 16u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(3) != Mt)
		{
			return false;
		}
		_Pbf = bitBuilder.RemoveBitsAsUint(1);
		if (bitBuilder.RemoveBitsAsUint(4) != Gid)
		{
			return false;
		}
		_Rfu = bitBuilder.RemoveBitsAsUint(2);
		if (bitBuilder.RemoveBitsAsUint(6) != Oid)
		{
			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(1) != UserFields.AccessType)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(5) != UserFields.Rfu2)
		{
			return false;
		}
		UserFields.ItemSize = bitBuilder.RemoveBitsAsUint(2);
		UserFields.StartAddress = bitBuilder.RemoveBitsAsUintLittleEndian(32);
		UserFields.NumberOfItems = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AddressDelta = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AccessDelay = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		for (int i = 0; i < UserFields.DataListOfLists.Count; i++)
		{
			for (int num = UserFields.DataListOfLists[i].Count - 1; num >= 0; num--)
			{
				UserFields.DataListOfLists[i][num] = bitBuilder.RemoveBitsAsUint(8);
			}
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(Mt, 3);
		bitBuilder.AddBitsAsUint(Pbf, 1);
		bitBuilder.AddBitsAsUint(Gid, 4);
		bitBuilder.AddBitsAsUint(Rfu, 2);
		bitBuilder.AddBitsAsUint(Oid, 6);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.AccessType, 1);
		bitBuilder.AddBitsAsUint(UserFields.Rfu2, 5);
		bitBuilder.AddBitsAsUint(UserFields.ItemSize, 2);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.StartAddress, 32);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfItems, 8);
		bitBuilder.AddBitsAsUint(UserFields.AddressDelta, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.AccessDelay, 16);
		for (int i = 0; i < UserFields.DataListOfLists.Count; i++)
		{
			for (int num = UserFields.DataListOfLists[i].Count - 1; num >= 0; num--)
			{
				if (UserFields.DataListOfLists[i][num] > 255)
				{
					throw new Exception("UserFields.DataListOfLists[j][i] is > 0xFF");
				}
				bitBuilder.AddBitsAsUint(UserFields.DataListOfLists[i][num], 8);
			}
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
