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

namespace QCRPG.TSL.BT.Packets;

public class LeSetAdvertisingParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		private uint _AdvertisingIntervalMin = 32u;

		private uint _AdvertisingIntervalMax = 32u;

		private uint _AdvertisingType;

		private uint _OwnAddressType;

		private uint _PeerAddressType;

		private ulong _PeerAddress;

		private uint _AdvertisingChannelMap;

		private uint _AdvertisingFilterPolicy;

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

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

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

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

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

		public ulong PeerAddress
		{
			get
			{
				return _PeerAddress;
			}
			set
			{
				_PeerAddress = value;
			}
		}

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

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

		public AdvertisingTypeEnum GetAdvertisingTypeAsAdvertisingTypeEnum()
		{
			return AdvertisingTypeInfo.ValueToAdvertisingType(_AdvertisingType);
		}

		public void SetAdvertisingTypeAsAdvertisingTypeEnum(AdvertisingTypeEnum enumItem)
		{
			AdvertisingType = AdvertisingTypeInfo.AdvertisingTypeToValue(enumItem);
		}

		public OwnAddressTypeEnum GetOwnAddressTypeAsOwnAddressTypeEnum()
		{
			return OwnAddressTypeInfo.ValueToOwnAddressType(_OwnAddressType);
		}

		public void SetOwnAddressTypeAsOwnAddressTypeEnum(OwnAddressTypeEnum enumItem)
		{
			OwnAddressType = OwnAddressTypeInfo.OwnAddressTypeToValue(enumItem);
		}

		public PeerAddressTypeEnum GetPeerAddressTypeAsPeerAddressTypeEnum()
		{
			return PeerAddressTypeInfo.ValueToPeerAddressType(_PeerAddressType);
		}

		public void SetPeerAddressTypeAsPeerAddressTypeEnum(PeerAddressTypeEnum enumItem)
		{
			PeerAddressType = PeerAddressTypeInfo.PeerAddressTypeToValue(enumItem);
		}

		public AdvertisingChannelMapEnumCollection GetAdvertisingChannelMapAsAdvertisingChannelMapEnumCollection()
		{
			return AdvertisingChannelMapInfo.ValueToAdvertisingChannelMap(_AdvertisingChannelMap);
		}

		public void SetAdvertisingChannelMapAsAdvertisingChannelMapEnumCollection(AdvertisingChannelMapEnumCollection enumCollection)
		{
			AdvertisingChannelMap = AdvertisingChannelMapInfo.AdvertisingChannelMapToValue(enumCollection);
		}

		public AdvertisingFilterPolicyEnum GetAdvertisingFilterPolicyAsAdvertisingFilterPolicyEnum()
		{
			return AdvertisingFilterPolicyInfo.ValueToAdvertisingFilterPolicy(_AdvertisingFilterPolicy);
		}

		public void SetAdvertisingFilterPolicyAsAdvertisingFilterPolicyEnum(AdvertisingFilterPolicyEnum enumItem)
		{
			AdvertisingFilterPolicy = AdvertisingFilterPolicyInfo.AdvertisingFilterPolicyToValue(enumItem);
		}
	}

	private const uint _Opcode = 8198u;

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

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

	public uint Opcode => 8198u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.AdvertisingIntervalMin = 2048u;
		UserFields.AdvertisingIntervalMax = 2048u;
		UserFields.AdvertisingChannelMap = 7u;
	}

	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.AdvertisingIntervalMin = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.AdvertisingIntervalMax = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.AdvertisingType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.OwnAddressType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PeerAddressType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PeerAddress = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.AdvertisingChannelMap = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AdvertisingFilterPolicy = 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.AddBitsAsUintLittleEndian(UserFields.AdvertisingIntervalMin, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.AdvertisingIntervalMax, 16);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingType, 8);
		bitBuilder.AddBitsAsUint(UserFields.OwnAddressType, 8);
		bitBuilder.AddBitsAsUint(UserFields.PeerAddressType, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.PeerAddress, 48);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingChannelMap, 8);
		bitBuilder.AddBitsAsUint(UserFields.AdvertisingFilterPolicy, 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.AdvertisingIntervalMin < 32)
		{
			list.Add("Minimum allowed value for Advertising_Interval_Min is 0x20");
		}
		if (UserFields.AdvertisingIntervalMin > 16384)
		{
			list.Add("Maximum allowed value for Advertising_Interval_Min is 0x4000");
		}
		if (UserFields.AdvertisingIntervalMax < 32)
		{
			list.Add("Minimum allowed value for Advertising_Interval_Max is 0x20");
		}
		if (UserFields.AdvertisingIntervalMax > 16384)
		{
			list.Add("Maximum allowed value for Advertising_Interval_Max is 0x4000");
		}
		return list;
	}
}
