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

namespace QCRPG.TSL.BT.Packets;

public class LePeriodicAdvertisingSyncTransferReceived : PacketBase
{
	public class UserFieldsWrapper
	{
		public enum AdvertisingAddresstypeEnum
		{
			Public_Address,
			Random_Address,
			InvalidAdvertisingAddresstypeValue
		}

		public static class AdvertisingAddresstypeInfo
		{
			public static AdvertisingAddresstypeEnum ValueToAdvertisingAddresstype(uint value)
			{
				return value switch
				{
					0u => AdvertisingAddresstypeEnum.Public_Address, 
					1u => AdvertisingAddresstypeEnum.Random_Address, 
					_ => AdvertisingAddresstypeEnum.InvalidAdvertisingAddresstypeValue, 
				};
			}

			public static uint AdvertisingAddresstypeToValue(AdvertisingAddresstypeEnum enumItem)
			{
				return enumItem switch
				{
					AdvertisingAddresstypeEnum.Public_Address => 0u, 
					AdvertisingAddresstypeEnum.Random_Address => 1u, 
					_ => uint.MaxValue, 
				};
			}

			public static string AdvertisingAddresstypeToString(AdvertisingAddresstypeEnum enumItem)
			{
				return enumItem switch
				{
					AdvertisingAddresstypeEnum.Public_Address => "Public Address", 
					AdvertisingAddresstypeEnum.Random_Address => "Random Address", 
					_ => "Invalid AdvertisingAddresstype Value", 
				};
			}

			public static string AdvertisingAddresstypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Public Address");
				stringBuilder.AppendLine("0x1 = Random Address");
				return stringBuilder.ToString();
			}
		}

		private const uint _SubeventCode = 24u;

		private uint _Status;

		private uint _ConnectionHandle;

		private uint _ServiceData;

		private uint _SyncHandle;

		private uint _AdvertisingSid;

		private uint _AdvertisingAddresstype;

		private ulong _AdvertisingAddress;

		private uint _AdvertisingPhy;

		private uint _PeriodicAdvertisingInterval;

		private uint _AdvertiserClockaccuracy;

		public uint SubeventCode => 24u;

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

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

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

		public uint SyncHandle
		{
			get
			{
				return _SyncHandle;
			}
			set
			{
				if (value > 65535)
				{
					throw new Exception("Maximum possible value for SyncHandle is 0xFFFF");
				}
				_SyncHandle = 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 AdvertisingAddress
		{
			get
			{
				return _AdvertisingAddress;
			}
			set
			{
				_AdvertisingAddress = value;
			}
		}

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

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

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

		public ErrorCodeEnum GetStatusAsErrorCodeEnum()
		{
			return ErrorCodeInfo.ValueToErrorCode(_Status);
		}

		public void SetStatusAsErrorCodeEnum(ErrorCodeEnum enumItem)
		{
			Status = ErrorCodeInfo.ErrorCodeToValue(enumItem);
		}

		public AdvertisingAddresstypeEnum GetAdvertisingAddresstypeAsAdvertisingAddresstypeEnum()
		{
			return AdvertisingAddresstypeInfo.ValueToAdvertisingAddresstype(_AdvertisingAddresstype);
		}

		public void SetAdvertisingAddresstypeAsAdvertisingAddresstypeEnum(AdvertisingAddresstypeEnum enumItem)
		{
			AdvertisingAddresstype = AdvertisingAddresstypeInfo.AdvertisingAddresstypeToValue(enumItem);
		}
	}

	private const uint _EventCode = 62u;

	private Type[] expectedResponseTypes = new Type[0];

	private PacketInfoBase info;

	private uint _PayloadLength;

	public UserFieldsWrapper _UserFields = new UserFieldsWrapper();

	public override PacketType Type => PacketType.Notification;

	public override Type[] ExpectedResponseTypes => expectedResponseTypes;

	public override PacketInfoBase Info
	{
		get
		{
			if (info == null)
			{
				info = new LePeriodicAdvertisingSyncTransferReceivedInfo(this);
			}
			return info;
		}
	}

	public uint EventCode => 62u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ConnectionHandle = 0u;
		UserFields.SyncHandle = 0u;
		UserFields.AdvertisingAddresstype = 0u;
	}

	protected override bool FromBitBuilder(ref BitBuilder bitBuilder)
	{
		if (bitBuilder.RemoveBitsAsUint(8) != EventCode)
		{
			return false;
		}
		_PayloadLength = bitBuilder.RemoveBitsAsUint(8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		if (numberOfBits > _PayloadLength * 8 + 7)
		{
			return false;
		}
		if (numberOfBits < _PayloadLength * 8)
		{
			return false;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubeventCode)
		{
			return false;
		}
		UserFields.Status = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ConnectionHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.ServiceData = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.SyncHandle = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.AdvertisingSid = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AdvertisingAddresstype = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AdvertisingAddress = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.AdvertisingPhy = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PeriodicAdvertisingInterval = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.AdvertiserClockaccuracy = bitBuilder.RemoveBitsAsUint(8);
		if (bitBuilder.GetNumberOfBits() > 7)
		{
			return false;
		}
		return true;
	}

	public override string ToBitString()
	{
		BitBuilder bitBuilder = new BitBuilder();
		bitBuilder.AddBitsAsUint(EventCode, 8);
		int numberOfBits = bitBuilder.GetNumberOfBits();
		bitBuilder.AddBitsAsUint(0u, 8);
		bitBuilder.AddBitsAsUint(UserFields.SubeventCode, 8);
		bitBuilder.AddBitsAsUint(UserFields.Status, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ConnectionHandle, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.ServiceData, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.SyncHandle, 16);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingSid, 8);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingAddresstype, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.AdvertisingAddress, 48);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingPhy, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.PeriodicAdvertisingInterval, 16);
		bitBuilder.AddBitsAsUint(UserFields.AdvertiserClockaccuracy, 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");
		}
		if (UserFields.SyncHandle < 0)
		{
			list.Add("Minimum allowed value for Sync_Handle is 0x0");
		}
		if (UserFields.SyncHandle > 3839)
		{
			list.Add("Maximum allowed value for Sync_Handle is 0xEFF");
		}
		if (UserFields.AdvertisingAddresstype < 0)
		{
			list.Add("Minimum allowed value for Advertising_AddressType is 0x0");
		}
		if (UserFields.AdvertisingAddresstype > 1)
		{
			list.Add("Maximum allowed value for Advertising_AddressType is 0x1");
		}
		return list;
	}
}
