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

namespace QCRPG.TSL.BT.Packets;

public class LeSetExtendedScanParameters : PacketBase
{
	public class UserFieldsWrapper
	{
		public class ScanningPhysEnumCollection
		{
			private EnableBitEnum _ScanAdvertisementsOnTheLe1mPhy;

			private EnableBitEnum _ScanAdvertisementsOnTheLeCodedPhy;

			public EnableBitEnum ScanAdvertisementsOnTheLe1mPhy
			{
				get
				{
					return _ScanAdvertisementsOnTheLe1mPhy;
				}
				set
				{
					_ScanAdvertisementsOnTheLe1mPhy = value;
				}
			}

			public EnableBitEnum ScanAdvertisementsOnTheLeCodedPhy
			{
				get
				{
					return _ScanAdvertisementsOnTheLeCodedPhy;
				}
				set
				{
					_ScanAdvertisementsOnTheLeCodedPhy = value;
				}
			}
		}

		public static class ScanningPhysInfo
		{
			public static ScanningPhysEnumCollection ValueToScanningPhys(uint value)
			{
				ScanningPhysEnumCollection scanningPhysEnumCollection = new ScanningPhysEnumCollection();
				scanningPhysEnumCollection.ScanAdvertisementsOnTheLe1mPhy = EnableBitInfo.ValueToEnableBit(value & 1u);
				scanningPhysEnumCollection.ScanAdvertisementsOnTheLeCodedPhy = EnableBitInfo.ValueToEnableBit((value & 4) >> 2);
				return scanningPhysEnumCollection;
			}

			public static uint ScanningPhysToValue(ScanningPhysEnumCollection enumCollection)
			{
				uint num = 0u;
				num |= EnableBitInfo.EnableBitToValue(enumCollection.ScanAdvertisementsOnTheLe1mPhy);
				return num | (EnableBitInfo.EnableBitToValue(enumCollection.ScanAdvertisementsOnTheLeCodedPhy) << 2);
			}

			public static string ScanningPhysToString(ScanningPhysEnumCollection enumCollection)
			{
				string text = "";
				text = text + "Scan advertisements on the LE 1M PHY: " + EnableBitInfo.EnableBitToString(enumCollection.ScanAdvertisementsOnTheLe1mPhy) + "\r\n";
				text = text + "Scan advertisements on the LE Coded PHY: " + EnableBitInfo.EnableBitToString(enumCollection.ScanAdvertisementsOnTheLeCodedPhy) + "\r\n";
				return text.TrimEnd('\r', '\n');
			}

			public static string ScanningPhysToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("Bit 0: Scan advertisements on the LE 1M PHY");
				stringBuilder.AppendLine("Bit 1: Reserved for future use");
				stringBuilder.AppendLine("Bit 2: Scan advertisements on the LE Coded PHY");
				stringBuilder.AppendLine("Bits 7-3: Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _OwnAddressType;

		private uint _ScanningFilterPolicy;

		private uint _ScanningPhys = 1u;

		private List<ParametersSubField> _ParametersList = new List<ParametersSubField>();

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

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

		public uint ScanningPhys
		{
			get
			{
				return _ScanningPhys;
			}
			set
			{
				if (value > 255)
				{
					throw new Exception("Maximum possible value for ScanningPhys is 0xFF");
				}
				_ScanningPhys = value;
				uint num = 0u;
				if (_ScanningPhys == 1)
				{
					num = 1u;
				}
				else if (_ScanningPhys == 4)
				{
					num = 1u;
				}
				else if (_ScanningPhys == 5)
				{
					num = 2u;
				}
				while (_ParametersList.Count < num)
				{
					_ParametersList.Add(new ParametersSubField());
				}
				while (_ParametersList.Count > num)
				{
					_ParametersList.RemoveAt(_ParametersList.Count - 1);
				}
			}
		}

		public List<ParametersSubField> ParametersList => _ParametersList;

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

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

		public ScanningFilterPolicyEnum GetScanningFilterPolicyAsScanningFilterPolicyEnum()
		{
			return ScanningFilterPolicyInfo.ValueToScanningFilterPolicy(_ScanningFilterPolicy);
		}

		public void SetScanningFilterPolicyAsScanningFilterPolicyEnum(ScanningFilterPolicyEnum enumItem)
		{
			ScanningFilterPolicy = ScanningFilterPolicyInfo.ScanningFilterPolicyToValue(enumItem);
		}

		public ScanningPhysEnumCollection GetScanningPhysAsScanningPhysEnumCollection()
		{
			return ScanningPhysInfo.ValueToScanningPhys(_ScanningPhys);
		}

		public void SetScanningPhysAsScanningPhysEnumCollection(ScanningPhysEnumCollection enumCollection)
		{
			ScanningPhys = ScanningPhysInfo.ScanningPhysToValue(enumCollection);
		}
	}

	public class ParametersSubField
	{
		public enum ScanTypeEnum
		{
			Passive_Scanning_No_scan_request_PDUs_shall_be_sent,
			Active_scanning_Scan_request_PDUs_may_be_sent,
			Reserved_for_future_use,
			InvalidScanTypeValue
		}

