/* 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 <string.h>

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

#include "libpikaconfig/pikaconfig.h"
#include "libpikawidgets/pikawidgets.h"

#include "actions-types.h"

#include "core/pika.h"
#include "core/pikacontainer.h"
#include "core/pikadatafactory.h"
#include "core/pikatoolinfo.h"
#include "core/pikatooloptions.h"
#include "core/pikatoolpreset.h"

#include "widgets/pikadataeditor.h"
#include "widgets/pikadialogfactory.h"
#include "widgets/pikaeditor.h"
#include "widgets/pikahelp-ids.h"
#include "widgets/pikamessagebox.h"
#include "widgets/pikamessagedialog.h"
#include "widgets/pikatooloptionseditor.h"
#include "widgets/pikauimanager.h"
#include "widgets/pikawidgets-utils.h"
#include "widgets/pikawindowstrategy.h"

#include "dialogs/data-delete-dialog.h"

#include "tool-options-commands.h"

#include "pika-intl.h"


/*  local function prototypes  */

static void   tool_options_show_preset_editor (Pika           *pika,
                                               PikaEditor     *editor,
                                               PikaToolPreset *preset);


/*  public functions  */

void
tool_options_save_new_preset_cmd_callback (PikaAction *action,
                                           GVariant   *value,
                                           gpointer    user_data)
{
  PikaEditor  *editor  = PIKA_EDITOR (user_data);
  Pika        *pika    = pika_editor_get_ui_manager (editor)->pika;
  PikaContext *context = pika_get_user_context (pika);
  PikaData    *data;

  data = pika_data_factory_data_new (context->pika->tool_preset_factory,
                                     context, _("Untitled"));

  tool_options_show_preset_editor (pika, editor, PIKA_TOOL_PRESET (data));
}

void
tool_options_save_preset_cmd_callback (PikaAction *action,
                                       GVariant   *value,
                                       gpointer    data)
{
  PikaEditor     *editor    = PIKA_EDITOR (data);
  Pika           *pika      = pika_editor_get_ui_manager (editor)->pika;
  PikaContext    *context   = pika_get_user_context (pika);
  PikaToolInfo   *tool_info = pika_context_get_tool (context);
  PikaToolPreset *preset;
  gint            index;

  index = g_variant_get_int32 (value);

  preset = (PikaToolPreset *)
    pika_container_get_child_by_index (tool_info->presets, index);

  if (preset)
    {
      pika_config_sync (G_OBJECT (tool_info->tool_options),
                        G_OBJECT (preset->tool_options), 0);

      tool_options_show_preset_editor (pika, editor, preset);
    }
}

void
tool_options_restore_preset_cmd_callback (PikaAction *action,
                                          GVariant   *value,
                                          gpointer    data)
{
  PikaEditor     *editor    = PIKA_EDITOR (data);
  Pika           *pika      = pika_editor_get_ui_manager (editor)->pika;
  PikaContext    *context   = pika_get_user_context (pika);
  PikaToolInfo   *tool_info = pika_context_get_tool (context);
  PikaToolPreset *preset;
  gint            index;

  index = g_variant_get_int32 (value);

  preset = (PikaToolPreset *)
    pika_container_get_child_by_index (tool_info->presets, index);

  if (preset)
    {
      if (pika_context_get_tool_preset (context) != preset)
        pika_context_set_tool_preset (context, preset);
      else
        pika_context_tool_preset_changed (context);
    }
}

void
tool_options_edit_preset_cmd_callback (PikaAction *action,
                                       GVariant   *value,
                                       gpointer    data)
{
  PikaEditor     *editor    = PIKA_EDITOR (data);
  Pika           *pika      = pika_editor_get_ui_manager (editor)->pika;
  PikaContext    *context   = pika_get_user_context (pika);
  PikaToolInfo   *tool_info = pika_context_get_tool (context);
  PikaToolPreset *preset;
  gint            index;

  index = g_variant_get_int32 (value);

  preset = (PikaToolPreset *)
    pika_container_get_child_by_index (tool_info->presets, index);

  if (preset)
    {
      tool_options_show_preset_editor (pika, editor, preset);
    }
}

