using System;
using System.Runtime.InteropServices;

namespace QC.MMI.MMI_Common;

public static class WifiAPI
{
	public struct ListHeader
	{
		public uint numberOfItems;

		public uint index;
	}

	[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
	public struct WlanInterfaceInfo
	{
		public Guid interfaceGuid;

		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
		public string interfaceDescription;

		public uint interfaceState;
	}

	public struct Dot11_SSID
	{
		public uint SSID_Num;

		[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
		public byte[] SSID;
	}

	[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
	public struct WlanAvailableNetwork
	{
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
		public string profileName;

		public Dot11_SSID dot11Ssid;

		public uint dot11BssType;

		public uint numberOfBssids;

		public bool networkConnectable;

		public uint wlanNotConnectableReason;

		private uint numberOfPhyTypes;

		[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
		private uint[] dot11PhyTypes;

		public bool bMorePhyTypes;

		public uint wlanSignalQuality;

		public bool securityEnabled;

		public uint dot11DefaultAuthAlgorithm;

		public uint dot11DefaultCipherAlgorithm;

		public uint flags;

		private uint reserved;

		public uint[] Dot11PhyTypes
		{
			get
			{
				uint[] array = new uint[numberOfPhyTypes];
				Array.Copy(dot11PhyTypes, array, numberOfPhyTypes);
				return array;
			}
		}
	}

	[Flags]
	public enum WlanNotificationSource
	{
		None = 0,
		All = 0xFFFF,
		ACM = 8,
		MSM = 0x10,
		Security = 0x20,
		IHV = 0x40
	}

	public struct WlanNotificationData
	{
		public WlanNotificationSource notifySource;

		public int notifyCode;

		public Guid interfaceGuid;

		public int dataSize;

		public IntPtr pData;

		public object NotificationCode => notifyCode;
	}

	public delegate void WlanNotificationCallbackDelegate(ref WlanNotificationData notificationData, IntPtr context);

	public const uint WINDOWS_VERSION_XP_SP2 = 1u;

	public const uint WINDOWS_VERSION_WIN7 = 2u;

	public const uint WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_ADHOC_PROFILES = 1u;

	[DllImport("wlanapi.dll")]
	public static extern int WlanOpenHandle([In] uint windowsVersion, [In][Out] IntPtr pReserved, out uint negotiatedVersion, out IntPtr clientHandle);

	[DllImport("wlanapi.dll")]
	public static extern int WlanCloseHandle([In] IntPtr clientHandle, [In][Out] IntPtr pReserved);

	[DllImport("wlanapi.dll")]
	public static extern int WlanEnumInterfaces([In] IntPtr clientHandle, [In][Out] IntPtr pReserved, out IntPtr ppInterfaceList);

	[DllImport("wlanapi.dll")]
	public static extern void WlanFreeMemory(IntPtr pMemory);

	[DllImport("wlanapi.dll")]
	public static extern int WlanScan([In] IntPtr clientHandle, [In][MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid, [In] IntPtr pDot11Ssid, [In] IntPtr pIeData, [In][Out] IntPtr pReserved);

	[DllImport("wlanapi.dll")]
	public static extern int WlanRegisterNotification([In] IntPtr clientHandle, [In] WlanNotificationSource notifySource, [In] bool ignoreDuplicate, [In] WlanNotificationCallbackDelegate funcCallback, [In] IntPtr callbackContext, [In] IntPtr reserved, out WlanNotificationSource prevNotifySource);

	[DllImport("wlanapi.dll")]
	public static extern int WlanGetAvailableNetworkList([In] IntPtr clientHandle, [In][MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid, [In] uint flags, [In][Out] IntPtr pReserved, out IntPtr pAvailableNetworkList);

	[DllImport("wlanapi.dll")]
	public static extern int WlanGetNetworkBssList([In] IntPtr clientHandle, [In][MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid, [In] IntPtr pDot11Ssid, [In] uint dot11BssType, [In] bool securityEnabled, IntPtr pReserved, out IntPtr pWlanBssList);
}
