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

namespace QCRPG.TSL.BT.Packets;

public class LeGenerateDhkeyV2 : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum KeyTypeEnum
		{
			GENERATED_PRIVATE_KEY,
			DEBUG_PRIVATE_KEY,
			InvalidKeyTypeValue
		}

		public static class KeyTypeInfo
		{
			public static KeyTypeEnum ValueToKeyType(uint value)
			{
				return value switch
				{
					0u => KeyTypeEnum.GENERATED_PRIVATE_KEY, 
					1u => KeyTypeEnum.DEBUG_PRIVATE_KEY, 
					_ => KeyTypeEnum.InvalidKeyTypeValue, 
				};
			}

			public static uint KeyTypeToValue(KeyTypeEnum enumItem)
			{
				return enumItem switch
				{
					KeyTypeEnum.GENERATED_PRIVATE_KEY => 0u, 
					KeyTypeEnum.DEBUG_PRIVATE_KEY => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string KeyTypeToString(KeyTypeEnum enumItem)
			{
				return enumItem switch
				{
					KeyTypeEnum.GENERATED_PRIVATE_KEY => "GENERATED_PRIVATE_KEY", 
					KeyTypeEnum.DEBUG_PRIVATE_KEY => "DEBUG_PRIVATE_KEY", 
					_ => "Invalid KeyType Value", 
				};
			}

			public static string KeyTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = GENERATED_PRIVATE_KEY");
				stringBuilder.AppendLine("0x1 = DEBUG_PRIVATE_KEY");
				return stringBuilder.ToString();
			}
		}

		private List<uint> _RemoteP256PublicKeyList = new List<uint>(new uint[64]);

		private uint _KeyType;

		public List<uint> RemoteP256PublicKeyList => _RemoteP256PublicKeyList;

		public uint KeyType
		{
			get
			{
				return _KeyType;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for KeyType is 0xFF");
				}
				_KeyType = value;
			}
		}

		public KeyTypeEnum GetKeyTypeAsKeyTypeEnum()
		{
			return KeyTypeInfo.ValueToKeyType(_KeyType);
		}

		public void SetKeyTypeAsKeyTypeEnum(KeyTypeEnum enumItem)
		{
			KeyType = KeyTypeInfo.KeyTypeToValue(enumItem);
		}
	}

	private const uint _Opcode = 8286u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForLeGenerateDhkeyV2),
		typeof(LeGenerateDhkeyComplete)
	};

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

	public uint Opcode => 8286u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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