using System.Runtime.InteropServices;

namespace Ehwrj.App.Infrastructure;

internal static class Win32
{
    private const int GwlExStyle = -20;
    private const int WsExTransparent = 0x00000020;
    private const int WsExToolWindow = 0x00000080;
    private const int WsExLayered = 0x00080000;
    private const int WsExNoActivate = 0x08000000;

    public static void MakeOverlayClickThrough(IntPtr hwnd)
    {
        if (!OperatingSystem.IsWindows())
        {
            return;
        }

        var style = GetWindowLongPtr(hwnd, GwlExStyle);
        style |= WsExTransparent | WsExToolWindow | WsExLayered | WsExNoActivate;
        _ = SetWindowLongPtr(hwnd, GwlExStyle, style);
    }

    [DllImport("user32.dll", EntryPoint = "GetWindowLongPtrW", SetLastError = true)]
    private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW", SetLastError = true)]
    private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
}
