/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libpikawidgets/pikawidgets.h"

#include "actions-types.h"

#include "core/pika.h"
#include "core/pikacontext.h"
#include "core/pikadatafactory.h"
#include "core/pikapalette.h"

#include "widgets/pikacontainerview.h"
#include "widgets/pikadatafactoryview.h"
#include "widgets/pikadialogfactory.h"
#include "widgets/pikahelp-ids.h"
#include "widgets/pikaview.h"
#include "widgets/pikawidgets-utils.h"

#include "dialogs/dialogs.h"

#include "actions.h"
#include "palettes-commands.h"

#include "pika-intl.h"


/*  local function prototypes  */

static void   palettes_merge_callback (GtkWidget   *widget,
                                       const gchar *palette_name,
                                       gpointer     data);


/*  public functions */

void
palettes_import_cmd_callback (PikaAction *action,
                              GVariant   *value,
                              gpointer    data)
{
  GtkWidget *widget;
  return_if_no_widget (widget, data);

  pika_dialog_factory_dialog_new (pika_dialog_factory_get_singleton (),
                                  pika_widget_get_monitor (widget),
                                  NULL /*ui_manager*/,
                                  widget,
                                  "pika-palette-import-dialog", -1, TRUE);
}

void
palettes_merge_cmd_callback (PikaAction *action,
                             GVariant   *value,
                             gpointer    data)
{
  PikaContainerEditor *editor = PIKA_CONTAINER_EDITOR (data);
  GtkWidget           *dialog;

#define MERGE_DIALOG_KEY "pika-palettes-merge-dialog"

  dialog = dialogs_get_dialog (G_OBJECT (editor), MERGE_DIALOG_KEY);

  if (! dialog)
    {
      dialog = pika_query_string_box (_("Merge Palettes"),
                                      GTK_WIDGET (editor),
                                      pika_standard_help_func,
                                      PIKA_HELP_PALETTE_MERGE,
                                      _("Enter a name for the merged palette"),
                                      NULL,
                                      G_OBJECT (editor), "destroy",
                                      palettes_merge_callback,
                                      editor, NULL);

      dialogs_attach_dialog (G_OBJECT (editor), MERGE_DIALOG_KEY, dialog);
    }

  gtk_window_present (GTK_WINDOW (dialog));
}


/*  private functions  */

static void
palettes_merge_callback (GtkWidget   *widget,
                         const gchar *palette_name,
                         gpointer     data)
{
  PikaContainerEditor *editor = data;
  PikaDataFactoryView *view   = data;
  PikaDataFactory     *factory;
  PikaContext         *context;
  PikaPalette         *new_palette;
  GList               *selected = NULL;
  GList               *list;

  context = pika_container_view_get_context (editor->view);
  factory = pika_data_factory_view_get_data_factory (view);

  pika_container_view_get_selected (editor->view, &selected, NULL);

  if (g_list_length (selected) < 2)
    {
      pika_message_literal (context->pika,
                            G_OBJECT (editor), PIKA_MESSAGE_WARNING,
                            _("There must be at least two palettes selected "
                              "to merge."));
      g_list_free (selected);
      return;
    }

  new_palette = PIKA_PALETTE (pika_data_factory_data_new (factory, context,
                                                          palette_name));

  for (list = selected; list; list = g_list_next (list))
    {
      PikaPalette *palette = list->data;
      GList       *cols;

      for (cols = pika_palette_get_colors (palette);
           cols;
           cols = g_list_next (cols))
        {
          PikaPaletteEntry *entry = cols->data;

          pika_palette_add_entry (new_palette, -1,
                                  entry->name,
                                  &entry->color);
        }
    }

  g_list_free (selected);
}
