/*
 * DWN - Desktop Window Manager
 * Core Abstract Types
 * Extreme Abstraction Architecture
 */

#ifndef WM_TYPES_H
#define WM_TYPES_H

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

/*==============================================================================
 * Version and API Information
 *============================================================================*/

#define WM_CORE_VERSION_MAJOR 2
#define WM_CORE_VERSION_MINOR 0
#define WM_CORE_VERSION_PATCH 0

#define WM_PLUGIN_API_VERSION 1

/*==============================================================================
 * Opaque Type Declarations
 *============================================================================*/

typedef struct WmCore WmCore;
typedef struct WmBackend WmBackend;
typedef struct WmPlugin WmPlugin;
typedef struct WmPluginManager WmPluginManager;
typedef struct WmEventBus WmEventBus;
typedef struct WmEventSubscription WmEventSubscription;
typedef struct WmContainer WmContainer;
typedef struct WmIterator WmIterator;
typedef struct WmString WmString;
typedef struct WmRenderer WmRenderer;
typedef struct WmSurface WmSurface;
typedef struct WmFont WmFont;
typedef struct WmImage WmImage;
typedef struct WmCommand WmCommand;
typedef struct WmCommandBuilder WmCommandBuilder;
typedef struct WmConfigSchema WmConfigSchema;
typedef struct WmConfigInstance WmConfigInstance;
typedef struct WmLayoutEngine WmLayoutEngine;
typedef struct WmPanelSystem WmPanelSystem;
typedef struct WmAnimation WmAnimation;
typedef struct WmTimer WmTimer;
typedef struct WmMutex WmMutex;
typedef struct WmCondition WmCondition;
typedef struct WmThread WmThread;

/*==============================================================================
 * Basic Type Definitions
 *============================================================================*/

typedef uint32_t WmColor;
typedef uint64_t WmTime;
typedef uint32_t WmHandle;
typedef uint32_t WmId;
typedef WmId WmClientId;
typedef int WmWorkspaceId;
typedef void* WmWindowHandle;
typedef void* WmNativeHandle;

/*==============================================================================
 * Window Types
 *============================================================================*/

typedef enum {
    WM_WINDOW_TYPE_UNKNOWN = 0,
    WM_WINDOW_TYPE_NORMAL,
    WM_WINDOW_TYPE_DIALOG,
    WM_WINDOW_TYPE_DOCK,
    WM_WINDOW_TYPE_DESKTOP,
    WM_WINDOW_TYPE_TOOLBAR,
    WM_WINDOW_TYPE_MENU,
    WM_WINDOW_TYPE_UTILITY,
    WM_WINDOW_TYPE_SPLASH,
    WM_WINDOW_TYPE_NOTIFICATION,
    WM_WINDOW_TYPE_COMBO,
    WM_WINDOW_TYPE_DND,
    WM_WINDOW_TYPE_POPUP_MENU,
    WM_WINDOW_TYPE_TOOLTIP
} WmWindowType;

/*==============================================================================
 * Geometry Types
 *============================================================================*/

typedef struct {
    int x;
    int y;
} WmPoint;

typedef struct {
    int width;
    int height;
} WmSize;

typedef struct {
    int x;
    int y;
    int width;
    int height;
} WmRect;

typedef struct {
    WmPoint location;
    WmSize size;
} WmGeometry;

typedef struct {
    int top;
    int bottom;
    int left;
    int right;
} WmInsets;

typedef struct {
    float x;
    float y;
} WmPointF;

typedef struct {
    float width;
    float height;
} WmSizeF;

typedef struct {
    WmPointF location;
    WmSizeF size;
} WmGeometryF;

/*==============================================================================
 * Rectangle Operations (Inline)
 *============================================================================*/

static inline WmRect wm_rect_make(int x, int y, int width, int height) {
    WmRect r = { x, y, width, height };
    return r;
}

static inline bool wm_rect_is_empty(const WmRect *r) {
    return r == NULL || r->width <= 0 || r->height <= 0;
}

static inline bool wm_rect_contains_point(const WmRect *r, int x, int y) {
    return r != NULL && 
           x >= r->x && x < r->x + r->width &&
           y >= r->y && y < r->y + r->height;
}

static inline bool wm_rect_intersects(const WmRect *a, const WmRect *b) {
    if (a == NULL || b == NULL) return false;
    return !(a->x + a->width <= b->x ||
             b->x + b->width <= a->x ||
             a->y + a->height <= b->y ||
             b->y + b->height <= a->y);
}

