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

namespace QCRPG.TSL.BT.Packets;

public class LeSetExtendedAdvertisingEnable : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum NumberOfSetsEnum
		{
			Disable_all_advertising_sets,
			Number_of_advertising_sets_to_enable_or_disable,
			InvalidNumberOfSetsValue
		}

		public static class NumberOfSetsInfo
		{
			public static NumberOfSetsEnum ValueToNumberOfSets(uint value)
			{
				if (value == 0)
				{
					return NumberOfSetsEnum.Disable_all_advertising_sets;
				}
				if (value >= 1 && value <= 63)
				{
					return NumberOfSetsEnum.Number_of_advertising_sets_to_enable_or_disable;
				}
				return NumberOfSetsEnum.InvalidNumberOfSetsValue;
			}

			public static uint NumberOfSetsToValue(NumberOfSetsEnum enumItem)
			{
				return enumItem switch
				{
					NumberOfSetsEnum.Disable_all_advertising_sets => 0u, 
					NumberOfSetsEnum.Number_of_advertising_sets_to_enable_or_disable => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string NumberOfSetsToString(NumberOfSetsEnum enumItem)
			{
				return enumItem switch
				{
					NumberOfSetsEnum.Disable_all_advertising_sets => "Disable all advertising sets", 
					NumberOfSetsEnum.Number_of_advertising_sets_to_enable_or_disable => "Number of advertising sets to enable or disable", 
					_ => "Invalid NumberOfSets Value", 
				};
			}

			public static string NumberOfSetsToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Disable all advertising sets");
				stringBuilder.AppendLine("0x1-0x3F = Number of advertising sets to enable or disable");
				return stringBuilder.ToString();
			}
		}

		private uint _Enable;

		private uint _NumberOfSets;

		private List<AdvertisingSetsSubField> _AdvertisingSetsList = new List<AdvertisingSetsSubField>();

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

		public uint NumberOfSets
		{
			get
			{
				return _NumberOfSets;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for NumberOfSets is 0xFF");
				}
				_NumberOfSets = value;
				while (_AdvertisingSetsList.Count < _NumberOfSets)
				{
					_AdvertisingSetsList.Add(new AdvertisingSetsSubField());
				}
				while (_AdvertisingSetsList.Count > _NumberOfSets)
				{
					_AdvertisingSetsList.RemoveAt(_AdvertisingSetsList.Count - 1);
				}
			}
		}

		public List<AdvertisingSetsSubField> AdvertisingSetsList => _AdvertisingSetsList;

		public AdvertisingEnableEnum GetEnableAsAdvertisingEnableEnum()
		{
			return AdvertisingEnableInfo.ValueToAdvertisingEnable(_Enable);
		}

		public void SetEnableAsAdvertisingEnableEnum(AdvertisingEnableEnum enumItem)
		{
			Enable = AdvertisingEnableInfo.AdvertisingEnableToValue(enumItem);
		}

		public NumberOfSetsEnum GetNumberOfSetsAsNumberOfSetsEnum()
		{
			return NumberOfSetsInfo.ValueToNumberOfSets(_NumberOfSets);
		}

		public void SetNumberOfSetsAsNumberOfSetsEnum(NumberOfSetsEnum enumItem)
		{
			NumberOfSets = NumberOfSetsInfo.NumberOfSetsToValue(enumItem);
		}
	}

	public class AdvertisingSetsSubField
	{
		public enum DurationEnum
		{
			No_advertising_duration_Advertising_to_continue_until_the_Host_disables_it,
			InvalidDurationValue
		}

		public static class DurationInfo
		{
			public static DurationEnum ValueToDuration(uint value)
			{
				if (value == 0)
				{
					return DurationEnum.No_advertising_duration_Advertising_to_continue_until_the_Host_disables_it;
				}
				return DurationEnum.InvalidDurationValue;
			}

			public static uint DurationToValue(DurationEnum enumItem)
			{
				if (enumItem == DurationEnum.No_advertising_duration_Advertising_to_continue_until_the_Host_disables_it)
				{
					return 0u;
				}
				return uint.MaxValue;
			}

			public static string DurationToString(DurationEnum enumItem)
			{
				if (enumItem == DurationEnum.No_advertising_duration_Advertising_to_continue_until_the_Host_disables_it)
				{
					return "No advertising duration. Advertising to continue until the Host disables it.";
				}
				return "Invalid Duration Value";
			}

			public static string DurationToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No advertising duration. Advertising to continue until the Host disables it.");
				return stringBuilder.ToString();
			}
		}

		public enum MaxExtendedAdvertisingEventsEnum
		{
			No_maximum_number_of_advertising_events,
			Maximum_number_of_extended_advertising_events_the_Controller_shall_attempt_to_send_prior_to_terminating_the_extended_advertising,
			InvalidMaxExtendedAdvertisingEventsValue
		}

		public static class MaxExtendedAdvertisingEventsInfo
		{
			public static MaxExtendedAdvertisingEventsEnum ValueToMaxExtendedAdvertisingEvents(uint value)
			{
				if (value == 0)
				{
					return MaxExtendedAdvertisingEventsEnum.No_maximum_number_of_advertising_events;
				}
				if (value >= 1 && value <= 255)
				{
					return MaxExtendedAdvertisingEventsEnum.Maximum_number_of_extended_advertising_events_the_Controller_shall_attempt_to_send_prior_to_terminating_the_extended_advertising;
				}
				return MaxExtendedAdvertisingEventsEnum.InvalidMaxExtendedAdvertisingEventsValue;
			}

			public static uint MaxExtendedAdvertisingEventsToValue(MaxExtendedAdvertisingEventsEnum enumItem)
			{
				return enumItem switch
				{
					MaxExtendedAdvertisingEventsEnum.No_maximum_number_of_advertising_events => 0u, 
					MaxExtendedAdvertisingEventsEnum.Maximum_number_of_extended_advertising_events_the_Controller_shall_attempt_to_send_prior_to_terminating_the_extended_advertising => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string MaxExtendedAdvertisingEventsToString(MaxExtendedAdvertisingEventsEnum enumItem)
			{
				return enumItem switch
				{
					MaxExtendedAdvertisingEventsEnum.No_maximum_number_of_advertising_events => "No maximum number of advertising events.", 
					MaxExtendedAdvertisingEventsEnum.Maximum_number_of_extended_advertising_events_the_Controller_shall_attempt_to_send_prior_to_terminating_the_extended_advertising => "Maximum number of extended advertising events the Controller shall attempt to send prior to terminating the extended advertising", 
					_ => "Invalid MaxExtendedAdvertisingEvents Value", 
				};
			}

			public static string MaxExtendedAdvertisingEventsToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = No maximum number of advertising events.");
				stringBuilder.AppendLine("0x1-0xFF = Maximum number of extended advertising events the Controller shall attempt to send prior to terminating the extended advertising");
				return stringBuilder.ToString();
			}
		}

		private uint _AdvertisingHandle;

		private uint _Duration;

		private uint _MaxExtendedAdvertisingEvents;

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

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

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

		public void SetDefaultValues()
		{
			AdvertisingHandle = 0u;
		}

		public DurationEnum GetDurationAsDurationEnum()
		{
			return DurationInfo.ValueToDuration(_Duration);
		}

		public void SetDurationAsDurationEnum(DurationEnum enumItem)
		{
			Duration = DurationInfo.DurationToValue(enumItem);
		}

		public MaxExtendedAdvertisingEventsEnum GetMaxExtendedAdvertisingEventsAsMaxExtendedAdvertisingEventsEnum()
		{
			return MaxExtendedAdvertisingEventsInfo.ValueToMaxExtendedAdvertisingEvents(_MaxExtendedAdvertisingEvents);
		}

		public void SetMaxExtendedAdvertisingEventsAsMaxExtendedAdvertisingEventsEnum(MaxExtendedAdvertisingEventsEnum enumItem)
		{
			MaxExtendedAdvertisingEvents = MaxExtendedAdvertisingEventsInfo.MaxExtendedAdvertisingEventsToValue(enumItem);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			AdvertisingHandle = bitBuilder.RemoveBitsAsUint(8);
			Duration = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			MaxExtendedAdvertisingEvents = bitBuilder.RemoveBitsAsUint(8);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(AdvertisingHandle, 8);
			bitBuilder.AddBitsAsUintLittleEndian(Duration, 16);
			bitBuilder.AddBitsAsUint(MaxExtendedAdvertisingEvents, 8);
			return bitBuilder.ToString();
		}

		public List<string> GetWarnings()
		{
			List<string> list = new List<string>();
			if (AdvertisingHandle < 0)
			{
				list.Add("Minimum allowed value for Advertising_Handle is 0x0");
			}
			return list;
		}
	}

	private const uint _Opcode = 8249u;

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

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

	public uint Opcode => 8249u;

	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.Enable = bitBuilder.RemoveBitsAsUint(8);
		UserFields.NumberOfSets = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.AdvertisingSetsList.Count; i++)
		{
			UserFields.AdvertisingSetsList[i].FromBitBuilder(ref bitBuilder);
		}
		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.Enable, 8);
		bitBuilder.AddBitsAsUint(UserFields.NumberOfSets, 8);
		for (int i = 0; i < UserFields.AdvertisingSetsList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.AdvertisingSetsList[i].ToBitString());
		}
		_PayloadLength = (uint)((bitBuilder.GetNumberOfBits() - numberOfBits - 8) / 8);
		if ((bitBuilder.GetNumberOfBits() - numberOfBits - 8) % 8 != 0)
		{
			_PayloadLength++;
		}
		bitBuilder.ChangeBitsAsUint(numberOfBits, _PayloadLength, 8);
		return bitBuilder.ToString();
	}
}
