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

// DirectX 11 Rendering Context for Windows
// Uses DXGI swap chain with premultiplied alpha + DirectComposition
// for true per-pixel transparency with DWM Mica/Acrylic backdrops.

#pragma once

#ifdef _WIN32

#include <d3d11.h>
#include <dxgi1_2.h>
#include <cstdio>

// Forward declarations - DirectComposition interfaces
struct IDCompositionDevice;
struct IDCompositionTarget;
struct IDCompositionVisual;

struct SDL_Window;

namespace dragonx {
namespace platform {

class DX11Context {
public:
    DX11Context() = default;
    ~DX11Context() { shutdown(); }

    // Non-copyable
    DX11Context(const DX11Context&) = delete;
    DX11Context& operator=(const DX11Context&) = delete;

    /**
     * @brief Initialize D3D11 device and DXGI swap chain with alpha support
     * @param window SDL window to create the swap chain for
     * @return true on success
     */
    bool init(SDL_Window* window);

    /**
     * @brief Shut down and release all D3D11/DXGI resources
     */
    void shutdown();

    /**
     * @brief Resize the swap chain buffers (call on window resize)
     * @param width New width (0 = auto-detect from swap chain)
     * @param height New height (0 = auto-detect from swap chain)
     */
    void resize(int width, int height);

    /**
     * @brief Ensure swap chain matches the current HWND client size.
     * Call once per frame to guarantee the buffers stay in sync.
     * No-op if the sizes already match.
     */
    void ensureSize();

    /**
     * @brief Clear the render target with a color
     * @param r Red (0-1)
     * @param g Green (0-1)
     * @param b Blue (0-1)
     * @param a Alpha (0-1), 0 = fully transparent for DWM
     */
    void clear(float r, float g, float b, float a);

    /**
     * @brief Present the frame (swap buffers)
     * @param vsync 1 = vsync on, 0 = vsync off
     */
    void present(int vsync = 1);

    /** @brief Whether premultiplied alpha compositing is active */
    bool hasAlphaCompositing() const { return hasAlpha_; }

    // Accessors
    ID3D11Device* device() const { return device_; }
    ID3D11DeviceContext* deviceContext() const { return deviceContext_; }
    ID3D11RenderTargetView* renderTargetView() const { return renderTargetView_; }
    IDXGISwapChain1* swapChain() const { return swapChain_; }

private:
    void createRenderTarget();
    void cleanupRenderTarget();

    // D3D11 core
    ID3D11Device*            device_ = nullptr;
    ID3D11DeviceContext*     deviceContext_ = nullptr;
    IDXGISwapChain1*         swapChain_ = nullptr;
    ID3D11RenderTargetView*  renderTargetView_ = nullptr;

    // DirectComposition (for premultiplied alpha swap chain presentation)
    IDCompositionDevice*     dcompDevice_ = nullptr;
    IDCompositionTarget*     dcompTarget_ = nullptr;
    IDCompositionVisual*     dcompVisual_ = nullptr;

    bool                     hasAlpha_ = false;
    HWND                     hwnd_ = nullptr;
};

} // namespace platform
} // namespace dragonx

#endif // _WIN32
