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

namespace QCRPG.TSL.BT.Packets;

public class SetTriggeredClockCapture : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum EnableEnum
		{
			Disable_triggered_clock_capturing_on_the_specified_Connection_Handle,
			Enable_triggered_clock_capturing_on_the_specified_Connection_Handle,
			Reserved_for_future_use,
			InvalidEnableValue
		}

		public static class EnableInfo
		{
			public static EnableEnum ValueToEnable(uint value)
			{
				switch (value)
				{
				case 0u:
					return EnableEnum.Disable_triggered_clock_capturing_on_the_specified_Connection_Handle;
				case 1u:
					return EnableEnum.Enable_triggered_clock_capturing_on_the_specified_Connection_Handle;
				default:
					if (value >= 2 && value <= 255)
					{
						return EnableEnum.Reserved_for_future_use;
					}
					return EnableEnum.InvalidEnableValue;
				}
			}

			public static uint EnableToValue(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.Disable_triggered_clock_capturing_on_the_specified_Connection_Handle => 0u, 
					EnableEnum.Enable_triggered_clock_capturing_on_the_specified_Connection_Handle => 1u, 
					EnableEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string EnableToString(EnableEnum enumItem)
			{
				return enumItem switch
				{
					EnableEnum.Disable_triggered_clock_capturing_on_the_specified_Connection_Handle => "Disable triggered clock capturing on the specified Connection Handle(Default).", 
					EnableEnum.Enable_triggered_clock_capturing_on_the_specified_Connection_Handle => "Enable triggered clock capturing on the specified Connection Handle.", 
					EnableEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid Enable Value", 
				};
			}

			public static string EnableToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Disable triggered clock capturing on the specified Connection Handle(Default).");
				stringBuilder.AppendLine("0x1 = Enable triggered clock capturing on the specified Connection Handle.");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		public enum WhichClockEnum
		{
			Local_Clock,
			Piconet_Clock,
			Reserved_for_future_use,
			InvalidWhichClockValue
		}

		public static class WhichClockInfo
		{
			public static WhichClockEnum ValueToWhichClock(uint value)
			{
				switch (value)
				{
				case 0u:
					return WhichClockEnum.Local_Clock;
				case 1u:
					return WhichClockEnum.Piconet_Clock;
				default:
					if (value >= 2 && value <= 255)
					{
						return WhichClockEnum.Reserved_for_future_use;
					}
					return WhichClockEnum.InvalidWhichClockValue;
				}
			}

			public static uint WhichClockToValue(WhichClockEnum enumItem)
			{
				return enumItem switch
				{
					WhichClockEnum.Local_Clock => 0u, 
					WhichClockEnum.Piconet_Clock => 1u, 
					WhichClockEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string WhichClockToString(WhichClockEnum enumItem)
			{
				return enumItem switch
				{
					WhichClockEnum.Local_Clock => "Local Clock (Connection_Handle does not have to be valid)", 
					WhichClockEnum.Piconet_Clock => "Piconet Clock (Connection_Handle shall be valid)", 
					WhichClockEnum.Reserved_for_future_use => "Reserved for future use.", 
					_ => "Invalid WhichClock Value", 
				};
			}

			public static string WhichClockToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Local Clock (Connection_Handle does not have to be valid)");
				stringBuilder.AppendLine("0x1 = Piconet Clock (Connection_Handle shall be valid)");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use.");
				return stringBuilder.ToString();
			}
		}

		public enum LpoAllowedEnum
		{
			Controller_shall_not_sleep,
			Controller_may_sleep,
			Reserved_for_future_use,
			InvalidLpoAllowedValue
		}

		public static class LpoAllowedInfo
		{
			public static LpoAllowedEnum ValueToLpoAllowed(uint value)
			{
				switch (value)
				{
				case 0u:
					return LpoAllowedEnum.Controller_shall_not_sleep;
				case 1u:
					return LpoAllowedEnum.Controller_may_sleep;
				default:
					if (value >= 2 && value <= 255)
					{
						return LpoAllowedEnum.Reserved_for_future_use;
					}
					return LpoAllowedEnum.InvalidLpoAllowedValue;
				}
			}

			public static uint LpoAllowedToValue(LpoAllowedEnum enumItem)
			{
				return enumItem switch
				{
					LpoAllowedEnum.Controller_shall_not_sleep => 0u, 
					LpoAllowedEnum.Controller_may_sleep => 1u, 
					LpoAllowedEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string LpoAllowedToString(LpoAllowedEnum enumItem)
			{
				return enumItem switch
				{
					LpoAllowedEnum.Controller_shall_not_sleep => "Controller shall not sleep (that is, clock accuracy shall be equal to or better than Â± 20 ppm)", 
					LpoAllowedEnum.Controller_may_sleep => "Controller may sleep (that is, clock accuracy shall be equal to or better than Â± 250 ppm)", 
					LpoAllowedEnum.Reserved_for_future_use => "Reserved for future use.", 
					_ => "Invalid LpoAllowed Value", 
				};
			}

			public static string LpoAllowedToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Controller shall not sleep (that is, clock accuracy shall be equal to or better than Â± 20 ppm)");
				stringBuilder.AppendLine("0x1 = Controller may sleep (that is, clock accuracy shall be equal to or better than Â± 250 ppm)");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use.");
				return stringBuilder.ToString();
			}
		}

		public enum NumClockCapturesToFilterEnum
		{
			All_triggered_clock_captures_result_in_a_Triggered_Clock_Capture_event_sent_to_the_Host,
			Number_of_triggered_clock_captures_filtered_between_sending_a_Triggered_Clock_Capture_event_to_the_Host,
			InvalidNumClockCapturesToFilterValue
		}

		public static class NumClockCapturesToFilterInfo
		{
			public static NumClockCapturesToFilterEnum ValueToNumClockCapturesToFilter(uint value)
			{
				if (value == 0)
				{
					return NumClockCapturesToFilterEnum.All_triggered_clock_captures_result_in_a_Triggered_Clock_Capture_event_sent_to_the_Host;
				}
				if (value >= 1 && value <= 255)
				{
					return NumClockCapturesToFilterEnum.Number_of_triggered_clock_captures_filtered_between_sending_a_Triggered_Clock_Capture_event_to_the_Host;
				}
				return NumClockCapturesToFilterEnum.InvalidNumClockCapturesToFilterValue;
			}

			public static uint NumClockCapturesToFilterToValue(NumClockCapturesToFilterEnum enumItem)
			{
				return enumItem switch
				{
					NumClockCapturesToFilterEnum.All_triggered_clock_captures_result_in_a_Triggered_Clock_Capture_event_sent_to_the_Host => 0u, 
					NumClockCapturesToFilterEnum.Number_of_triggered_clock_captures_filtered_between_sending_a_Triggered_Clock_Capture_event_to_the_Host => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string NumClockCapturesToFilterToString(NumClockCapturesToFilterEnum enumItem)
			{
				return enumItem switch
				{
					NumClockCapturesToFilterEnum.All_triggered_clock_captures_result_in_a_Triggered_Clock_Capture_event_sent_to_the_Host => "All triggered clock captures result in a Triggered Clock Capture event sent to the Host.", 
					NumClockCapturesToFilterEnum.Number_of_triggered_clock_captures_filtered_between_sending_a_Triggered_Clock_Capture_event_to_the_Host => "Number of triggered clock captures filtered between sending a Triggered Clock Capture event to the Host.", 
					_ => "Invalid NumClockCapturesToFilter Value", 
				};
			}

			public static string NumClockCapturesToFilterToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = All triggered clock captures result in a Triggered Clock Capture event sent to the Host.");
				stringBuilder.AppendLine("0x1-0xFF = Number of triggered clock captures filtered between sending a Triggered Clock Capture event to the Host.");
				return stringBuilder.ToString();
			}
		}

		private uint _ConnectionHandle;

		private uint _Enable;

		private uint _WhichClock;

		private uint _LpoAllowed;

		private uint _NumClockCapturesToFilter;

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

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

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

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

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

		public EnableEnum GetEnableAsEnableEnum()
		{
			return EnableInfo.ValueToEnable(_Enable);
		}

		public void SetEnableAsEnableEnum(EnableEnum enumItem)
		{
			Enable = EnableInfo.EnableToValue(enumItem);
		}

		public WhichClockEnum GetWhichClockAsWhichClockEnum()
		{
			return WhichClockInfo.ValueToWhichClock(_WhichClock);
		}

		public void SetWhichClockAsWhichClockEnum(WhichClockEnum enumItem)
		{
			WhichClock = WhichClockInfo.WhichClockToValue(enumItem);
		}

		public LpoAllowedEnum GetLpoAllowedAsLpoAllowedEnum()
		{
			return LpoAllowedInfo.ValueToLpoAllowed(_LpoAllowed);
		}

		public void SetLpoAllowedAsLpoAllowedEnum(LpoAllowedEnum enumItem)
		{
			LpoAllowed = LpoAllowedInfo.LpoAllowedToValue(enumItem);
		}

		public NumClockCapturesToFilterEnum GetNumClockCapturesToFilterAsNumClockCapturesToFilterEnum()
		{
			return NumClockCapturesToFilterInfo.ValueToNumClockCapturesToFilter(_NumClockCapturesToFilter);
		}

		public void SetNumClockCapturesToFilterAsNumClockCapturesToFilterEnum(NumClockCapturesToFilterEnum enumItem)
		{
			NumClockCapturesToFilter = NumClockCapturesToFilterInfo.NumClockCapturesToFilterToValue(enumItem);
		}
	}

	private const uint _Opcode = 5133u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForSetTriggeredClockCapture),
		typeof(TriggeredClockCapture)
	};

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

	public uint Opcode => 5133u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 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.Enable = bitBuilder.RemoveBitsAsUint(8);
		UserFields.WhichClock = bitBuilder.RemoveBitsAsUint(8);
		UserFields.LpoAllowed = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumClockCapturesToFilter = 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.Enable, 8);
		bitBuilder.AddBitsAsUint(UserFields.WhichClock, 8);
		bitBuilder.AddBitsAsUint(UserFields.LpoAllowed, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumClockCapturesToFilter, 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;
	}
}
