// DragonX Wallet - ImGui Edition
// Copyright 2024-2026 The Hush Developers
// Released under the GPLv3

#include "windows_backdrop.h"

#ifdef _WIN32

#include <windows.h>
#include <dwmapi.h>
#include <SDL3/SDL.h>
#include <cstdio>
#include "../util/logger.h"

// Link with dwmapi.lib
#pragma comment(lib, "dwmapi.lib")

// DWM attribute values not in older SDK headers
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif

#ifndef DWMWA_SYSTEMBACKDROP_TYPE
#define DWMWA_SYSTEMBACKDROP_TYPE 38
#endif

#ifndef DWMWA_MICA_EFFECT
#define DWMWA_MICA_EFFECT 1029
#endif

// System backdrop types (Windows 11 22H2+)
typedef enum {
    DWMSBT_AUTO = 0,
    DWMSBT_NONE = 1,
    DWMSBT_MAINWINDOW = 2,      // Mica
    DWMSBT_TRANSIENTWINDOW = 3, // Acrylic
    DWMSBT_TABBEDWINDOW = 4     // Mica Alt
} DWM_SYSTEMBACKDROP_TYPE;

namespace dragonx {
namespace platform {

// Helper to get HWND from SDL window
static HWND getHWND(SDL_Window* window) {
    if (!window) return nullptr;
    
    SDL_PropertiesID props = SDL_GetWindowProperties(window);
    if (props == 0) return nullptr;
    
    return (HWND)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
}

WindowsVersionInfo getWindowsVersion() {
    WindowsVersionInfo info = {};
    
    // Use RtlGetVersion to get the real version (GetVersionEx is deprecated/lies)
    typedef NTSTATUS (WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
    
    HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
    if (ntdll) {
        RtlGetVersionPtr rtlGetVersion = (RtlGetVersionPtr)GetProcAddress(ntdll, "RtlGetVersion");
        if (rtlGetVersion) {
            RTL_OSVERSIONINFOW osvi = {};
            osvi.dwOSVersionInfoSize = sizeof(osvi);
            if (rtlGetVersion(&osvi) == 0) {
                info.major = osvi.dwMajorVersion;
                info.minor = osvi.dwMinorVersion;
                info.build = osvi.dwBuildNumber;
            }
        }
    }
    
    info.isWindows11 = (info.major >= 10 && info.build >= 22000);
    info.isWindows10 = (info.major >= 10 && info.build >= 10240);
    
    return info;
}

bool isWindowsBackdropAvailable() {
    WindowsVersionInfo ver = getWindowsVersion();
    // Backdrop effects require at least Windows 10
    return ver.isWindows10;
}

bool enableWindowsBackdrop(SDL_Window* window, WindowsBackdrop type) {
    HWND hwnd = getHWND(window);
    if (!hwnd) {
        DEBUG_LOGF("WindowsBackdrop: Failed to get HWND\n");
        return false;
    }
    
    WindowsVersionInfo ver = getWindowsVersion();
    DEBUG_LOGF("WindowsBackdrop: Windows %d.%d.%d detected\n", ver.major, ver.minor, ver.build);
    
    if (!ver.isWindows10) {
        DEBUG_LOGF("WindowsBackdrop: Windows 10+ required\n");
        return false;
    }
    
    // Enable dark mode for title bar (looks better with acrylic)
    BOOL useDarkMode = TRUE;
    DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &useDarkMode, sizeof(useDarkMode));
    
    // Auto-detect best backdrop type
    if (type == WindowsBackdrop::Auto) {
        if (ver.isWindows11 && ver.build >= 22621) {
            // Windows 11 22H2+ - use Mica Alt for dark themed apps
            type = WindowsBackdrop::MicaAlt;
        } else if (ver.isWindows11) {
            // Windows 11 21H2 - use Mica
            type = WindowsBackdrop::Mica;
        } else {
            // Windows 10 - use Acrylic
            type = WindowsBackdrop::Acrylic;
        }
    }
    
    // Extend DWM frame into entire client area - required for all backdrop types
    // This lets the DWM composition show through where we render with alpha = 0
    MARGINS margins = {-1, -1, -1, -1};
    DwmExtendFrameIntoClientArea(hwnd, &margins);
    
    HRESULT hr = S_OK;
    
    if (ver.build >= 22621) {
        // Windows 11 22H2+ - use DWMWA_SYSTEMBACKDROP_TYPE
        DWM_SYSTEMBACKDROP_TYPE backdrop;
        switch (type) {
            case WindowsBackdrop::Mica:
                backdrop = DWMSBT_MAINWINDOW;
                DEBUG_LOGF("WindowsBackdrop: Enabling Mica\n");
                break;
            case WindowsBackdrop::MicaAlt:
                backdrop = DWMSBT_TABBEDWINDOW;
                DEBUG_LOGF("WindowsBackdrop: Enabling Mica Alt\n");
                break;
            case WindowsBackdrop::Acrylic:
                backdrop = DWMSBT_TRANSIENTWINDOW;
                DEBUG_LOGF("WindowsBackdrop: Enabling Acrylic\n");
                break;
            default:
                backdrop = DWMSBT_NONE;
                break;
        }
        hr = DwmSetWindowAttribute(hwnd, DWMWA_SYSTEMBACKDROP_TYPE, &backdrop, sizeof(backdrop));
    }
    else if (ver.isWindows11) {
        // Windows 11 21H2 - use legacy DWMWA_MICA_EFFECT
        BOOL enableMica = (type == WindowsBackdrop::Mica || type == WindowsBackdrop::MicaAlt);
        hr = DwmSetWindowAttribute(hwnd, DWMWA_MICA_EFFECT, &enableMica, sizeof(enableMica));
        DEBUG_LOGF("WindowsBackdrop: Enabling Mica (legacy API)\n");
    }
    else {
        // Windows 10 - frame already extended above, basic DWM transparency
        // Note: Full acrylic on Win10 requires undocumented APIs
        DEBUG_LOGF("WindowsBackdrop: Using extended frame (Win10)\n");
    }
    
    if (SUCCEEDED(hr)) {
        DEBUG_LOGF("WindowsBackdrop: Successfully enabled\n");
        return true;
    } else {
        DEBUG_LOGF("WindowsBackdrop: DWM call failed with HRESULT 0x%08lx\n", hr);
        return false;
    }
}

void disableWindowsBackdrop(SDL_Window* window) {
    HWND hwnd = getHWND(window);
    if (!hwnd) return;
    
    DWM_SYSTEMBACKDROP_TYPE backdrop = DWMSBT_NONE;
    DwmSetWindowAttribute(hwnd, DWMWA_SYSTEMBACKDROP_TYPE, &backdrop, sizeof(backdrop));
    
    MARGINS margins = {0, 0, 0, 0};
    DwmExtendFrameIntoClientArea(hwnd, &margins);
}

} // namespace platform
} // namespace dragonx

#endif // _WIN32
