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

namespace QCRPG.TSL.BT.Packets;

public class LeSetPeriodicAdvertisingParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		public class PeriodicAdvertisingPropertiesEnumCollection
		{
			private EnableBitEnum _IncludeTxpowerInTheAdvertisingPdu;

			public EnableBitEnum IncludeTxpowerInTheAdvertisingPdu
			{
				get
				{
					return _IncludeTxpowerInTheAdvertisingPdu;
				}
				set
				{
					_IncludeTxpowerInTheAdvertisingPdu = value;
				}
			}
		}

		public static class PeriodicAdvertisingPropertiesInfo
		{
			public static PeriodicAdvertisingPropertiesEnumCollection ValueToPeriodicAdvertisingProperties(uint value)
			{
				PeriodicAdvertisingPropertiesEnumCollection periodicAdvertisingPropertiesEnumCollection = new PeriodicAdvertisingPropertiesEnumCollection();
				periodicAdvertisingPropertiesEnumCollection.IncludeTxpowerInTheAdvertisingPdu = EnableBitInfo.ValueToEnableBit((value & 0x40) >> 6);
				return periodicAdvertisingPropertiesEnumCollection;
			}

			public static uint PeriodicAdvertisingPropertiesToValue(PeriodicAdvertisingPropertiesEnumCollection enumCollection)
			{
				uint num = 0u;
				return num | (EnableBitInfo.EnableBitToValue(enumCollection.IncludeTxpowerInTheAdvertisingPdu) << 6);
			}

			public static string PeriodicAdvertisingPropertiesToString(PeriodicAdvertisingPropertiesEnumCollection enumCollection)
			{
				string text = "";
				text = text + "Include TxPower in the advertising PDU: " + EnableBitInfo.EnableBitToString(enumCollection.IncludeTxpowerInTheAdvertisingPdu) + "\r\n";
				return text.TrimEnd('\r', '\n');
			}

			public static string PeriodicAdvertisingPropertiesToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Bits 5-0: Reserved for future use");
				stringBuilder.AppendLine("Bit 6: Include TxPower in the advertising PDU");
				stringBuilder.AppendLine("Bits 15-7: Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _AdvertisingHandle;

		private uint _PeriodicAdvertisingIntervalMin = 6u;

		private uint _PeriodicAdvertisingIntervalMax = 6u;

		private uint _PeriodicAdvertisingProperties;

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

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

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

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

		public PeriodicAdvertisingPropertiesEnumCollection GetPeriodicAdvertisingPropertiesAsPeriodicAdvertisingPropertiesEnumCollection()
		{
			return PeriodicAdvertisingPropertiesInfo.ValueToPeriodicAdvertisingProperties(_PeriodicAdvertisingProperties);
		}

		public void SetPeriodicAdvertisingPropertiesAsPeriodicAdvertisingPropertiesEnumCollection(PeriodicAdvertisingPropertiesEnumCollection enumCollection)
		{
			PeriodicAdvertisingProperties = PeriodicAdvertisingPropertiesInfo.PeriodicAdvertisingPropertiesToValue(enumCollection);
		}
	}

	private const uint _Opcode = 8254u;

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

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

	public uint Opcode => 8254u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.AdvertisingHandle = 0u;
		UserFields.PeriodicAdvertisingIntervalMin = 6u;
		UserFields.PeriodicAdvertisingIntervalMax = 6u;
	}

	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.PeriodicAdvertisingIntervalMin = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.PeriodicAdvertisingIntervalMax = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.PeriodicAdvertisingProperties = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		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.AddBitsAsUintLittleEndian(UserFields.PeriodicAdvertisingIntervalMin, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.PeriodicAdvertisingIntervalMax, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.PeriodicAdvertisingProperties, 16);
		_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.PeriodicAdvertisingIntervalMin < 6)
		{
			list.Add("Minimum allowed value for Periodic_Advertising_Interval_Min is 0x6");
		}
		if (UserFields.PeriodicAdvertisingIntervalMin > 65535)
		{
			list.Add("Maximum allowed value for Periodic_Advertising_Interval_Min is 0xFFFF");
		}
		if (UserFields.PeriodicAdvertisingIntervalMax < 6)
		{
			list.Add("Minimum allowed value for Periodic_Advertising_Interval_Max is 0x6");
		}
		if (UserFields.PeriodicAdvertisingIntervalMax > 65535)
		{
			list.Add("Maximum allowed value for Periodic_Advertising_Interval_Max is 0xFFFF");
		}
		return list;
	}
}
