/*
 * DWN - Desktop Window Manager
 * Floating Layout Plugin (Built-in)
 */

#include "plugins/layout_plugin.h"
#include "core/wm_client.h"
#include "util.h"

#include <stdlib.h>

/*==============================================================================
 * Floating Layout Interface
 *============================================================================*/

static bool floating_init(LayoutPlugin *plugin) {
    LOG_INFO("Floating layout initialized");
    (void)plugin;
    return true;
}

static void floating_shutdown(LayoutPlugin *plugin) {
    LOG_INFO("Floating layout shutdown");
    (void)plugin;
}

static WmLayoutState* floating_state_create(LayoutPlugin *plugin, WmWorkspaceId workspace) {
    (void)plugin;
    (void)workspace;
    /* No state needed for floating layout */
    return NULL;
}

static void floating_state_destroy(LayoutPlugin *plugin, WmLayoutState *state) {
    (void)plugin;
    (void)state;
}

static void floating_state_reset(LayoutPlugin *plugin, WmLayoutState *state) {
    (void)plugin;
    (void)state;
}

/*==============================================================================
 * Floating Layout Calculation
 *============================================================================*/

static bool floating_arrange(LayoutPlugin *plugin, LayoutContext *ctx,
                              AbstractClient **clients, int num_clients,
                              WmLayoutGeometry *geometries) {
    (void)plugin;
    (void)ctx;
    (void)clients;
    
    /* Floating layout doesn't arrange windows - they stay where they are */
    /* Just mark all as floating */
    for (int i = 0; i < num_clients; i++) {
        geometries[i].floating = true;
        geometries[i].hidden = false;
        geometries[i].fullscreen = false;
        /* x, y, width, height are ignored since floating = true */
    }
    
    return true;
}

/*==============================================================================
 * Layout Configuration Parameters
 *============================================================================*/

static WmLayoutParameter floating_parameters[] = {
    {
        .name = "constrain_to_workarea",
        .description = "Keep floating windows within work area",
        .type = WM_LAYOUT_PARAM_BOOL,
        .default_value.bool_value = true
    }
};

/*==============================================================================
 * Layout Plugin Interface
 *============================================================================*/

static LayoutPluginInterface floating_interface = {
    .api_version = WM_LAYOUT_PLUGIN_API_VERSION,
    .name = "floating",
    .description = "Floating layout - windows can be moved freely",
    .author = "DWN",
    .version = "1.0",
    
    .parameters = floating_parameters,
    .num_parameters = sizeof(floating_parameters) / sizeof(floating_parameters[0]),
    
    .init = floating_init,
    .shutdown = floating_shutdown,
    
    .state_create = floating_state_create,
    .state_destroy = floating_state_destroy,
    .state_reset = floating_state_reset,
    
    .config_parse = NULL,
    .config_apply = NULL,
    
    .arrange = floating_arrange,
    
    .client_added = NULL,
    .client_removed = NULL,
    .client_floating_changed = NULL,
    
    .inc_master = NULL,
    .set_master_ratio = NULL,
    .set_master_count = NULL,
    .swap_master = NULL,
    .rotate = NULL,
    .resize_master = NULL
};

/*==============================================================================
 * Exported Interface
 *============================================================================*/

const LayoutPluginInterface* wm_layout_floating_get_interface(void) {
    return &floating_interface;
}

WM_LAYOUT_REGISTER(floating, floating_interface)
