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

namespace QCRPG.TSL.BT.Packets;

public class ReturnLinkKeysEvent : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _NumKeys = 1u;

		private List<ParametersSubField> _ParametersList = new List<ParametersSubField>();

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

		public List<ParametersSubField> ParametersList => _ParametersList;
	}

	public class ParametersSubField
	{
		private ulong _BdAddress;

		private List<uint> _LinkKeyList = new List<uint>(new uint[16]);

		public ulong BdAddress
		{
			get
			{
				return _BdAddress;
			}
			set
			{
				_BdAddress = value;
			}
		}

		public List<uint> LinkKeyList => _LinkKeyList;

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			BdAddress = bitBuilder.RemoveBitsAsUlong(48);
			for (int i = 0; i < LinkKeyList.Count; i++)
			{
				LinkKeyList[i] = bitBuilder.RemoveBitsAsUint(8);
			}
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUlong(BdAddress, 48);
			for (int i = 0; i < LinkKeyList.Count; i++)
			{
				if (LinkKeyList[i] > 255)
				{
					throw new Exception("LinkKeyList[i] is > 0xFF");
				}
				bitBuilder.AddBitsAsUint(LinkKeyList[i], 8);
			}
			return bitBuilder.ToString();
		}
	}

	private const uint _EventCode = 21u;

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

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new ReturnLinkKeysEventInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 21u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != EventCode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		UserFields.NumKeys = bitBuilder.RemoveBitsAsUint(8);
		for (int num = UserFields.ParametersList.Count - 1; num >= 0; num--)
		{
			UserFields.ParametersList[num].FromBitBuilder(ref bitBuilder);
		}
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(EventCode, 8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumKeys, 8);
		for (int num = UserFields.ParametersList.Count - 1; num >= 0; num--)
		{
			bitBuilder.AddBitsAsString(UserFields.ParametersList[num].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.NumKeys < 1)
		{
			list.Add("Minimum allowed value for Num Keys is 0x1");
		}
		if (UserFields.NumKeys > 11)
		{
			list.Add("Maximum allowed value for Num Keys is 0xB");
		}
		return list;
	}
}
