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

namespace QCRPG.TSL.BT.Packets;

public class HostNumberOfCompletedPackets : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _NumberOfHandles;

		private List<NHandlesSubField> _NHandlesList = new List<NHandlesSubField>();

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

		public List<NHandlesSubField> NHandlesList => _NHandlesList;
	}

	public class NHandlesSubField
	{
		private uint _ConnectionHandle;

		private uint _HostNumOfCompletedPackets;

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

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

		public void SetDefaultValues()
		{
			ConnectionHandle = 0u;
			HostNumOfCompletedPackets = 0u;
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			HostNumOfCompletedPackets = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUintLittleEndian(ConnectionHandle, 16);
			bitBuilder.AddBitsAsUintLittleEndian(HostNumOfCompletedPackets, 16);
			return bitBuilder.ToString();
		}

		public List<string> GetWarnings()
		{
			List<string> list = new List<string>();
			if (ConnectionHandle < 0)
			{
				list.Add("Minimum allowed value for Connection_Handle is 0x0");
			}
			if (ConnectionHandle > 3839)
			{
				list.Add("Maximum allowed value for Connection_Handle is 0xEFF");
			}
			if (HostNumOfCompletedPackets < 0)
			{
				list.Add("Minimum allowed value for Host_Num_Of_Completed_Packets is 0x0");
			}
			if (HostNumOfCompletedPackets > 65535)
			{
				list.Add("Maximum allowed value for Host_Num_Of_Completed_Packets is 0xFFFF");
			}
			return list;
		}
	}

	private const uint _Opcode = 3125u;

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

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

	public uint Opcode => 3125u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.NumberOfHandles = 0u;
		for (int i = 0; i < UserFields.NHandlesList.Count; i++)
		{
			UserFields.NHandlesList[i].SetDefaultValues();
		}
	}

	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;
		}
		UserFields.NumberOfHandles = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.NHandlesList.Count; i++)
		{
			UserFields.NHandlesList[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.NumberOfHandles, 8);
		for (int i = 0; i < UserFields.NHandlesList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.NHandlesList[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();
	}
}
