/*
 *  $Id: xyz_channels.c 30111 2026-06-22 11:02:00Z yeti-dn $
 *  Copyright (C) 2003-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 <gtk/gtk.h>
#include <gwy.h>
#include "../preview.h"

#define RUN_MODES (GWY_RUN_INTERACTIVE)

enum {
    PARAM_XDATA,
    PARAM_YDATA,
    PARAM_ZDATA,
};

typedef struct {
    GwyParams *params;
    GwyField *field;
    GwyScatter *scatter;
} 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             execute             (ModuleArgs *args);
static GwyDialogOutcome run_gui             (ModuleArgs *args);
static void             param_changed       (ModuleGUI *gui,
                                             gint id);
static gboolean         xzdata_image_filter (GwyFile *data,
                                             gint id,
                                             gpointer user_data);
static gboolean         ydata_image_filter  (GwyFile *data,
                                             gint id,
                                             gpointer user_data);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Converts three channels to XYZ data."),
    "Petr Klapetek <klapetek@gwyddion.net>",
    "2.1",
    "David Nečas (Yeti) & Petr Klapetek",
    "2018",
};

GWY_MODULE_QUERY2(module_info, xyz_channels)

static gboolean
module_register(void)
{
    gwy_image_func_register("xyz_channels",
                            module_main,
                            N_("/_Basic Operations/XYZ from C_hannels..."),
                            NULL,
                            RUN_MODES,
                            GWY_MENU_FLAG_IMAGE,
                            N_("Convert three channels to XYZ data"));

    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_image_func_current());
    gwy_param_def_add_image_id(paramdef, PARAM_XDATA, "xdata", _("_X data"));
    gwy_param_def_add_image_id(paramdef, PARAM_YDATA, "ydata", _("_Y data"));
    gwy_param_def_add_image_id(paramdef, PARAM_ZDATA, "zdata", _("_Z data"));
    return paramdef;
}

static void
module_main(GwyFile *data, GwyRunModeFlags mode)
{
    ModuleArgs args;
    gwy_clear1(args);
    GwyDialogOutcome outcome;
    gint id, newid;

    g_return_if_fail(mode & RUN_MODES);
    gwy_data_browser_get_current(GWY_APP_FIELD, &args.field,
                                 GWY_APP_FIELD_ID, &id,
                                 0);
    g_return_if_fail(args.field);
    args.scatter = gwy_scatter_new(1);

    args.params = gwy_params_new_from_settings(define_module_params());
    outcome = run_gui(&args);
    gwy_params_save_to_settings(args.params);
    if (outcome == GWY_DIALOG_CANCEL)
        goto end;

    if (outcome != GWY_DIALOG_HAVE_RESULT)
        execute(&args);

    newid = gwy_file_add_xyz(data, args.scatter);

    gwy_file_set_visible(data, GWY_FILE_XYZ, newid, TRUE);
    gwy_log_add_full(data, GWY_FILE_XYZ, -1, newid, "image::xyzize", NULL);

end:
    g_object_unref(args.scatter);
    g_object_unref(args.params);
}

static GwyDialogOutcome
run_gui(ModuleArgs *args)
{
    GwyDialog *dialog;
    GwyParamTable *table;
    ModuleGUI gui;

    gui.args = args;

    gui.dialog = gwy_dialog_new(_("XYZ Channels"));
    dialog = GWY_DIALOG(gui.dialog);
    gwy_dialog_add_stock_buttons(dialog, GWY_STOCK_CANCEL, GWY_STOCK_OK, 0);

    table = gui.table = gwy_param_table_new(args->params);
    gwy_param_table_append_image_id(table, PARAM_XDATA);
    gwy_param_table_data_id_set_filter(table, PARAM_XDATA, xzdata_image_filter, args->field, NULL);
    gwy_param_table_append_image_id(table, PARAM_YDATA);
    gwy_param_table_data_id_set_filter(table, PARAM_YDATA, ydata_image_filter, args->params, NULL);
    gwy_param_table_append_image_id(table, PARAM_ZDATA);
    gwy_param_table_data_id_set_filter(table, PARAM_ZDATA, xzdata_image_filter, args->field, NULL);
    gwy_dialog_add_content(dialog, gwy_param_table_widget(table), TRUE, TRUE, 0);

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

    return gwy_dialog_run(dialog);
}

static void
param_changed(ModuleGUI *gui, gint id)
{
    if (id == PARAM_XDATA)
        gwy_param_table_data_id_refilter(gui->table, PARAM_YDATA);
}

static gboolean
xzdata_image_filter(GwyFile *data, gint id, gpointer user_data)
{
    GwyField *field = (GwyField*)user_data;
    GwyField *coordfield = gwy_file_get_image(data, id);

    return !gwy_field_is_incompatible(coordfield, field, GWY_DATA_MISMATCH_RES | GWY_DATA_MISMATCH_REAL);
}

/* Y cooordinates must have the same units as X. */
static gboolean
ydata_image_filter(GwyFile *data, gint id, gpointer user_data)
{
    GwyParams *params = (GwyParams*)user_data;
    GwyField *xfield = gwy_params_get_image(params, PARAM_XDATA);
    GwyField *yfield = gwy_file_get_image(data, id);

    return !gwy_field_is_incompatible(yfield, xfield,
                                      GWY_DATA_MISMATCH_RES | GWY_DATA_MISMATCH_REAL | GWY_DATA_MISMATCH_VALUE);
}

static void
execute(ModuleArgs *args)
{
    GwyParams *params = args->params;
    GwyField *xfield = gwy_params_get_image(params, PARAM_XDATA);
    GwyField *yfield = gwy_params_get_image(params, PARAM_YDATA);
    GwyField *zfield = gwy_params_get_image(params, PARAM_ZDATA);
    const gdouble *xd = gwy_field_get_data_const(xfield);
    const gdouble *yd = gwy_field_get_data_const(yfield);
    const gdouble *zd = gwy_field_get_data_const(zfield);
    gsize n = gwy_field_get_xres(xfield)*gwy_field_get_yres(xfield);

    GwyScatter *scatter = args->scatter;
    gwy_scatter_resize(scatter, n);
    GwyXY *xy = gwy_scatter_get_xy(scatter);
    gdouble *z = gwy_scatter_get_data(scatter, 0);
    for (gsize i = 0; i < n; i++) {
        xy[i].x = xd[i];
        xy[i].y = yd[i];
        z[i] = zd[i];
    }

    gwy_unit_assign(gwy_scatter_get_unit_xy(scatter), gwy_field_get_unit_z(xfield));
    gwy_unit_assign(gwy_scatter_get_unit_data(scatter, 0), gwy_field_get_unit_z(zfield));
}

/* 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 : */