static inline WmRect wm_rect_intersection(const WmRect *a, const WmRect *b) {
    WmRect r = { 0, 0, 0, 0 };
    if (a == NULL || b == NULL) return r;
    
    int x1 = a->x > b->x ? a->x : b->x;
    int y1 = a->y > b->y ? a->y : b->y;
    int x2 = (a->x + a->width) < (b->x + b->width) ? (a->x + a->width) : (b->x + b->width);
    int y2 = (a->y + a->height) < (b->y + b->height) ? (a->y + a->height) : (b->y + b->height);
    
    if (x2 > x1 && y2 > y1) {
        r.x = x1;
        r.y = y1;
        r.width = x2 - x1;
        r.height = y2 - y1;
    }
    return r;
}

static inline WmRect wm_rect_union(const WmRect *a, const WmRect *b) {
    WmRect r = { 0, 0, 0, 0 };
    if (a == NULL && b == NULL) return r;
    if (a == NULL) return *b;
    if (b == NULL) return *a;
    
    int x1 = a->x < b->x ? a->x : b->x;
    int y1 = a->y < b->y ? a->y : b->y;
    int x2 = (a->x + a->width) > (b->x + b->width) ? (a->x + a->width) : (b->x + b->width);
    int y2 = (a->y + a->height) > (b->y + b->height) ? (a->y + a->height) : (b->y + b->height);
    
    r.x = x1;
    r.y = y1;
    r.width = x2 - x1;
    r.height = y2 - y1;
    return r;
}

/*==============================================================================
 * Color Operations (Inline)
 *============================================================================*/

static inline WmColor wm_color_rgb(uint8_t r, uint8_t g, uint8_t b) {
    return (0xFF << 24) | (r << 16) | (g << 8) | b;
}

static inline WmColor wm_color_rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
    return (a << 24) | (r << 16) | (g << 8) | b;
}

static inline uint8_t wm_color_get_red(WmColor c) {
    return (c >> 16) & 0xFF;
}

static inline uint8_t wm_color_get_green(WmColor c) {
    return (c >> 8) & 0xFF;
}

static inline uint8_t wm_color_get_blue(WmColor c) {
    return c & 0xFF;
}

static inline uint8_t wm_color_get_alpha(WmColor c) {
    return (c >> 24) & 0xFF;
}

/*==============================================================================
 * Client State Flags
 *============================================================================*/

typedef enum {
    WM_CLIENT_FLAG_NONE        = 0,
    WM_CLIENT_FLAG_FLOATING    = (1 << 0),
    WM_CLIENT_FLAG_FULLSCREEN  = (1 << 1),
    WM_CLIENT_FLAG_URGENT      = (1 << 2),
    WM_CLIENT_FLAG_MINIMIZED   = (1 << 3),
    WM_CLIENT_FLAG_STICKY      = (1 << 4),
    WM_CLIENT_FLAG_MAXIMIZED   = (1 << 5),
    WM_CLIENT_FLAG_UNMANAGING  = (1 << 6),
    WM_CLIENT_FLAG_FOCUSED     = (1 << 7),
    WM_CLIENT_FLAG_MAPPED      = (1 << 8),
    WM_CLIENT_FLAG_DECORATED   = (1 << 9),
    WM_CLIENT_FLAG_DIALOG      = (1 << 10),
    WM_CLIENT_FLAG_DOCK        = (1 << 11),
    WM_CLIENT_FLAG_UTILITY     = (1 << 12),
    WM_CLIENT_FLAG_SPLASH      = (1 << 13),
    WM_CLIENT_FLAG_MENU        = (1 << 14),
    WM_CLIENT_FLAG_TOOLBAR     = (1 << 15)
} WmClientFlags;

/*==============================================================================
 * Layout Types
 *============================================================================*/

typedef enum {
    WM_LAYOUT_TYPE_TILING,
    WM_LAYOUT_TYPE_FLOATING,
    WM_LAYOUT_TYPE_MONOCLE,
    WM_LAYOUT_TYPE_GRID,
    WM_LAYOUT_TYPE_SPIRAL,
    WM_LAYOUT_TYPE_DWINDLE,
    WM_LAYOUT_TYPE_MAX
} WmLayoutType;

/*==============================================================================
 * Panel Positions
 *============================================================================*/

typedef enum {
    WM_PANEL_POSITION_TOP,
    WM_PANEL_POSITION_BOTTOM,
    WM_PANEL_POSITION_LEFT,
    WM_PANEL_POSITION_RIGHT
} WmPanelPosition;

