#ifndef FCITX5_GTK_WINDOW_WATCHER_H
#define FCITX5_GTK_WINDOW_WATCHER_H

#include <stdint.h>

struct ukui_window;
struct wl_display;

typedef struct _GdkWindow GdkWindow;
typedef struct _GdkDisplay GdkDisplay;

#ifdef __cplusplus
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>

#include <gtk/gtk.h>
#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/gdkwayland.h>
#endif

/**
 * @brief 窗口信息结构体
 * 存储窗口的位置、大小、句柄和状态信息
 */
struct WindowInfo {
    int32_t x;                          ///< 窗口X坐标
    int32_t y;                          ///< 窗口Y坐标
    uint32_t width;                     ///< 窗口宽度
    uint32_t height;                    ///< 窗口高度
    struct ukui_window* window_handle;  ///< 窗口句柄
    char uuid[256];                     ///< 窗口唯一标识符
    int is_active;                      ///< 窗口是否激活 (0=非激活, 1=激活)
};

/**
 * @brief 窗口几何信息结构体
 * 用于传递窗口的位置和大小信息
 */
struct WindowGeometry {
    int32_t x;          ///< 窗口X坐标
    int32_t y;          ///< 窗口Y坐标
    uint32_t width;     ///< 窗口宽度
    uint32_t height;    ///< 窗口高度
};

#endif // __cplusplus

#ifdef __cplusplus
extern "C" {
#endif

void* window_watcher_get_instance();
void initialize_window_watcher_from_gdk_window(GdkWindow* gdk_window);
struct WindowGeometry window_watcher_get_active_window_geometry();
void window_watcher_add_window(const char* uuid, struct ukui_window* window_handle);
void window_watcher_remove_window(struct ukui_window* window_handle);
void window_watcher_update_geometry(struct ukui_window* window_handle, 
                                   int32_t x, int32_t y, 
                                   uint32_t width, uint32_t height);
void window_watcher_update_state(struct ukui_window* window_handle, int is_active);
void window_watcher_clear_all();

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus

/**
 * @brief 窗口监视器类
 * 单例类，用于管理Wayland环境下的窗口信息
 * 提供窗口的添加、删除、更新和查询功能
 */
class WindowWatcher {
public:
    static WindowWatcher& getInstance();
    
    void initialize(GdkWindow* gdk_window);
    
    void addWindow(const std::string& uuid, struct ukui_window* window_handle);
    
    void removeWindow(struct ukui_window* window_handle);
    
    void updateWindowGeometry(struct ukui_window* window_handle, 
                             int32_t x, int32_t y, 
                             uint32_t width, uint32_t height);
    
    void updateWindowState(struct ukui_window* window_handle, bool is_active);
    
    WindowGeometry activeWindowGeometry() const;
 
    void clearAllWindows();

private:
    WindowWatcher() = default;
    ~WindowWatcher() = default;
    WindowWatcher(const WindowWatcher&) = delete;
    WindowWatcher& operator=(const WindowWatcher&) = delete;

private:
    std::unordered_map<struct ukui_window*, std::shared_ptr<WindowInfo>> window_map_;
    mutable std::mutex mutex_;
    bool wayland_initialized_ = false;
    GdkDisplay* gdk_display_ = nullptr;
    struct wl_display* wl_display_ = nullptr;
};

#endif // __cplusplus

#endif // FCITX5_GTK_WINDOW_WATCHER_H
