/*
 * DWN - Desktop Window Manager
 * Built-in Layout Registration
 */

#include "plugins/layout_plugin.h"
#include "plugins/builtin_layouts.h"
#include "core/wm_list.h"
#include "dwn.h"
#include "util.h"

#include <stdlib.h>

/* External built-in layout interfaces */
extern const LayoutPluginInterface* wm_layout_tiling_get_interface(void);
extern const LayoutPluginInterface* wm_layout_floating_get_interface(void);
extern const LayoutPluginInterface* wm_layout_monocle_get_interface(void);
extern const LayoutPluginInterface* wm_layout_grid_get_interface(void);

/*==============================================================================
 * Built-in Layout Registration
 *============================================================================*/

static LayoutPlugin* register_builtin_layout(LayoutManager *mgr, 
                                              const LayoutPluginInterface *iface) {
    if (!mgr || !iface) return NULL;
    
    LayoutPlugin *plugin = calloc(1, sizeof(LayoutPlugin));
    if (!plugin) return NULL;
    
    plugin->interface = iface;
    plugin->handle = NULL;  /* Built-in, not dynamically loaded */
    plugin->path = NULL;
    plugin->enabled = true;
    
    if (!wm_layout_manager_register(mgr, plugin)) {
        free(plugin);
        return NULL;
    }
    
    LOG_INFO("Registered built-in layout: %s", iface->name);
    return plugin;
}

/*==============================================================================
 * Initialize All Built-in Layouts
 *============================================================================*/

bool wm_layouts_builtin_init(void) {
    LayoutManager *mgr = wm_layout_manager_get();
    if (!mgr) {
        LOG_ERROR("Layout manager not initialized");
        return false;
    }
    
    bool success = true;
    
    /* Register tiling layout */
    if (!register_builtin_layout(mgr, wm_layout_tiling_get_interface())) {
        LOG_ERROR("Failed to register tiling layout");
        success = false;
    }
    
    /* Register floating layout */
    if (!register_builtin_layout(mgr, wm_layout_floating_get_interface())) {
        LOG_ERROR("Failed to register floating layout");
        success = false;
    }
    
    /* Register monocle layout */
    if (!register_builtin_layout(mgr, wm_layout_monocle_get_interface())) {
        LOG_ERROR("Failed to register monocle layout");
        success = false;
    }
    
    /* Register grid layout */
    if (!register_builtin_layout(mgr, wm_layout_grid_get_interface())) {
        LOG_ERROR("Failed to register grid layout");
        success = false;
    }
    
    /* Set default layout */
    LayoutPlugin *tiling = wm_layout_manager_get_layout(mgr, "tiling");
    if (tiling) {
        wm_layout_manager_set_default(mgr, tiling);
    }
    
    return success;
}

/*==============================================================================
 * Get Layout by Type (Compatibility)
 *============================================================================*/

LayoutPlugin* wm_layout_get_by_type(LayoutManager *mgr, LayoutType type) {
    if (!mgr) mgr = wm_layout_manager_get();
    if (!mgr) return NULL;
    
    switch (type) {
        case LAYOUT_TILING:
            return wm_layout_manager_get_layout(mgr, "tiling");
        case LAYOUT_FLOATING:
            return wm_layout_manager_get_layout(mgr, "floating");
        case LAYOUT_MONOCLE:
            return wm_layout_manager_get_layout(mgr, "monocle");
        default:
            return wm_layout_manager_get_default(mgr);
    }
}

/*==============================================================================
 * Compatibility Bridge
 *============================================================================*/

/**
 * Bridge function to call layout arrangement through the new plugin system.
 * This allows gradual migration from the old layout system.
 */
bool wm_layout_arrange_workspace(LayoutManager *mgr, int workspace, LayoutType type) {
    if (!mgr) mgr = wm_layout_manager_get();
    if (!mgr) return false;
    
    LayoutPlugin *plugin = wm_layout_get_by_type(mgr, type);
    if (!plugin) {
        plugin = wm_layout_manager_get_default(mgr);
    }
    
    if (!plugin) {
        LOG_ERROR("No layout available for workspace %d", workspace);
        return false;
    }
    
    return wm_layout_arrange(plugin, workspace);
}

/**
 * Get the symbol for a layout type.
 */
const char* wm_layout_get_symbol_for_type(LayoutType type) {
    switch (type) {
        case LAYOUT_TILING:
            return "[]=";
        case LAYOUT_FLOATING:
            return "><>";
        case LAYOUT_MONOCLE:
            return "[M]";
        default:
            return "???";
    }
}

/**
 * Get the name for a layout type.
 */
const char* wm_layout_get_name_for_type(LayoutType type) {
    switch (type) {
        case LAYOUT_TILING:
            return "tiling";
        case LAYOUT_FLOATING:
            return "floating";
        case LAYOUT_MONOCLE:
            return "monocle";
        default:
            return "unknown";
    }
}

/**
 * Get LayoutType from layout name.
 */
LayoutType wm_layout_type_from_name(const char *name) {
    if (!name) return LAYOUT_TILING;
    
    if (strcmp(name, "tiling") == 0) return LAYOUT_TILING;
    if (strcmp(name, "floating") == 0) return LAYOUT_FLOATING;
    if (strcmp(name, "monocle") == 0) return LAYOUT_MONOCLE;
    if (strcmp(name, "grid") == 0) return LAYOUT_TILING;  /* Default to tiling for unknown */
    
    return LAYOUT_TILING;
}