		public static class ScanTypeInfo
		{
			public static ScanTypeEnum ValueToScanType(uint value)
			{
				switch (value)
				{
				case 0u:
					return ScanTypeEnum.Passive_Scanning_No_scan_request_PDUs_shall_be_sent;
				case 1u:
					return ScanTypeEnum.Active_scanning_Scan_request_PDUs_may_be_sent;
				default:
					if (value >= 2 && value <= 255)
					{
						return ScanTypeEnum.Reserved_for_future_use;
					}
					return ScanTypeEnum.InvalidScanTypeValue;
				}
			}

			public static uint ScanTypeToValue(ScanTypeEnum enumItem)
			{
				return enumItem switch
				{
					ScanTypeEnum.Passive_Scanning_No_scan_request_PDUs_shall_be_sent => 0u, 
					ScanTypeEnum.Active_scanning_Scan_request_PDUs_may_be_sent => 1u, 
					ScanTypeEnum.Reserved_for_future_use => 2u, 
					_ => uint.MaxValue, 
				};
			}

			public static string ScanTypeToString(ScanTypeEnum enumItem)
			{
				return enumItem switch
				{
					ScanTypeEnum.Passive_Scanning_No_scan_request_PDUs_shall_be_sent => "Passive Scanning. No scan request PDUs shall be sent.", 
					ScanTypeEnum.Active_scanning_Scan_request_PDUs_may_be_sent => "Active Scanning. Scan request PDUs may be sent.", 
					ScanTypeEnum.Reserved_for_future_use => "Reserved for future use", 
					_ => "Invalid ScanType Value", 
				};
			}

			public static string ScanTypeToDescription()
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine("0x0 = Passive Scanning. No scan request PDUs shall be sent.");
				stringBuilder.AppendLine("0x1 = Active Scanning. Scan request PDUs may be sent.");
				stringBuilder.AppendLine("0x2-0xFF = Reserved for future use");
				return stringBuilder.ToString();
			}
		}

		private uint _ScanType;

		private uint _ScanInterval = 4u;

		private uint _ScanWindow = 4u;

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

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

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

		public void SetDefaultValues()
		{
			ScanInterval = 4u;
			ScanWindow = 4u;
		}

		public ScanTypeEnum GetScanTypeAsScanTypeEnum()
		{
			return ScanTypeInfo.ValueToScanType(_ScanType);
		}

		public void SetScanTypeAsScanTypeEnum(ScanTypeEnum enumItem)
		{
			ScanType = ScanTypeInfo.ScanTypeToValue(enumItem);
		}

		public bool FromBitBuilder(ref BitBuilder bitBuilder)
		{
			ScanType = bitBuilder.RemoveBitsAsUint(8);
			ScanInterval = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			ScanWindow = bitBuilder.RemoveBitsAsUintLittleEndian(16);
			return true;
		}

		public string ToBitString()
		{
			BitBuilder bitBuilder = new BitBuilder();
			bitBuilder.AddBitsAsUint(ScanType, 8);
			bitBuilder.AddBitsAsUintLittleEndian(ScanInterval, 16);
			bitBuilder.AddBitsAsUintLittleEndian(ScanWindow, 16);
			return bitBuilder.ToString();
		}

		public List<string> GetWarnings()
		{
			List<string> list = new List<string>();
			if (ScanInterval < 4)
			{
				list.Add("Minimum allowed value for Scan_Interval is 0x4");
			}
			if (ScanInterval > 65535)
			{
				list.Add("Maximum allowed value for Scan_Interval is 0xFFFF");
			}
			if (ScanWindow < 4)
			{
				list.Add("Minimum allowed value for Scan_Window is 0x4");
			}
			if (ScanWindow > 65535)
			{
				list.Add("Maximum allowed value for Scan_Window is 0xFFFF");
			}
			return list;
		}
	}

	private const uint _Opcode = 8257u;

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

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

	public uint Opcode => 8257u;

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

	public UserFieldsWrapper UserFields => _UserFields;

	public override void SetDefaultValues()
	{
		UserFields.ScanningPhys = 1u;
		for (int i = 0; i < UserFields.ParametersList.Count; i++)
		{
			UserFields.ParametersList[i].SetDefaultValues();
		}
	}

	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.OwnAddressType = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ScanningFilterPolicy = bitBuilder.RemoveBitsAsUint(8);
		UserFields.ScanningPhys = bitBuilder.RemoveBitsAsUint(8);
		for (int i = 0; i < UserFields.ParametersList.Count; i++)
		{
			UserFields.ParametersList[i].FromBitBuilder(ref bitBuilder);
		}
		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.OwnAddressType, 8);
		bitBuilder.AddBitsAsUint(UserFields.ScanningFilterPolicy, 8);
		bitBuilder.AddBitsAsUint(UserFields.ScanningPhys, 8);
		for (int i = 0; i < UserFields.ParametersList.Count; i++)
		{
			bitBuilder.AddBitsAsString(UserFields.ParametersList[i].ToBitString());
		}
		_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.ScanningPhys < 1)
		{
			list.Add("Minimum allowed value for Scanning_PHYs is 0x1");
		}
		for (int i = 0; i < UserFields.ParametersList.Count; i++)
		{
			list.AddRange(UserFields.ParametersList[i].GetWarnings());
		}
		return list;
	}
}
