/* Chrysalide - Outil d'analyse de fichiers binaires
 * core-ui.c - présentation de notes sous forme de texte et de code Python
 *
 * Copyright (C) 2025 Cyrille Bagard
 *
 *  This file is part of Chrysalide.
 *
 *  Chrysalide is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chrysalide is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "core-ui.h"


#include <i18n.h>
#include <gui/core/panels.h>
#include <plugins/self.h>
#include <plugins/tweakable-int.h>


#include "core-ui-int.h"
#include "panel.h"
#include "params.h"
#include "prefs.h"



/* ---------------------- COMPOSITION DE NOUVEAU GREFFON NATIF ---------------------- */


/* Initialise la classe des recherches et identifications. */
static void g_python_notebook_plugin_ui_class_init(GPythonNotebookPluginUIClass *);

/* Procède à l'initialisation de l'interface d'intervention. */
static void g_python_notebook_plugin_ui_tweakable_plugin_interface_init(GTweakablePluginInterface *);

/* Initialise une instance de recherches et identifications. */
static void g_python_notebook_plugin_ui_init(GPythonNotebookPluginUI *);

/* Supprime toutes les références externes. */
static void g_python_notebook_plugin_ui_dispose(GObject *);

/* Procède à la libération totale de la mémoire. */
static void g_python_notebook_plugin_ui_finalize(GObject *);



/* --------------------- IMPLEMENTATION DES FONCTIONS DE CLASSE --------------------- */


/* Prend acte de l'activation du greffon. */
static bool g_python_notebook_plugin_ui_enable(GPythonNotebookPluginUI *);

/* Prend acte de la désactivation du greffon. */
static bool g_python_notebook_plugin_ui_disable(GPythonNotebookPluginUI *);



/* ------------------- INTEGRATION DANS L'EDITION DES PREFERENCES ------------------- */


/* Fournit une liste de sections de configuration. */
static tweak_info_t *g_python_notebook_plugin_ui_get_tweak_info(const GTweakablePlugin *, size_t *);



/* ---------------------------------------------------------------------------------- */
/*                        COMPOSITION DE NOUVEAU GREFFON NATIF                        */
/* ---------------------------------------------------------------------------------- */


/* Indique le type défini pour une présentation de notes texte et code Python. */
G_DEFINE_TYPE_WITH_CODE(GPythonNotebookPluginUI, g_python_notebook_plugin_ui, G_TYPE_NATIVE_PLUGIN,
                        G_IMPLEMENT_INTERFACE(G_TYPE_TWEAKABLE_PLUGIN, g_python_notebook_plugin_ui_tweakable_plugin_interface_init));


NATIVE_PLUGIN_ENTRYPOINT(g_python_notebook_plugin_ui_new);


/******************************************************************************
*                                                                             *
*  Paramètres  : class = classe à initialiser.                                *
*                                                                             *
*  Description : Initialise la classe des recherches et identifications.      *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_python_notebook_plugin_ui_class_init(GPythonNotebookPluginUIClass *class)
{
    GObjectClass *object;                   /* Autre version de la classe  */
    GPluginModuleClass *plugin;             /* Version parente de la classe*/

    object = G_OBJECT_CLASS(class);

    object->dispose = g_python_notebook_plugin_ui_dispose;
    object->finalize = g_python_notebook_plugin_ui_finalize;

    plugin = G_PLUGIN_MODULE_CLASS(class);

    plugin->enable = (pg_management_fc)g_python_notebook_plugin_ui_enable;
    plugin->disable = (pg_management_fc)g_python_notebook_plugin_ui_disable;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : iface = interface GLib à initialiser.                        *
