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

namespace QCRPG.TSL.BT.Packets;

public class LeSetExtendedAdvertisingData : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum OperationEnum
		{
			Intermediate_fragment_of_fragmented_extended_advertising_data,
			First_fragment_of_fragmented_extended_advertising_data,
			Last_fragment_of_fragmented_extended_advertising_data,
			Complete_extended_advertising_data,
			Unchanged_data_just_update_the_Advertising_DID,
			Reserved_for_future_use,
			InvalidOperationValue
		}

		public static class OperationInfo
		{
			public static OperationEnum ValueToOperation(uint value)
			{
				switch (value)
				{
				case 0u:
					return OperationEnum.Intermediate_fragment_of_fragmented_extended_advertising_data;
				case 1u:
					return OperationEnum.First_fragment_of_fragmented_extended_advertising_data;
				case 2u:
					return OperationEnum.Last_fragment_of_fragmented_extended_advertising_data;
				case 3u:
					return OperationEnum.Complete_extended_advertising_data;
				case 4u:
					return OperationEnum.Unchanged_data_just_update_the_Advertising_DID;
				default:
					if (value >= 5 && value <= 255)
					{
						return OperationEnum.Reserved_for_future_use;
					}
					return OperationEnum.InvalidOperationValue;
				}
			}

			public static uint OperationToValue(OperationEnum enumItem)
			{
				return enumItem switch
				{
					OperationEnum.Intermediate_fragment_of_fragmented_extended_advertising_data => 0u, 
					OperationEnum.First_fragment_of_fragmented_extended_advertising_data => 1u, 
					OperationEnum.Last_fragment_of_fragmented_extended_advertising_data => 2u, 
					OperationEnum.Complete_extended_advertising_data => 3u, 
					OperationEnum.Unchanged_data_just_update_the_Advertising_DID => 4u, 
					OperationEnum.Reserved_for_future_use => 5u, 
					_ => uint.MaxValue, 
				};
			}

			public static string OperationToString(OperationEnum enumItem)
			{
				return enumItem switch
				{
					OperationEnum.Intermediate_fragment_of_fragmented_extended_advertising_data => "Intermediate fragment of fragmented extended advertising data", 
					OperationEnum.First_fragment_of_fragmented_extended_advertising_data => "First fragment of fragmented extended advertising data", 
					OperationEnum.Last_fragment_of_fragmented_extended_advertising_data => "Last fragment of fragmented extended advertising data", 
					OperationEnum.Complete_extended_advertising_data => "Complete extended advertising data", 
					OperationEnum.Unchanged_data_just_update_the_Advertising_DID => "Unchanged data (just update the Advertising DID)", 
					OperationEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid Operation Value", 
				};
			}

			public static string OperationToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Intermediate fragment of fragmented extended advertising data");
				stringBuilder.AppendLine("0x1 = First fragment of fragmented extended advertising data");
				stringBuilder.AppendLine("0x2 = Last fragment of fragmented extended advertising data");
				stringBuilder.AppendLine("0x3 = Complete extended advertising data");
				stringBuilder.AppendLine("0x4 = Unchanged data (just update the Advertising DID)");
				stringBuilder.AppendLine("0x5-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _AdvertisingHandle;

		private uint _Operation;

		private uint _FragmentPreference;

		private uint _AdvertisingDataLength;

		private List<uint> _AdvertisingDataList = new List<uint>();

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

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

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

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

		public List<uint> AdvertisingDataList => _AdvertisingDataList;

		public OperationEnum GetOperationAsOperationEnum()
		{
			return OperationInfo.ValueToOperation(_Operation);
		}

		public void SetOperationAsOperationEnum(OperationEnum enumItem)
		{
			Operation = OperationInfo.OperationToValue(enumItem);
		}

		public FragmentPreferenceEnum GetFragmentPreferenceAsFragmentPreferenceEnum()
		{
			return FragmentPreferenceInfo.ValueToFragmentPreference(_FragmentPreference);
		}

		public void SetFragmentPreferenceAsFragmentPreferenceEnum(FragmentPreferenceEnum enumItem)
		{
			FragmentPreference = FragmentPreferenceInfo.FragmentPreferenceToValue(enumItem);
		}
	}

	private const uint _Opcode = 8247u;

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

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

	public uint Opcode => 8247u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.AdvertisingHandle = 0u;
		UserFields.AdvertisingDataLength = 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.AdvertisingHandle = bitBuilder.RemoveBitsAsUint(8);
		UserFields.Operation = bitBuilder.RemoveBitsAsUint(8);
		UserFields.FragmentPreference = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AdvertisingDataLength = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.AdvertisingDataList.Count; i++)
		{
			UserFields.AdvertisingDataList[i] = 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.AdvertisingHandle, 8);
		bitBuilder.AddBitsAsUint(UserFields.Operation, 8);
		bitBuilder.AddBitsAsUint(UserFields.FragmentPreference, 8);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingDataLength, 8);
		for (int i = 0; i < UserFields.AdvertisingDataList.Count; i++)
		{
			if (UserFields.AdvertisingDataList[i] > 255)
			{
				throw new Exception("UserFields.AdvertisingDataList[i] is > 0xFF");
			}
			bitBuilder.AddBitsAsUint(UserFields.AdvertisingDataList[i], 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.AdvertisingHandle < 0)
		{
			list.Add("Minimum allowed value for Advertising_Handle is 0x0");
		}
		if (UserFields.AdvertisingHandle > 239)
		{
			list.Add("Maximum allowed value for Advertising_Handle is 0xEF");
		}
		if (UserFields.AdvertisingDataLength < 0)
		{
			list.Add("Minimum allowed value for Advertising_Data_Length is 0x0");
		}
		if (UserFields.AdvertisingDataLength > 252)
		{
			list.Add("Maximum allowed value for Advertising_Data_Length is 0xFC");
		}
		return list;
	}
}
