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

namespace QCRPG.TSL.BT.Packets;

public class LeSetPrivacyMode : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum PrivacyModeEnum
		{
			Use_Network_Privacy_Mode_for_this_peer_device,
			Use_Device_Privacy_Mode_for_this_peer_device,
			Reserved_for_future_use,
			InvalidPrivacyModeValue
		}

		public static class PrivacyModeInfo
		{
			public static PrivacyModeEnum ValueToPrivacyMode(uint value)
			{
				switch (value)
				{
				case 0u:
					return PrivacyModeEnum.Use_Network_Privacy_Mode_for_this_peer_device;
				case 1u:
					return PrivacyModeEnum.Use_Device_Privacy_Mode_for_this_peer_device;
				default:
					if (value >= 2 && value <= 255)
					{
						return PrivacyModeEnum.Reserved_for_future_use;
					}
					return PrivacyModeEnum.InvalidPrivacyModeValue;
				}
			}

			public static uint PrivacyModeToValue(PrivacyModeEnum enumItem)
			{
				return enumItem switch
				{
					PrivacyModeEnum.Use_Network_Privacy_Mode_for_this_peer_device => 0u, 
					PrivacyModeEnum.Use_Device_Privacy_Mode_for_this_peer_device => 1u, 
					PrivacyModeEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string PrivacyModeToString(PrivacyModeEnum enumItem)
			{
				return enumItem switch
				{
					PrivacyModeEnum.Use_Network_Privacy_Mode_for_this_peer_device => "Use Network Privacy Mode for this peer device (default)", 
					PrivacyModeEnum.Use_Device_Privacy_Mode_for_this_peer_device => "Use Device Privacy Mode for this peer device", 
					PrivacyModeEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid PrivacyMode Value", 
				};
			}

			public static string PrivacyModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Use Network Privacy Mode for this peer device (default)");
				stringBuilder.AppendLine("0x1 = Use Device Privacy Mode for this peer device");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _PeerIdentityAddressType;

		private ulong _PeerIdentityAddress;

		private uint _PrivacyMode;

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

		public ulong PeerIdentityAddress
		{
			get
			{
				return _PeerIdentityAddress;
			}
			set
			{
				_PeerIdentityAddress = value;
			}
		}

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

		public PeerIdentityAddressTypeEnum GetPeerIdentityAddressTypeAsPeerIdentityAddressTypeEnum()
		{
			return PeerIdentityAddressTypeInfo.ValueToPeerIdentityAddressType(_PeerIdentityAddressType);
		}

		public void SetPeerIdentityAddressTypeAsPeerIdentityAddressTypeEnum(PeerIdentityAddressTypeEnum enumItem)
		{
			PeerIdentityAddressType = PeerIdentityAddressTypeInfo.PeerIdentityAddressTypeToValue(enumItem);
		}

		public PrivacyModeEnum GetPrivacyModeAsPrivacyModeEnum()
		{
			return PrivacyModeInfo.ValueToPrivacyMode(_PrivacyMode);
		}

		public void SetPrivacyModeAsPrivacyModeEnum(PrivacyModeEnum enumItem)
		{
			PrivacyMode = PrivacyModeInfo.PrivacyModeToValue(enumItem);
		}
	}

	private const uint _Opcode = 8270u;

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

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

	public uint Opcode => 8270u;

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