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

namespace QCRPG.TSL.BT.Packets;

public class HeadlessSetLeAdvertisingData : PacketBase
{
	public class UserFieldsWrapper
	{
		private const uint _SubOpcode = 6u;

		private uint _AdvIntervalMin = 32u;

		private uint _AdvIntervalMax = 32u;

		private uint _AdvType;

		private uint _OwnAddrType;

		private uint _PeerAddrType;

		private ulong _PeerAddr;

		private uint _AdvChannelMap;

		private uint _AdvFilterPolicy;

		private uint _AdvDataLength;

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

		public uint SubOpcode => 6u;

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

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

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

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

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

		public ulong PeerAddr
		{
			get
			{
				return _PeerAddr;
			}
			set
			{
				_PeerAddr = value;
			}
		}

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

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

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

		public List<uint> AdvertisingDataList => _AdvertisingDataList;

		public AdvertisingTypeEnum GetAdvTypeAsAdvertisingTypeEnum()
		{
			return AdvertisingTypeInfo.ValueToAdvertisingType(_AdvType);
		}

		public void SetAdvTypeAsAdvertisingTypeEnum(AdvertisingTypeEnum enumItem)
		{
			AdvType = AdvertisingTypeInfo.AdvertisingTypeToValue(enumItem);
		}

		public OwnAddressTypeEnum GetOwnAddrTypeAsOwnAddressTypeEnum()
		{
			return OwnAddressTypeInfo.ValueToOwnAddressType(_OwnAddrType);
		}

		public void SetOwnAddrTypeAsOwnAddressTypeEnum(OwnAddressTypeEnum enumItem)
		{
			OwnAddrType = OwnAddressTypeInfo.OwnAddressTypeToValue(enumItem);
		}

		public PeerAddressTypeEnum GetPeerAddrTypeAsPeerAddressTypeEnum()
		{
			return PeerAddressTypeInfo.ValueToPeerAddressType(_PeerAddrType);
		}

		public void SetPeerAddrTypeAsPeerAddressTypeEnum(PeerAddressTypeEnum enumItem)
		{
			PeerAddrType = PeerAddressTypeInfo.PeerAddressTypeToValue(enumItem);
		}

		public AdvertisingChannelMapEnumCollection GetAdvChannelMapAsAdvertisingChannelMapEnumCollection()
		{
			return AdvertisingChannelMapInfo.ValueToAdvertisingChannelMap(_AdvChannelMap);
		}

		public void SetAdvChannelMapAsAdvertisingChannelMapEnumCollection(AdvertisingChannelMapEnumCollection enumCollection)
		{
			AdvChannelMap = AdvertisingChannelMapInfo.AdvertisingChannelMapToValue(enumCollection);
		}

		public AdvertisingFilterPolicyEnum GetAdvFilterPolicyAsAdvertisingFilterPolicyEnum()
		{
			return AdvertisingFilterPolicyInfo.ValueToAdvertisingFilterPolicy(_AdvFilterPolicy);
		}

		public void SetAdvFilterPolicyAsAdvertisingFilterPolicyEnum(AdvertisingFilterPolicyEnum enumItem)
		{
			AdvFilterPolicy = AdvertisingFilterPolicyInfo.AdvertisingFilterPolicyToValue(enumItem);
		}
	}

	private const uint _Opcode = 64566u;

	private Type[] expectedResponseTypes = new Type[2]
	{
		typeof(CommandCompleteForHeadlessSetLeAdvertisingData),
		typeof(CommandCompleteForNop)
	};

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

	public uint Opcode => 64566u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.AdvIntervalMin = 2048u;
		UserFields.AdvIntervalMax = 2048u;
		UserFields.AdvChannelMap = 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;
		}
		if (bitBuilder.RemoveBitsAsUint(8) != UserFields.SubOpcode)
		{
			return false;
		}
		UserFields.AdvIntervalMin = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.AdvIntervalMax = bitBuilder.RemoveBitsAsUintLittleEndian(16);
		UserFields.AdvType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.OwnAddrType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PeerAddrType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.PeerAddr = bitBuilder.RemoveBitsAsUlongLittleEndian(48);
		UserFields.AdvChannelMap = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AdvFilterPolicy = bitBuilder.RemoveBitsAsUint(8);
		UserFields.AdvDataLength = 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.SubOpcode, 8);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.AdvIntervalMin, 16);
		bitBuilder.AddBitsAsUintLittleEndian(UserFields.AdvIntervalMax, 16);
		bitBuilder.AddBitsAsUint(UserFields.AdvType, 8);
		bitBuilder.AddBitsAsUint(UserFields.OwnAddrType, 8);
		bitBuilder.AddBitsAsUint(UserFields.PeerAddrType, 8);
		bitBuilder.AddBitsAsUlongLittleEndian(UserFields.PeerAddr, 48);
		bitBuilder.AddBitsAsUint(UserFields.AdvChannelMap, 8);
		bitBuilder.AddBitsAsUint(UserFields.AdvFilterPolicy, 8);
		bitBuilder.AddBitsAsUint(UserFields.AdvDataLength, 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.AdvIntervalMin < 32)
		{
			list.Add("Minimum allowed value for Adv_Interval_Min is 0x20");
		}
		if (UserFields.AdvIntervalMin > 16384)
		{
			list.Add("Maximum allowed value for Adv_Interval_Min is 0x4000");
		}
		if (UserFields.AdvIntervalMax < 32)
		{
			list.Add("Minimum allowed value for Adv_Interval_Max is 0x20");
		}
		if (UserFields.AdvIntervalMax > 16384)
		{
			list.Add("Maximum allowed value for Adv_Interval_Max is 0x4000");
		}
		return list;
	}
}