/*==============================================================================
 * Event Types
 *============================================================================*/

typedef enum {
    /* Window events */
    WM_EVENT_NONE = 0,
    WM_EVENT_WINDOW_CREATED,
    WM_EVENT_WINDOW_DESTROYED,
    WM_EVENT_WINDOW_MAPPED,
    WM_EVENT_WINDOW_UNMAPPED,
    WM_EVENT_WINDOW_CONFIGURED,
    WM_EVENT_WINDOW_FOCUSED,
    WM_EVENT_WINDOW_UNFOCUSED,
    WM_EVENT_WINDOW_RAISED,
    WM_EVENT_WINDOW_LOWERED,
    WM_EVENT_WINDOW_MINIMIZED,
    WM_EVENT_WINDOW_RESTORED,
    WM_EVENT_WINDOW_MAXIMIZED,
    WM_EVENT_WINDOW_UNMAXIMIZED,
    WM_EVENT_WINDOW_FULLSCREENED,
    WM_EVENT_WINDOW_UNFULLSCREENED,
    WM_EVENT_WINDOW_FLOATING_CHANGED,
    WM_EVENT_WINDOW_PROPERTY_CHANGED,
    WM_EVENT_WINDOW_STATE_CHANGED,
    WM_EVENT_WINDOW_TITLE_CHANGED,
    WM_EVENT_WINDOW_CLASS_CHANGED,
    WM_EVENT_WINDOW_ROLE_CHANGED,
    WM_EVENT_WINDOW_URGENCY_CHANGED,
    WM_EVENT_WINDOW_MOVED,
    WM_EVENT_WINDOW_RESIZED,
    
    /* Workspace events */
    WM_EVENT_WORKSPACE_CREATED,
    WM_EVENT_WORKSPACE_DESTROYED,
    WM_EVENT_WORKSPACE_SWITCHED,
    WM_EVENT_WORKSPACE_RENAMED,
    WM_EVENT_WORKSPACE_LAYOUT_CHANGED,
    WM_EVENT_WORKSPACE_MASTER_RATIO_CHANGED,
    WM_EVENT_WORKSPACE_MASTER_COUNT_CHANGED,
    
    /* Client/workspace relationship */
    WM_EVENT_CLIENT_ADDED_TO_WORKSPACE,
    WM_EVENT_CLIENT_REMOVED_FROM_WORKSPACE,
    
    /* Input events */
    WM_EVENT_KEY_PRESSED,
    WM_EVENT_KEY_RELEASED,
    WM_EVENT_BUTTON_PRESSED,
    WM_EVENT_BUTTON_RELEASED,
    WM_EVENT_MOTION,
    WM_EVENT_ENTER,
    WM_EVENT_LEAVE,
    WM_EVENT_SCROLL,
    
    /* System events */
    WM_EVENT_MONITOR_ADDED,
    WM_EVENT_MONITOR_REMOVED,
    WM_EVENT_MONITOR_CONFIGURED,
    WM_EVENT_SCREEN_RESIZED,
    
    /* Configuration events */
    WM_EVENT_CONFIG_RELOADED,
    WM_EVENT_CONFIG_CHANGED,
    
    /* Lifecycle events */
    WM_EVENT_WM_STARTED,
    WM_EVENT_WM_SHUTDOWN,
    WM_EVENT_WM_READY,
    
    /* Plugin events */
    WM_EVENT_PLUGIN_LOADED,
    WM_EVENT_PLUGIN_UNLOADED,
    WM_EVENT_PLUGIN_ERROR,
    
    /* Custom events for plugins */
    WM_EVENT_CUSTOM = 1000
} WmEventType;

/*==============================================================================
 * Container Types
 *============================================================================*/

typedef enum {
    WM_CONTAINER_LIST,
    WM_CONTAINER_ARRAY,
    WM_CONTAINER_HASHMAP,
    WM_CONTAINER_QUEUE,
    WM_CONTAINER_STACK,
    WM_CONTAINER_TREE
} WmContainerType;

/*==============================================================================
 * Command Types
 *============================================================================*/

