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

namespace QCRPG.TSL.BT.Packets;

public class IoCapabilityResponse : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum OobDataPresentEnum
		{
			OOB_authentication_data_not_present,
			OOB_authentication_data_from_remote_device_present,
			Reserved_for_future_use,
			InvalidOobDataPresentValue
		}

		public static class OobDataPresentInfo
		{
			public static OobDataPresentEnum ValueToOobDataPresent(uint value)
			{
				switch (value)
				{
				case 0u:
					return OobDataPresentEnum.OOB_authentication_data_not_present;
				case 1u:
					return OobDataPresentEnum.OOB_authentication_data_from_remote_device_present;
				default:
					if (value >= 2 && value <= 255)
					{
						return OobDataPresentEnum.Reserved_for_future_use;
					}
					return OobDataPresentEnum.InvalidOobDataPresentValue;
				}
			}

			public static uint OobDataPresentToValue(OobDataPresentEnum enumItem)
			{
				return enumItem switch
				{
					OobDataPresentEnum.OOB_authentication_data_not_present => 0u, 
					OobDataPresentEnum.OOB_authentication_data_from_remote_device_present => 1u, 
					OobDataPresentEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string OobDataPresentToString(OobDataPresentEnum enumItem)
			{
				return enumItem switch
				{
					OobDataPresentEnum.OOB_authentication_data_not_present => "OOB authentication data not present", 
					OobDataPresentEnum.OOB_authentication_data_from_remote_device_present => "OOB authentication data from remote device present", 
					OobDataPresentEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid OobDataPresent Value", 
				};
			}

			public static string OobDataPresentToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = OOB authentication data not present");
				stringBuilder.AppendLine("0x1 = OOB authentication data from remote device present");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private ulong _BdAddr;

		private uint _IoCapability;

		private uint _OobDataPresent;

		private uint _AuthenticationRequirements;

		public ulong BdAddr
		{
			get
			{
				return _BdAddr;
			}
			set
			{
				_BdAddr = value;
			}
		}

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

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

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

		public IoCapabilityEnum GetIoCapabilityAsIoCapabilityEnum()
		{
			return IoCapabilityInfo.ValueToIoCapability(_IoCapability);
		}

		public void SetIoCapabilityAsIoCapabilityEnum(IoCapabilityEnum enumItem)
		{
			IoCapability = IoCapabilityInfo.IoCapabilityToValue(enumItem);
		}

		public OobDataPresentEnum GetOobDataPresentAsOobDataPresentEnum()
		{
			return OobDataPresentInfo.ValueToOobDataPresent(_OobDataPresent);
		}

		public void SetOobDataPresentAsOobDataPresentEnum(OobDataPresentEnum enumItem)
		{
			OobDataPresent = OobDataPresentInfo.OobDataPresentToValue(enumItem);
		}

		public AuthenticationRequirementsEnum GetAuthenticationRequirementsAsAuthenticationRequirementsEnum()
		{
			return AuthenticationRequirementsInfo.ValueToAuthenticationRequirements(_AuthenticationRequirements);
		}

		public void SetAuthenticationRequirementsAsAuthenticationRequirementsEnum(AuthenticationRequirementsEnum enumItem)
		{
			AuthenticationRequirements = AuthenticationRequirementsInfo.AuthenticationRequirementsToValue(enumItem);
		}
	}

	private const uint _EventCode = 50u;

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

	public uint EventCode => 50u;

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

	public UserFieldsWrapper UserFields => _UserFields;

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

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