void
tool_options_delete_preset_cmd_callback (PikaAction *action,
                                         GVariant   *value,
                                         gpointer    data)
{
  PikaEditor     *editor    = PIKA_EDITOR (data);
  PikaContext    *context   = pika_get_user_context (pika_editor_get_ui_manager (editor)->pika);
  PikaToolInfo   *tool_info = pika_context_get_tool (context);
  PikaToolPreset *preset;
  gint            index;

  index = g_variant_get_int32 (value);

  preset = (PikaToolPreset *)
    pika_container_get_child_by_index (tool_info->presets, index);

  if (preset &&
      pika_data_is_deletable (PIKA_DATA (preset)))
    {
      PikaDataFactory *factory = context->pika->tool_preset_factory;
      GtkWidget       *dialog;

      dialog = data_delete_dialog_new (factory, PIKA_DATA (preset), NULL,
                                       GTK_WIDGET (editor));
      gtk_widget_show (dialog);
    }
}

void
tool_options_reset_cmd_callback (PikaAction *action,
                                 GVariant   *value,
                                 gpointer    data)
{
  PikaEditor   *editor    = PIKA_EDITOR (data);
  PikaContext  *context   = pika_get_user_context (pika_editor_get_ui_manager (editor)->pika);
  PikaToolInfo *tool_info = pika_context_get_tool (context);

  pika_config_reset (PIKA_CONFIG (tool_info->tool_options));
}

void
tool_options_reset_all_cmd_callback (PikaAction *action,
                                     GVariant   *value,
                                     gpointer    data)
{
  PikaEditor *editor = PIKA_EDITOR (data);
  GtkWidget  *dialog;

  dialog = pika_message_dialog_new (_("Reset All Tool Options"),
                                    PIKA_ICON_DIALOG_QUESTION,
                                    GTK_WIDGET (editor),
                                    GTK_DIALOG_MODAL |
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    pika_standard_help_func, NULL,

                                    _("_Cancel"), GTK_RESPONSE_CANCEL,
                                    _("_Reset"),  GTK_RESPONSE_OK,

                                    NULL);

  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
  pika_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

  g_signal_connect_object (gtk_widget_get_toplevel (GTK_WIDGET (editor)),
                           "unmap",
                           G_CALLBACK (gtk_widget_destroy),
                           dialog, G_CONNECT_SWAPPED);

  pika_message_box_set_primary_text (PIKA_MESSAGE_DIALOG (dialog)->box,
                                     _("Do you really want to reset all "
                                       "tool options to default values?"));

  if (pika_dialog_run (PIKA_DIALOG (dialog)) == GTK_RESPONSE_OK)
    {
      Pika  *pika = pika_editor_get_ui_manager (editor)->pika;
      GList *list;

      for (list = pika_get_tool_info_iter (pika);
           list;
           list = g_list_next (list))
        {
          PikaToolInfo *tool_info = list->data;

          pika_config_reset (PIKA_CONFIG (tool_info->tool_options));
        }
    }

  gtk_widget_destroy (dialog);
}


/*  private functions  */

static void
tool_options_show_preset_editor (Pika           *pika,
                                 PikaEditor     *editor,
                                 PikaToolPreset *preset)
{
  GtkWidget *dockable;

  dockable =
    pika_window_strategy_show_dockable_dialog (PIKA_WINDOW_STRATEGY (pika_get_window_strategy (pika)),
                                               pika,
                                               pika_dialog_factory_get_singleton (),
                                               pika_widget_get_monitor (GTK_WIDGET (editor)),
                                               "pika-tool-preset-editor");

  pika_data_editor_set_data (PIKA_DATA_EDITOR (gtk_bin_get_child (GTK_BIN (dockable))),
                             PIKA_DATA (preset));
}
