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

namespace QCRPG.TSL.NFC.Packets;

public class CoreGetConfigCmd : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _NumberOfParameters;

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

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

		public List<uint> ParameterIdList => _ParameterIdList;

		public ConfigurationParameterTagsEnum GetParameterIdAsConfigurationParameterTagsEnum(int index)
		{
			return ConfigurationParameterTagsInfo.ValueToConfigurationParameterTags(_ParameterIdList[index]);
		}

		public void SetParameterIdAsConfigurationParameterTagsEnum(int index, ConfigurationParameterTagsEnum enumItem)
		{
			_ParameterIdList[index] = ConfigurationParameterTagsInfo.ConfigurationParameterTagsToValue(enumItem);
		}
	}

	private const uint _Mt = 1u;

	private const uint _Gid = 0u;

	private const uint _Oid = 3u;

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

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

	public uint Mt => 1u;

	public uint Pbf => _Pbf;

	public uint Gid => 0u;

	public uint Rfu => _Rfu;

	public uint Oid => 3u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	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;
		}
		UserFields.NumberOfParameters = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.ParameterIdList.Count; i++)
		{
			UserFields.ParameterIdList[i] = 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.NumberOfParameters, 8);
		for (int i = 0; i < UserFields.ParameterIdList.Count; i++)
		{
			if (UserFields.ParameterIdList[i] > 255)
			{
				throw new Exception("Maximum possible value for ParameterId is 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.ParameterIdList[i], 8);
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