typedef enum {
    WM_COMMAND_NONE = 0,
    
    /* Window commands */
    WM_COMMAND_WINDOW_FOCUS,
    WM_COMMAND_WINDOW_MOVE,
    WM_COMMAND_WINDOW_RESIZE,
    WM_COMMAND_WINDOW_MOVE_RESIZE,
    WM_COMMAND_WINDOW_CLOSE,
    WM_COMMAND_WINDOW_KILL,
    WM_COMMAND_WINDOW_MINIMIZE,
    WM_COMMAND_WINDOW_RESTORE,
    WM_COMMAND_WINDOW_MAXIMIZE,
    WM_COMMAND_WINDOW_UNMAXIMIZE,
    WM_COMMAND_WINDOW_FULLSCREEN,
    WM_COMMAND_WINDOW_UNFULLSCREEN,
    WM_COMMAND_WINDOW_FLOAT,
    WM_COMMAND_WINDOW_UNFLOAT,
    WM_COMMAND_WINDOW_RAISE,
    WM_COMMAND_WINDOW_LOWER,
    WM_COMMAND_WINDOW_SET_WORKSPACE,
    
    /* Workspace commands */
    WM_COMMAND_WORKSPACE_SWITCH,
    WM_COMMAND_WORKSPACE_CREATE,
    WM_COMMAND_WORKSPACE_DESTROY,
    WM_COMMAND_WORKSPACE_RENAME,
    WM_COMMAND_WORKSPACE_SET_LAYOUT,
    WM_COMMAND_WORKSPACE_ADJUST_MASTER_RATIO,
    WM_COMMAND_WORKSPACE_ADJUST_MASTER_COUNT,
    WM_COMMAND_WORKSPACE_SWAP,
    
    /* Client manipulation */
    WM_COMMAND_CLIENT_SWAP,
    WM_COMMAND_CLIENT_SWAP_MASTER,
    WM_COMMAND_CLIENT_CYCLE,
    WM_COMMAND_CLIENT_CYCLE_REVERSE,
    
    /* System commands */
    WM_COMMAND_RELOAD_CONFIG,
    WM_COMMAND_QUIT,
    WM_COMMAND_RESTART,
    
    /* Plugin commands */
    WM_COMMAND_PLUGIN_LOAD,
    WM_COMMAND_PLUGIN_UNLOAD,
    WM_COMMAND_PLUGIN_ENABLE,
    WM_COMMAND_PLUGIN_DISABLE
} WmCommandType;

/*==============================================================================
 * Result/Error Types
 *============================================================================*/

typedef enum {
    WM_RESULT_OK = 0,
    WM_RESULT_ERROR_GENERIC = -1,
    WM_RESULT_ERROR_INVALID_ARG = -2,
    WM_RESULT_ERROR_NO_MEMORY = -3,
    WM_RESULT_ERROR_NOT_FOUND = -4,
    WM_RESULT_ERROR_EXISTS = -5,
    WM_RESULT_ERROR_PERMISSION = -6,
    WM_RESULT_ERROR_NOT_SUPPORTED = -7,
    WM_RESULT_ERROR_NOT_IMPLEMENTED = -8,
    WM_RESULT_ERROR_BACKEND = -9,
    WM_RESULT_ERROR_PLUGIN = -10,
    WM_RESULT_ERROR_TIMEOUT = -11,
    WM_RESULT_ERROR_CANCELED = -12
} WmResult;

/*==============================================================================
 * Callback Types
 *============================================================================*/

typedef void (*WmDestroyFunc)(void *data);
typedef int (*WmCompareFunc)(const void *a, const void *b);
typedef uint32_t (*WmHashFunc)(const void *key);
typedef void* (*WmCloneFunc)(const void *data);
typedef void (*WmEventHandler)(WmEventType type, const void *event_data, void *user_data);
typedef void (*WmTimerCallback)(WmTimer *timer, void *user_data);
typedef void* (*WmThreadFunc)(void *arg);

/*==============================================================================
 * Memory Management Types
 *============================================================================*/

typedef void* (*WmAllocFunc)(size_t size);
typedef void* (*WmReallocFunc)(void *ptr, size_t old_size, size_t new_size);
typedef void (*WmFreeFunc)(void *ptr);

typedef struct {
    WmAllocFunc alloc;
    WmReallocFunc realloc;
    WmFreeFunc free;
} WmAllocator;

/*==============================================================================
 * Logging Types
 *============================================================================*/

typedef enum {
    WM_LOG_LEVEL_DEBUG,
    WM_LOG_LEVEL_INFO,
    WM_LOG_LEVEL_WARN,
    WM_LOG_LEVEL_ERROR,
    WM_LOG_LEVEL_FATAL
} WmLogLevel;

typedef void (*WmLogHandler)(WmLogLevel level, const char *file, int line, 
                              const char *func, const char *message);

#ifdef __cplusplus
}
#endif

#endif /* WM_TYPES_H */
