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

namespace QCRPG.TSL.BT.Packets;

public class LePeriodicAdvertisingCreateSync : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum OptionEnum
		{
			Advertising_SID,
			Periodic_Advertiser,
			Reserved,
			InvalidOptionValue
		}

		public static class OptionInfo
		{
			public static OptionEnum ValueToOption(uint value)
			{
				switch (value)
				{
				case 0u:
					return OptionEnum.Advertising_SID;
				case 1u:
					return OptionEnum.Periodic_Advertiser;
				default:
					if (value >= 2 && value <= 255)
					{
						return OptionEnum.Reserved;
					}
					return OptionEnum.InvalidOptionValue;
				}
			}

			public static uint OptionToValue(OptionEnum enumItem)
			{
				return enumItem switch
				{
					OptionEnum.Advertising_SID => 0u, 
					OptionEnum.Periodic_Advertiser => 1u, 
					OptionEnum.Reserved => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string OptionToString(OptionEnum enumItem)
			{
				return enumItem switch
				{
					OptionEnum.Advertising_SID => "Use advertising info to listen", 
					OptionEnum.Periodic_Advertiser => "Use periodic advertiser list to listen", 
					OptionEnum.Reserved => "Reserved for future use", 
					_ => "Invalid Option Value", 
				};
			}

			public static string OptionToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Use advertising info to listen");
				stringBuilder.AppendLine("0x1 = Use periodic advertiser list to listen");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		public class SupplementalTypeEnumCollection
		{
			private EnableBitEnum _DoNotSyncToPacketsWithAnAoaSupplemental;

			public EnableBitEnum DoNotSyncToPacketsWithAnAoaSupplemental
			{
				get
				{
					return _DoNotSyncToPacketsWithAnAoaSupplemental;
				}
				set
				{
					_DoNotSyncToPacketsWithAnAoaSupplemental = value;
				}
			}
		}

		public static class SupplementalTypeInfo
		{
			public static SupplementalTypeEnumCollection ValueToSupplementalType(uint value)
			{
				SupplementalTypeEnumCollection supplementalTypeEnumCollection = new SupplementalTypeEnumCollection();
				supplementalTypeEnumCollection.DoNotSyncToPacketsWithAnAoaSupplemental = EnableBitInfo.ValueToEnableBit(value & 1u);
				return supplementalTypeEnumCollection;
			}

			public static uint SupplementalTypeToValue(SupplementalTypeEnumCollection enumCollection)
			{
				uint num = 0u;
				return num | EnableBitInfo.EnableBitToValue(enumCollection.DoNotSyncToPacketsWithAnAoaSupplemental);
			}

			public static string SupplementalTypeToString(SupplementalTypeEnumCollection enumCollection)
			{
				string text = "";
				text = text + "Do_not_sync_to_packets_with_an_AoA_Supplemental: " + EnableBitInfo.EnableBitToString(enumCollection.DoNotSyncToPacketsWithAnAoaSupplemental) + "\r\n";
				return text.TrimEnd('\r', '\n');
			}

			public static string SupplementalTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Bit 0: Do_not_sync_to_packets_with_an_AoA_Supplemental");
				stringBuilder.AppendLine("Bit 1: Do_not_sync_to_packets_with_an_AoD_Supplemental_with_1_us_slots");
				stringBuilder.AppendLine("Bit 2: Do_not_sync_to_packets_with_an_AoD_Supplemental_with_2_us_slots");
				stringBuilder.AppendLine("Bit 4: Do_not_sync_to_packets_without_a_Supplemental");
				stringBuilder.AppendLine("Bits 7-5: Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _Option;

		private uint _AdvertisingSid;

		private uint _AdvertisingAddressType;

		private ulong _AdvertiserAddress;

		private uint _Skip;

		private uint _SyncTimeout = 10u;

		private uint _SupplementalType;

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

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

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

		public ulong AdvertiserAddress
		{
			get
			{
				return _AdvertiserAddress;
			}
			set
			{
				_AdvertiserAddress = value;
			}
		}

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

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

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

		public OptionEnum GetOptionAsOptionEnum()
		{
			return OptionInfo.ValueToOption(_Option);
		}

		public void SetOptionAsOptionEnum(OptionEnum enumItem)
		{
			Option = OptionInfo.OptionToValue(enumItem);
		}

		public DeviceAddressTypeEnum GetAdvertisingAddressTypeAsDeviceAddressTypeEnum()
		{
			return DeviceAddressTypeInfo.ValueToDeviceAddressType(_AdvertisingAddressType);
		}

		public void SetAdvertisingAddressTypeAsDeviceAddressTypeEnum(DeviceAddressTypeEnum enumItem)
		{
			AdvertisingAddressType = DeviceAddressTypeInfo.DeviceAddressTypeToValue(enumItem);
		}

		public SupervisionTimeoutEnum GetSyncTimeoutAsSupervisionTimeoutEnum()
		{
			return SupervisionTimeoutInfo.ValueToSupervisionTimeout(_SyncTimeout);
		}

		public void SetSyncTimeoutAsSupervisionTimeoutEnum(SupervisionTimeoutEnum enumItem)
		{
			SyncTimeout = SupervisionTimeoutInfo.SupervisionTimeoutToValue(enumItem);
		}

		public SupplementalTypeEnumCollection GetSupplementalTypeAsSupplementalTypeEnumCollection()
		{
			return SupplementalTypeInfo.ValueToSupplementalType(_SupplementalType);
		}

		public void SetSupplementalTypeAsSupplementalTypeEnumCollection(SupplementalTypeEnumCollection enumCollection)
		{
			SupplementalType = SupplementalTypeInfo.SupplementalTypeToValue(enumCollection);
		}
	}

	private const uint _Opcode = 8260u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandStatusForLePeriodicAdvertisingCreateSync),
		typeof(LePeriodicAdvertisingSyncEstablished)
	};

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

	public uint Opcode => 8260u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.AdvertisingSid = 0u;
		UserFields.Skip = 0u;
		UserFields.SyncTimeout = 10u;
	}

	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.Option = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AdvertisingSid = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AdvertisingAddressType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AdvertiserAddress = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.Skip = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.SyncTimeout = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.SupplementalType = 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.Option, 8);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingSid, 8);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingAddressType, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.AdvertiserAddress, 48);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.Skip, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SyncTimeout, 16);
		bitBuilder.AddBitsAsUint(UserFields.SupplementalType, 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.AdvertisingSid < 0)
		{
			list.Add("Minimum allowed value for Advertising_SID is 0x0");
		}
		if (UserFields.AdvertisingSid > 15)
		{
			list.Add("Maximum allowed value for Advertising_SID is 0xF");
		}
		if (UserFields.Skip < 0)
		{
			list.Add("Minimum allowed value for Skip is 0x0");
		}
		if (UserFields.Skip > 499)
		{
			list.Add("Maximum allowed value for Skip is 0x1F3");
		}
		if (UserFields.SyncTimeout < 10)
		{
			list.Add("Minimum allowed value for Sync_Timeout is 0xA");
		}
		if (UserFields.SyncTimeout > 16384)
		{
			list.Add("Maximum allowed value for Sync_Timeout is 0x4000");
		}
		return list;
	}
}
