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

namespace QCRPG.TSL.FMHCI.Packets;

public class MultiPokeWords : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _SubOpcode = 19u;

		private uint _NumberOfWords;

		private List<WordsSubField> _WordsList = new List<WordsSubField>();

		public uint SubOpcode => 19u;

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

		public List<WordsSubField> WordsList => _WordsList;
	}

	public class WordsSubField
	{
		private const uint _DataLength = 4u;

		private uint _Address;

		private uint _Data;

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

		public uint DataLength => 4u;

		public uint Data
		{
			get
			{
				return _Data;
			}
			set
			{
				_Data = value;
			}
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			Address = bitBuilder.RemoveBitsAsUintLittleEndian(32);
			if (bitBuilder.RemoveBitsAsUint(8) != DataLength)
			{
				return false;
			}
			Data = bitBuilder.RemoveBitsAsUintLittleEndian(32);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUintLittleEndian(Address, 32);
			bitBuilder.AddBitsAsUint(DataLength, 8);
			bitBuilder.AddBitsAsUintLittleEndian(Data, 32);
			return bitBuilder.ToString();
		}
	}

	private const uint _Opcode = 64515u;

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

	private PacketInfoBase info;

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

	public uint Opcode => 64515u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUintLittleEndian(16) != Opcode)
		{
			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(8) != UserFields.SubOpcode)
		{
			return false;
		}
		UserFields.NumberOfWords = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.WordsList.Count; i++)
		{
			UserFields.WordsList[i].FromBitBuilder(ref bitBuilder);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUintLittleEndian(Opcode, 16);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.SubOpcode, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfWords, 8);
		for (int i = 0; i < UserFields.WordsList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.WordsList[i].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
