/*
 *  $Id: cmap_rename.c 30111 2026-06-22 11:02:00Z yeti-dn $
 *  Copyright (C) 2026 David Necas (Yeti), Petr Klapetek.
 *  E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
 *
 *  This program 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 2 of the License, or (at your option) any
 *  later version.
 *
 *  This program 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 this program; if not, write to the
 *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include <glib/gi18n-lib.h>
#include <gwy.h>

#define RUN_MODES GWY_RUN_INTERACTIVE

enum {
    PARAM_CURVE,
    PARAM_NEW_NAME,

    BUTTON_RENAME,
};

enum {
    RESPONSE_RENAME = GWY_RESPONSE_USER
};

typedef struct {
    GwyParams *params;
    GwyLawn *lawn;
} ModuleArgs;

typedef struct {
    ModuleArgs *args;
    GtkWidget *dialog;
    GwyParamTable *table;
} ModuleGUI;

static gboolean     module_register     (void);
static GwyParamDef* define_module_params(void);
static void         module_main         (GwyFile *data,
                                         GwyRunModeFlags mode);
static void         run_gui             (ModuleArgs *args);
static void         param_changed       (ModuleGUI *gui,
                                         gint id);
static void         dialog_response     (ModuleGUI *gui,
                                         gint response);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Curve map data renaming."),
    "Yeti <yeti@gwyddion.net>",
    "1.0",
    "David Nečas (Yeti) & Petr Klapetek",
    "2026",
};

GWY_MODULE_QUERY2(module_info, cmap_rename)

static gboolean
module_register(void)
{
    gwy_cmap_func_register("cmap_rename_curves",
                           module_main,
                           N_("/_Basic Operations/Re_name Curves..."),
                           NULL,
                           RUN_MODES,
                           GWY_MENU_FLAG_CMAP,
                           N_("Rename curve map curves"));

    return TRUE;
}

static GwyParamDef*
define_module_params(void)
{
    static GwyParamDef *paramdef = NULL;

    if (paramdef)
        return paramdef;

    paramdef = gwy_param_def_new();
    gwy_param_def_set_function_name(paramdef, gwy_cmap_func_current());
    gwy_param_def_add_lawn_curve(paramdef, PARAM_CURVE, "curve", _("Curve to rename"));
    gwy_param_def_add_string(paramdef, PARAM_NEW_NAME, "name", _("New name"),
                             GWY_PARAM_STRING_NULL_IS_EMPTY, NULL, "");

    return paramdef;
}

static void
module_main(G_GNUC_UNUSED GwyFile *data, GwyRunModeFlags mode)
{
    ModuleArgs args;
    gwy_clear1(args);

    g_return_if_fail(mode & RUN_MODES);

    gwy_data_browser_get_current(GWY_APP_LAWN, &args.lawn, 0);
    g_return_if_fail(GWY_IS_LAWN(args.lawn));

    args.params = gwy_params_new_from_settings(define_module_params());
    run_gui(&args);
    gwy_params_save_to_settings(args.params);
    g_object_unref(args.params);
}

static void
run_gui(ModuleArgs *args)
{
    ModuleGUI gui;

    gui.args = args;
    gui.dialog = gwy_dialog_new(_("Rename Curve"));
    GwyDialog *dialog = GWY_DIALOG(gui.dialog);
    gwy_dialog_add_stock_buttons(dialog, GWY_STOCK_CLOSE, 0);

    GwyParamTable *table = gui.table = gwy_param_table_new(args->params);

    gwy_param_table_append_lawn_curve(table, PARAM_CURVE, args->lawn);
    gwy_param_table_append_entry(table, PARAM_NEW_NAME);
    gwy_param_table_append_button(table, BUTTON_RENAME, -1, RESPONSE_RENAME, _("_Rename"));
    gwy_dialog_add_content(dialog, gwy_param_table_widget(table), FALSE, TRUE, 0);

    g_signal_connect_swapped(table, "param-changed", G_CALLBACK(param_changed), &gui);
    g_signal_connect_swapped(dialog, "response", G_CALLBACK(dialog_response), &gui);

    gwy_dialog_run(dialog);
}

static void
param_changed(ModuleGUI *gui, gint id)
{
    ModuleArgs *args = gui->args;
    GwyParamTable *table = gui->table;

    if (id < 0 || id == PARAM_CURVE) {
        gint i = gwy_params_get_int(args->params, PARAM_CURVE);
        const gchar *label = gwy_lawn_get_curve_label(args->lawn, i);
        gwy_param_table_set_string(table, PARAM_NEW_NAME, label);
    }
}

static void
dialog_response(ModuleGUI *gui, gint response)
{
    ModuleArgs *args = gui->args;
    GwyParamTable *table = gui->table;

    if (response == RESPONSE_RENAME) {
        gint i = gwy_params_get_int(args->params, PARAM_CURVE);
        const gchar *oldlabel = gwy_lawn_get_curve_label(args->lawn, i);
        const gchar *newlabel = gwy_params_get_string(args->params, PARAM_NEW_NAME);
        if (!g_strcmp0(newlabel, oldlabel))
            return;

        gwy_lawn_set_curve_label(args->lawn, i, newlabel);
        gwy_param_table_lawn_curve_refresh(table, PARAM_CURVE, TRUE);
    }
}

/* vim: set cin columns=120 tw=118 et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
