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

namespace QCRPG.TSL.FM.Packets;

public class MultiplePeek : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _RegisterAddress = 31u;

		private const uint _MemoryControl = 0u;

		private const uint _MemoryAccess = 1u;

		private const uint _ReadStatus = 0u;

		private const uint _MemoryMode = 1u;

		private uint _Length;

		private List<PeekInfoSubField> _PeekInfoList = new List<PeekInfoSubField>();

		public uint RegisterAddress => 31u;

		public uint MemoryControl => 0u;

		public uint MemoryAccess => 1u;

		public uint ReadStatus => 0u;

		public uint Length
		{
			get
			{
				return _Length;
			}
			set
			{
				if (value > 15)
				{
					throw new Exception("Maximum possible value for Length is 0xF");
				}
				_Length = value;
				while (_PeekInfoList.Count < _Length)
				{
					_PeekInfoList.Add(new PeekInfoSubField());
				}
				while (_PeekInfoList.Count > _Length)
				{
					_PeekInfoList.RemoveAt(_PeekInfoList.Count - 1);
				}
			}
		}

		public uint MemoryMode => 1u;

		public List<PeekInfoSubField> PeekInfoList => _PeekInfoList;

		public RegisterMapEnum GetRegisterAddressAsRegisterMapEnum()
		{
			return RegisterMapInfo.ValueToRegisterMap(31u);
		}
	}

	public class PeekInfoSubField
	{
		private uint _Address;

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

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			Address = bitBuilder.RemoveBitsAsUint(16);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(Address, 16);
			return bitBuilder.ToString();
		}
	}

	private Type[] expectedResponseTypes = new Type[0];

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.RegisterAddress)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(1) != UserFields.MemoryControl)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(1) != UserFields.MemoryAccess)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(1) != UserFields.ReadStatus)
		{
			return false;
		}
		UserFields.Length = bitBuilder.RemoveBitsAsUint(4);
		if (bitBuilder.RemoveBitsAsUint(1) != UserFields.MemoryMode)
		{
			return false;
		}
		for (int i = 0; i < UserFields.PeekInfoList.Count; i++)
		{
			UserFields.PeekInfoList[i].FromBitBuilder(ref bitBuilder);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(UserFields.RegisterAddress, 8);
		bitBuilder.AddBitsAsUint(UserFields.MemoryControl, 1);
		bitBuilder.AddBitsAsUint(UserFields.MemoryAccess, 1);
		bitBuilder.AddBitsAsUint(UserFields.ReadStatus, 1);
		bitBuilder.AddBitsAsUint(UserFields.Length, 4);
		bitBuilder.AddBitsAsUint(UserFields.MemoryMode, 1);
		for (int i = 0; i < UserFields.PeekInfoList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.PeekInfoList[i].ToBitString());
		}
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.Length > 8)
		{
			list.Add("Maximum allowed value for Length is 0x8");
		}
		return list;
	}
}
