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

namespace QCRPG.TSL.BT.Packets;

public class WriteSecureConnectionsTestMode : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum Dm1AclUModeEnum
		{
			DM1_ACL_U_mode_disabled,
			DM1_ACL_U_mode_enabled,
			Reserved_for_future_use,
			InvalidDm1AclUModeValue
		}

		public static class Dm1AclUModeInfo
		{
			public static Dm1AclUModeEnum ValueToDm1AclUMode(uint value)
			{
				switch (value)
				{
				case 0u:
					return Dm1AclUModeEnum.DM1_ACL_U_mode_disabled;
				case 1u:
					return Dm1AclUModeEnum.DM1_ACL_U_mode_enabled;
				default:
					if (value >= 2 && value <= 255)
					{
						return Dm1AclUModeEnum.Reserved_for_future_use;
					}
					return Dm1AclUModeEnum.InvalidDm1AclUModeValue;
				}
			}

			public static uint Dm1AclUModeToValue(Dm1AclUModeEnum enumItem)
			{
				return enumItem switch
				{
					Dm1AclUModeEnum.DM1_ACL_U_mode_disabled => 0u, 
					Dm1AclUModeEnum.DM1_ACL_U_mode_enabled => 1u, 
					Dm1AclUModeEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string Dm1AclUModeToString(Dm1AclUModeEnum enumItem)
			{
				return enumItem switch
				{
					Dm1AclUModeEnum.DM1_ACL_U_mode_disabled => "DM1 ACL-U mode disabled (default)", 
					Dm1AclUModeEnum.DM1_ACL_U_mode_enabled => "DM1 ACL-U mode enabled", 
					Dm1AclUModeEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid Dm1AclUMode Value", 
				};
			}

			public static string Dm1AclUModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = DM1 ACL-U mode disabled (default)");
				stringBuilder.AppendLine("0x1 = DM1 ACL-U mode enabled");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		public enum EscoLoopbackModeEnum
		{
			eSCO_loopback_mode_disabled,
			eSCO_loopback_mode_enabled,
			Reserved_for_future_use,
			InvalidEscoLoopbackModeValue
		}

		public static class EscoLoopbackModeInfo
		{
			public static EscoLoopbackModeEnum ValueToEscoLoopbackMode(uint value)
			{
				switch (value)
				{
				case 0u:
					return EscoLoopbackModeEnum.eSCO_loopback_mode_disabled;
				case 1u:
					return EscoLoopbackModeEnum.eSCO_loopback_mode_enabled;
				default:
					if (value >= 2 && value <= 255)
					{
						return EscoLoopbackModeEnum.Reserved_for_future_use;
					}
					return EscoLoopbackModeEnum.InvalidEscoLoopbackModeValue;
				}
			}

			public static uint EscoLoopbackModeToValue(EscoLoopbackModeEnum enumItem)
			{
				return enumItem switch
				{
					EscoLoopbackModeEnum.eSCO_loopback_mode_disabled => 0u, 
					EscoLoopbackModeEnum.eSCO_loopback_mode_enabled => 1u, 
					EscoLoopbackModeEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EscoLoopbackModeToString(EscoLoopbackModeEnum enumItem)
			{
				return enumItem switch
				{
					EscoLoopbackModeEnum.eSCO_loopback_mode_disabled => "eSCO loopback mode disabled (default)", 
					EscoLoopbackModeEnum.eSCO_loopback_mode_enabled => "eSCO loopback mode enabled", 
					EscoLoopbackModeEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid EscoLoopbackMode Value", 
				};
			}

			public static string EscoLoopbackModeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = eSCO loopback mode disabled (default)");
				stringBuilder.AppendLine("0x1 = eSCO loopback mode enabled");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _ConnectionHandle;

		private uint _Dm1AclUMode;

		private uint _EscoLoopbackMode;

		public uint ConnectionHandle
		{
			get
			{
				return _ConnectionHandle;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for ConnectionHandle is 0xFFFF");
				}
				_ConnectionHandle = value;
			}
		}

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

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

		public Dm1AclUModeEnum GetDm1AclUModeAsDm1AclUModeEnum()
		{
			return Dm1AclUModeInfo.ValueToDm1AclUMode(_Dm1AclUMode);
		}

		public void SetDm1AclUModeAsDm1AclUModeEnum(Dm1AclUModeEnum enumItem)
		{
			Dm1AclUMode = Dm1AclUModeInfo.Dm1AclUModeToValue(enumItem);
		}

		public EscoLoopbackModeEnum GetEscoLoopbackModeAsEscoLoopbackModeEnum()
		{
			return EscoLoopbackModeInfo.ValueToEscoLoopbackMode(_EscoLoopbackMode);
		}

		public void SetEscoLoopbackModeAsEscoLoopbackModeEnum(EscoLoopbackModeEnum enumItem)
		{
			EscoLoopbackMode = EscoLoopbackModeInfo.EscoLoopbackModeToValue(enumItem);
		}
	}

	private const uint _Opcode = 6154u;

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

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

	public uint Opcode => 6154u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 0u;
		UserFields.Dm1AclUMode = 0u;
		UserFields.EscoLoopbackMode = 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.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.Dm1AclUMode = bitBuilder.RemoveBitsAsUint(8);
		UserFields.EscoLoopbackMode = 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.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.Dm1AclUMode, 8);
		bitBuilder.AddBitsAsUint(UserFields.EscoLoopbackMode, 8);
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}

	public override List<string> GetWarnings()
	{
		List<string> list = new List<string>();
		if (UserFields.ConnectionHandle < 0)
		{
			list.Add("Minimum allowed value for Connection_Handle is 0x0");
		}
		if (UserFields.ConnectionHandle > 3839)
		{
			list.Add("Maximum allowed value for Connection_Handle is 0xEFF");
		}
		return list;
	}
}
