using System;
using System.Text;
using QCRPG.Framework;

namespace QCRPG.TSL.BT.Packets;

public class MasterLinkKey : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum KeyFlagEnum
		{
			USE_SEMI_PERMANENT_LINK_KEYS,
			USE_TEMPORARY_LINK_KEY,
			InvalidKeyFlagValue
		}

		public static class KeyFlagInfo
		{
			public static KeyFlagEnum ValueToKeyFlag(uint value)
			{
				return value switch
				{
					0u => KeyFlagEnum.USE_SEMI_PERMANENT_LINK_KEYS, 
					1u => KeyFlagEnum.USE_TEMPORARY_LINK_KEY, 
					_ => KeyFlagEnum.InvalidKeyFlagValue, 
				};
			}

			public static uint KeyFlagToValue(KeyFlagEnum enumItem)
			{
				return enumItem switch
				{
					KeyFlagEnum.USE_SEMI_PERMANENT_LINK_KEYS => 0u, 
					KeyFlagEnum.USE_TEMPORARY_LINK_KEY => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string KeyFlagToString(KeyFlagEnum enumItem)
			{
				return enumItem switch
				{
					KeyFlagEnum.USE_SEMI_PERMANENT_LINK_KEYS => "USE_SEMI_PERMANENT_LINK_KEYS", 
					KeyFlagEnum.USE_TEMPORARY_LINK_KEY => "USE_TEMPORARY_LINK_KEY", 
					_ => "Invalid KeyFlag Value", 
				};
			}

			public static string KeyFlagToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = USE_SEMI_PERMANENT_LINK_KEYS");
				stringBuilder.AppendLine("0x1 = USE_TEMPORARY_LINK_KEY");
				return stringBuilder.ToString();
			}
		}

		private uint _KeyFlag;

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

		public KeyFlagEnum GetKeyFlagAsKeyFlagEnum()
		{
			return KeyFlagInfo.ValueToKeyFlag(_KeyFlag);
		}

		public void SetKeyFlagAsKeyFlagEnum(KeyFlagEnum enumItem)
		{
			KeyFlag = KeyFlagInfo.KeyFlagToValue(enumItem);
		}
	}

	private const uint _Opcode = 1047u;

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

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

	public uint Opcode => 1047u;

	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;
		}
		UserFields.KeyFlag = 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);
		bitBuilder.AddBitsAsUint(UserFields.KeyFlag, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