*                                                                             *
*  Description : Procède à l'initialisation de l'interface d'intervention.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_python_notebook_plugin_ui_tweakable_plugin_interface_init(GTweakablePluginInterface *iface)
{
    iface->get_info = g_python_notebook_plugin_ui_get_tweak_info;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = instance à initialiser.                             *
*                                                                             *
*  Description : Initialise une instance de recherches et identifications.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_python_notebook_plugin_ui_init(GPythonNotebookPluginUI *plugin)
{
    STORE_PLUGIN_ABI(plugin);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : object = instance d'objet GLib à traiter.                    *
*                                                                             *
*  Description : Supprime toutes les références externes.                     *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_python_notebook_plugin_ui_dispose(GObject *object)
{
    G_OBJECT_CLASS(g_python_notebook_plugin_ui_parent_class)->dispose(object);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : object = instance d'objet GLib à traiter.                    *
*                                                                             *
*  Description : Procède à la libération totale de la mémoire.                *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static void g_python_notebook_plugin_ui_finalize(GObject *object)
{
    G_OBJECT_CLASS(g_python_notebook_plugin_ui_parent_class)->finalize(object);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : module = extension vue du système.                           *
*                                                                             *
*  Description : Crée un module pour présentation de notes.                   *
*                                                                             *
*  Retour      : Adresse de la structure mise en place.                       *
*                                                                             *
*  Remarques   : Le transfert de propriétée du module est total.              *
*                                                                             *
******************************************************************************/

GPluginModule *g_python_notebook_plugin_ui_new(GModule *module)
{
    GPythonNotebookPluginUI *result;                  /* Structure à retourner       */

    result = g_object_new(G_TYPE_PYTHON_NOTEBOOK_PLUGIN_UI, NULL);

    if (!g_python_notebook_plugin_ui_create(result, module))
        g_clear_object(&result);

    return G_PLUGIN_MODULE(result);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = instance à initialiser pleinement.                  *
*                module = extension vue du système.                           *
*                                                                             *
*  Description : Met en place un module pour un module pour présentation.     *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : Le transfert de propriétée du module est total.              *
*                                                                             *
******************************************************************************/

bool g_python_notebook_plugin_ui_create(GPythonNotebookPluginUI *plugin, GModule *module)
{
    bool result;                            /* Bilan à retourner           */

#ifdef INCLUDE_PYTHON3_BINDINGS
#   define PG_REQ REQ_LIST("PyChrysalide")
#else
#   define PG_REQ NO_REQ
#endif

    result = g_native_plugin_create(G_NATIVE_PLUGIN(plugin),
                                    "PythonNotebook",
                                    "Edit notebook with text and code to support binary analysis",
                                    PACKAGE_VERSION,
                                    CHRYSALIDE_WEBSITE("doc/plugins/pynb"),
                                    PG_REQ,
                                    module);

    return result;

}



/* ---------------------------------------------------------------------------------- */
/*                       IMPLEMENTATION DES FONCTIONS DE CLASSE                       */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à manipuler.                                *
*                                                                             *
*  Description : Prend acte de l'activation du greffon.                       *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_python_notebook_plugin_ui_enable(GPythonNotebookPluginUI *plugin)
{
    bool result;                            /* Bilan à retourner           */
    panel_info_t info;                      /* Infos d'enregistrement      */

    info.category = "Main";

    info.image = "pynb-symbolic";
    info.title = _("Python notebook");
    info.desc = _("Edit notebook with text and code to support binary analysis");

    info.personality = FPP_MAIN_PANEL;

    info.panel_type = GTK_TYPE_PYTHON_NOTEBOOK_PANEL;
    info.params_type = GTK_TYPE_PYTHON_NOTEBOOK_PARAMETERS;

    result = register_framework_panel_definition(&info);

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = greffon à manipuler.                                *
*                                                                             *
*  Description : Prend acte de la désactivation du greffon.                   *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static bool g_python_notebook_plugin_ui_disable(GPythonNotebookPluginUI *plugin)
{
    bool result;                            /* Bilan à retourner           */


    // TODO : unregister

    result = true;


    return result;

}



/* ---------------------------------------------------------------------------------- */
/*                     INTEGRATION DANS L'EDITION DES PREFERENCES                     */
/* ---------------------------------------------------------------------------------- */


/******************************************************************************
*                                                                             *
*  Paramètres  : plugin = interface à manipuler.                              *
*                count  = taille de la liste renvoyée. [OUT]                  *
*                                                                             *
*  Description : Fournit une liste de sections de configuration.              *
*                                                                             *
*  Retour      : Définition(s) de section de configuration ou NULL.           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

static tweak_info_t *g_python_notebook_plugin_ui_get_tweak_info(const GTweakablePlugin *plugin, size_t *count)
{
    tweak_info_t *result;                   /* Liste à renvoyer            */

    tweak_info_t infos[] = {
        TWEAK_SIMPLE_DEF("root", "Basics",
                         "pynb-symbolic", "pynotebook", "Notebook", GTK_TYPE_PYTHON_NOTEBOOK_TWEAK_PANEL),
    };

    *count = 1;
    result = malloc(*count * sizeof(tweak_info_t));

    memcpy(result, infos, *count * sizeof(tweak_info_t));

    return result;

}