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

namespace QCRPG.TSL.BT.Packets;

public class WriteSimplePairingMode : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum SimplePairingModeEnum
		{
			Simple_Pairing_disabled,
			Simple_pairing_enabled,
			Reserved,
			InvalidSimplePairingModeValue
		}

		public static class SimplePairingModeInfo
		{
			public static SimplePairingModeEnum ValueToSimplePairingMode(uint value)
			{
				switch (value)
				{
				case 0u:
					return SimplePairingModeEnum.Simple_Pairing_disabled;
				case 1u:
					return SimplePairingModeEnum.Simple_pairing_enabled;
				default:
					if (value >= 2 && value <= 255)
					{
						return SimplePairingModeEnum.Reserved;
					}
					return SimplePairingModeEnum.InvalidSimplePairingModeValue;
				}
			}

			public static uint SimplePairingModeToValue(SimplePairingModeEnum enumItem)
			{
				return enumItem switch
				{
					SimplePairingModeEnum.Simple_Pairing_disabled => 0u, 
					SimplePairingModeEnum.Simple_pairing_enabled => 1u, 
					SimplePairingModeEnum.Reserved => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string SimplePairingModeToString(SimplePairingModeEnum enumItem)
			{
				return enumItem switch
				{
					SimplePairingModeEnum.Simple_Pairing_disabled => "Simple_Pairing_disabled", 
					SimplePairingModeEnum.Simple_pairing_enabled => "Simple_pairing_enabled", 
					SimplePairingModeEnum.Reserved => "Reserved", 
					_ => "Invalid SimplePairingMode Value", 
				};
			}

			public static string SimplePairingModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Simple_Pairing_disabled");
				stringBuilder.AppendLine("0x1 = Simple_pairing_enabled");
				stringBuilder.AppendLine("0x2-0xFF = Reserved");
				return stringBuilder.ToString();
			}
		}

		private uint _SimplePairingMode;

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

		public SimplePairingModeEnum GetSimplePairingModeAsSimplePairingModeEnum()
		{
			return SimplePairingModeInfo.ValueToSimplePairingMode(_SimplePairingMode);
		}

		public void SetSimplePairingModeAsSimplePairingModeEnum(SimplePairingModeEnum enumItem)
		{
			SimplePairingMode = SimplePairingModeInfo.SimplePairingModeToValue(enumItem);
		}
	}

	private const uint _Opcode = 3158u;

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

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

	public uint Opcode => 3158u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.SimplePairingMode = 0u;
	}

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