/**
 * GTK2 Media Player - Plugin Manager
 * 
 * Internal interface for loading, managing, and unloading plugins.
 */

#ifndef PLUGIN_MANAGER_H
#define PLUGIN_MANAGER_H

#include <gtk/gtk.h>
#include "plugin.h"

typedef struct PluginManager PluginManager;

/**
 * Loaded plugin instance
 */
typedef struct {
    char *id;               /* Plugin ID (directory name) */
    char *path;             /* Path to .so file */
    void *handle;           /* dlopen handle */
    PluginInfo info;        /* Plugin metadata */
    gboolean enabled;       /* Currently active */
    gboolean has_menu;      /* Has menu item */
    GtkWidget *menu_item;   /* Registered menu item */
    
    /* Function pointers */
    PluginGetInfoFunc get_info;
    PluginInitFunc init;
    PluginShutdownFunc shutdown;
    PluginGetMenuFunc get_menu_item;
    PluginOnEventFunc on_event;
    PluginConfigureFunc configure;
    PluginAboutFunc about;
} LoadedPlugin;

typedef struct AppUI AppUI;

/**
 * Create a new plugin manager
 * 
 * @param ui Main application UI structure
 * @return New plugin manager instance
 */
PluginManager* plugin_manager_new(AppUI *ui);

/**
 * Destroy plugin manager and unload all plugins
 */
void plugin_manager_destroy(PluginManager *pm);

/**
 * Set player control callbacks
 * Must be called before loading plugins
 */
void plugin_manager_set_player_callbacks(PluginManager *pm,
    void (*play)(void),
    void (*pause)(void),
    void (*stop)(void),
    void (*toggle_pause)(void),
    void (*seek)(double),
    void (*seek_absolute)(double),
    double (*get_position)(void),
    double (*get_duration)(void),
    double (*get_volume)(void),
    gboolean (*is_paused)(void),
    gboolean (*is_playing)(void),
    char* (*get_current_file)(void),
    char* (*get_media_title)(void),
    void (*take_screenshot)(void),
    void (*take_screenshot_to_file)(const char*)
);

/**
 * Load all plugins from the plugins directory
 * 
 * @return Number of plugins loaded
 */
int plugin_manager_load_all(PluginManager *pm);

/**
 * Install a plugin from a .zip file
 * 
 * @param zip_path Path to the .zip file
 * @return TRUE on success, FALSE on failure
 */
gboolean plugin_manager_install_zip(PluginManager *pm, const char *zip_path);

/**
 * Uninstall a plugin
 * 
 * @param plugin_id Plugin ID to uninstall
 * @return TRUE on success, FALSE on failure
 */
gboolean plugin_manager_uninstall(PluginManager *pm, const char *plugin_id);

/**
 * Get list of loaded plugins
 * 
 * @param count Output: number of plugins
 * @return Array of LoadedPlugin pointers (do not free)
 */
LoadedPlugin** plugin_manager_get_list(PluginManager *pm, int *count);

/**
 * Get the plugins menu
 * Creates menu items for all loaded plugins
 * 
 * @return GtkMenu widget
 */
GtkWidget* plugin_manager_get_menu(PluginManager *pm);

/**
 * Register a menu item from a plugin
 */
void plugin_manager_register_menu_item(PluginManager *pm, GtkWidget *item);

/**
 * Unregister a menu item from a plugin
 */
void plugin_manager_unregister_menu_item(PluginManager *pm, GtkWidget *item);

/**
 * Broadcast an event to all loaded plugins
 */
void plugin_manager_broadcast_event(PluginManager *pm, PluginEventType event, void *data);

/**
 * Create configuration widget for preferences dialog
 * Returns a GtkWidget (VBox) containing the plugin management UI
 */
GtkWidget* plugin_manager_create_config_widget(PluginManager *pm);

/**
 * Show plugin management dialog (Deprecated in favor of Preferences)
 */
void plugin_manager_show_dialog(PluginManager *pm);

/**
 * Get the plugins directory path
 * @return Path string (do not free)
 */
const char* plugin_manager_get_plugins_dir(PluginManager *pm);

#endif /* PLUGIN_MANAGER_H */
