/* 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-2001 Spencer Kimball, Peter Mattis and others
 *
 * 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 "libpikabase/pikabase.h"
#include "libpikaconfig/pikaconfig.h"
#include "libpikawidgets/pikawidgets.h"

#include "tools-types.h"

#include "widgets/pikawidgets-utils.h"

#include "core/pika.h"
#include "core/pika-contexts.h"
#include "core/pika-internal-data.h"
#include "core/pikacontext.h"
#include "core/pikalist.h"
#include "core/pikatoolgroup.h"
#include "core/pikatoolinfo.h"
#include "core/pikatooloptions.h"

#include "pika-tool-options-manager.h"
#include "pika-tools.h"
#include "pikatooloptions-gui.h"
#include "tool_manager.h"

#include "pikaairbrushtool.h"
#include "pikaaligntool.h"
#include "pikabrightnesscontrasttool.h"
#include "pikabucketfilltool.h"
#include "pikabycolorselecttool.h"
#include "pikacagetool.h"
#include "pikaclonetool.h"
#include "pikacolorpickertool.h"
#include "pikaconvolvetool.h"
#include "pikacroptool.h"
#include "pikacurvestool.h"
#include "pikadodgeburntool.h"
#include "pikaellipseselecttool.h"
#include "pikaerasertool.h"
#include "pikafliptool.h"
#include "pikafreeselecttool.h"
#include "pikaforegroundselecttool.h"
#include "pikafuzzyselecttool.h"
#include "pikagegltool.h"
#include "pikagradienttool.h"
#include "pikahandletransformtool.h"
#include "pikahealtool.h"
#include "pikainktool.h"
#include "pikaiscissorstool.h"
#include "pikalevelstool.h"
#include "pikaoperationtool.h"
#include "pikamagnifytool.h"
#include "pikameasuretool.h"
#include "pikamovetool.h"
#include "pikamybrushtool.h"
#include "pikanpointdeformationtool.h"
#include "pikaoffsettool.h"
#include "pikapaintbrushtool.h"
#include "pikapaintselecttool.h"
#include "pikapenciltool.h"
#include "pikaperspectiveclonetool.h"
#include "pikaperspectivetool.h"
#include "pikathresholdtool.h"
#include "pikarectangleselecttool.h"
#include "pikarotatetool.h"
#include "pikaseamlessclonetool.h"
#include "pikascaletool.h"
#include "pikasheartool.h"
#include "pikasmudgetool.h"
#include "pikatexttool.h"
#include "pikatransform3dtool.h"
#include "pikaunifiedtransformtool.h"
#include "pikavectortool.h"
#include "pikawarptool.h"

#include "pika-intl.h"


#define TOOL_RC_FILE_VERSION 1


/*  local function prototypes  */

static void   pika_tools_register       (GType                   tool_type,
                                         GType                   tool_options_type,
                                         PikaToolOptionsGUIFunc  options_gui_func,
                                         PikaContextPropMask     context_props,
                                         const gchar            *identifier,
                                         const gchar            *label,
                                         const gchar            *tooltip,
                                         const gchar            *menu_label,
                                         const gchar            *menu_accel,
                                         const gchar            *help_domain,
                                         const gchar            *help_data,
                                         const gchar            *icon_name,
                                         gpointer                data);

static void   pika_tools_copy_structure (Pika                   *pika,
                                         PikaContainer          *src_container,
                                         PikaContainer          *dest_container,
                                         GHashTable             *tools);

/*  private variables  */

static GBinding *toolbox_groups_binding = NULL;
static gboolean  tool_options_deleted   = FALSE;


/*  public functions  */

void
pika_tools_init (Pika *pika)
{
  PikaToolRegisterFunc register_funcs[] =
  {
    /*  selection tools */

    pika_rectangle_select_tool_register,
    pika_ellipse_select_tool_register,
    pika_free_select_tool_register,
    pika_fuzzy_select_tool_register,
    pika_by_color_select_tool_register,
    pika_iscissors_tool_register,
    pika_foreground_select_tool_register,
    pika_paint_select_tool_register,

    /*  path tool */

    pika_vector_tool_register,

    /*  non-modifying tools  */

    pika_color_picker_tool_register,
    pika_magnify_tool_register,
    pika_measure_tool_register,

    /*  transform tools  */

    pika_move_tool_register,
    pika_align_tool_register,
    pika_crop_tool_register,
    pika_unified_transform_tool_register,
    pika_rotate_tool_register,
    pika_scale_tool_register,
    pika_shear_tool_register,
    pika_handle_transform_tool_register,
    pika_perspective_tool_register,
    pika_transform_3d_tool_register,
    pika_flip_tool_register,
    pika_cage_tool_register,
    pika_warp_tool_register,
    pika_n_point_deformation_tool_register,

    /*  paint tools  */

    pika_seamless_clone_tool_register,
    pika_text_tool_register,
    pika_bucket_fill_tool_register,
    pika_gradient_tool_register,
    pika_pencil_tool_register,
    pika_paintbrush_tool_register,
    pika_eraser_tool_register,
    pika_airbrush_tool_register,
    pika_ink_tool_register,
    pika_mybrush_tool_register,
    pika_clone_tool_register,
    pika_heal_tool_register,
    pika_perspective_clone_tool_register,
    pika_convolve_tool_register,
    pika_smudge_tool_register,
    pika_dodge_burn_tool_register,

    /*  filter tools  */

    pika_brightness_contrast_tool_register,
    pika_threshold_tool_register,
    pika_levels_tool_register,
    pika_curves_tool_register,
    pika_offset_tool_register,
    pika_gegl_tool_register,
    pika_operation_tool_register
  };

  gint i;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  pika_tool_options_create_folder ();

  pika_container_freeze (pika->tool_info_list);

  for (i = 0; i < G_N_ELEMENTS (register_funcs); i++)
    {
      register_funcs[i] (pika_tools_register, pika);
    }

  pika_container_thaw (pika->tool_info_list);

  pika_tool_options_manager_init (pika);

  tool_manager_init (pika);

  toolbox_groups_binding = g_object_bind_property (
    pika->config,            "toolbox-groups",
    pika->tool_item_ui_list, "flat",
    G_BINDING_INVERT_BOOLEAN |
    G_BINDING_SYNC_CREATE);
}

void
pika_tools_exit (Pika *pika)
{
  GList *list;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  g_clear_object (&toolbox_groups_binding);

  tool_manager_exit (pika);

  pika_tool_options_manager_exit (pika);

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

      pika_tools_set_tool_options_gui (tool_info->tool_options, NULL);
    }
}

void
pika_tools_restore (Pika *pika)
{
  PikaObject *object;
  GList      *list;
  GError     *error = NULL;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  /* restore tool order */
  pika_tools_reset (pika, pika->tool_item_list, TRUE);

  /* make the generic operation tool invisible by default */
  object = pika_container_get_child_by_name (pika->tool_info_list,
                                             "pika-operation-tool");
  if (object)
    g_object_set (object, "visible", FALSE, NULL);

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

      /*  get default values from prefs (see bug #120832)  */
      pika_config_reset (PIKA_CONFIG (tool_info->tool_options));
    }

  if (! pika_contexts_load (pika, &error))
    {
      pika_message_literal (pika, NULL, PIKA_MESSAGE_WARNING, error->message);
      g_clear_error (&error);
    }

  if (! pika_internal_data_load (pika, &error))
    {
      pika_message_literal (pika, NULL, PIKA_MESSAGE_WARNING, error->message);
      g_clear_error (&error);
    }

  /*  make sure there is always a tool active, so broken config files
   *  can't leave us with no initial tool
   */
  if (! pika_context_get_tool (pika_get_user_context (pika)))
    {
      pika_context_set_tool (pika_get_user_context (pika),
                             pika_get_tool_info_iter (pika)->data);
    }

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

      /*  copy all context properties except those the tool actually
       *  uses, because the subsequent deserialize() on the tool
       *  options will only set the properties that were set to
       *  non-default values at the time of saving, and we want to
       *  keep these default values as if they have been saved.
       * (see bug #541586).
       */
      pika_context_copy_properties (pika_get_user_context (pika),
                                    PIKA_CONTEXT (tool_info->tool_options),
                                    PIKA_CONTEXT_PROP_MASK_ALL &~
                                    (tool_info->context_props    |
                                     PIKA_CONTEXT_PROP_MASK_TOOL |
                                     PIKA_CONTEXT_PROP_MASK_PAINT_INFO));

      pika_tool_options_deserialize (tool_info->tool_options, NULL);

      options_gui_func = g_object_get_data (G_OBJECT (tool_info),
                                            "pika-tool-options-gui-func");

      if (! options_gui_func)
        options_gui_func = pika_tool_options_empty_gui;

      pika_tools_set_tool_options_gui_func (tool_info->tool_options,
                                            options_gui_func);
    }
}

void
pika_tools_save (Pika     *pika,
                 gboolean  save_tool_options,
                 gboolean  always_save)
{
  PikaConfigWriter *writer;
  GFile            *file;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  if (save_tool_options && (! tool_options_deleted || always_save))
    {
      GList  *list;
      GError *error = NULL;

      if (! pika_contexts_save (pika, &error))
        {
          pika_message_literal (pika, NULL, PIKA_MESSAGE_WARNING,
                                error->message);
          g_clear_error (&error);
        }

      if (! pika_internal_data_save (pika, &error))
        {
          pika_message_literal (pika, NULL, PIKA_MESSAGE_WARNING,
                                error->message);
          g_clear_error (&error);
        }

      pika_tool_options_create_folder ();

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

          pika_tool_options_serialize (tool_info->tool_options, NULL);
        }
    }

  file = pika_directory_file ("toolrc", NULL);

  if (pika->be_verbose)
    g_print ("Writing '%s'\n", pika_file_get_utf8_name (file));

  writer = pika_config_writer_new_from_file (file, TRUE, "PIKA toolrc", NULL);

  if (writer)
    {
      pika_tools_serialize (pika, pika->tool_item_list, writer);

      pika_config_writer_finish (writer, "end of toolrc", NULL);
    }

  g_object_unref (file);
}

gboolean
pika_tools_clear (Pika    *pika,
                  GError **error)
{
  GList    *list;
  gboolean  success = TRUE;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);

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

      success = pika_tool_options_delete (tool_info->tool_options, NULL);
    }

  if (success)
    success = pika_contexts_clear (pika, error);

  if (success)
    success = pika_internal_data_clear (pika, error);

  if (success)
    tool_options_deleted = TRUE;

  return success;
}

gboolean
pika_tools_serialize (Pika             *pika,
                      PikaContainer    *container,
                      PikaConfigWriter *writer)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (PIKA_IS_CONTAINER (container), FALSE);

  pika_config_writer_open (writer, "file-version");
  pika_config_writer_printf (writer, "%d", TOOL_RC_FILE_VERSION);
  pika_config_writer_close (writer);

  pika_config_writer_linefeed (writer);

  return pika_config_serialize (PIKA_CONFIG (container), writer, NULL);
}

gboolean
pika_tools_deserialize (Pika          *pika,
                        PikaContainer *container,
                        GScanner      *scanner)
{
  enum
  {
    FILE_VERSION = 1
  };

  PikaContainer *src_container;
  GTokenType     token;
  guint          scope_id;
  guint          old_scope_id;
  gint           file_version = 0;
  gboolean       result       = FALSE;

  scope_id     = g_type_qname (PIKA_TYPE_TOOL_GROUP);
  old_scope_id = g_scanner_set_scope (scanner, scope_id);

  g_scanner_scope_add_symbol (scanner, scope_id,
                              "file-version",
                              GINT_TO_POINTER (FILE_VERSION));

  token = G_TOKEN_LEFT_PAREN;

  while (g_scanner_peek_next_token (scanner) == token &&
         (token != G_TOKEN_LEFT_PAREN                 ||
          ! file_version))
    {
      token = g_scanner_get_next_token (scanner);

      switch (token)
        {
        case G_TOKEN_LEFT_PAREN:
          token = G_TOKEN_SYMBOL;
          break;

        case G_TOKEN_SYMBOL:
          switch (GPOINTER_TO_INT (scanner->value.v_symbol))
            {
            case FILE_VERSION:
              token = G_TOKEN_INT;
              if (pika_scanner_parse_int (scanner, &file_version))
                token = G_TOKEN_RIGHT_PAREN;
              break;
            }
          break;

        case G_TOKEN_RIGHT_PAREN:
          token = G_TOKEN_LEFT_PAREN;
          break;

        default:
          break;
        }
    }

  g_scanner_set_scope (scanner, old_scope_id);

  if (token != G_TOKEN_LEFT_PAREN)
    {
      g_scanner_get_next_token (scanner);
      g_scanner_unexp_token (scanner, token, NULL, NULL, NULL,
                             _("fatal parse error"), TRUE);

      return FALSE;
    }
  else if (file_version != TOOL_RC_FILE_VERSION)
    {
      g_scanner_error (scanner, "wrong toolrc file format version");

      return FALSE;
    }

  pika_container_freeze (container);

  /* make sure the various PikaToolItem types are registered */
  g_type_class_unref (g_type_class_ref (PIKA_TYPE_TOOL_GROUP));
  g_type_class_unref (g_type_class_ref (PIKA_TYPE_TOOL_INFO));

  pika_container_clear (container);

  src_container = g_object_new (PIKA_TYPE_LIST,
                                "children-type", PIKA_TYPE_TOOL_ITEM,
                                "append",        TRUE,
                                NULL);

  if (pika_config_deserialize (PIKA_CONFIG (src_container),
                               scanner, 0, NULL))
    {
      GHashTable *tools;
      GList      *list;

      result = TRUE;

      tools = g_hash_table_new (g_direct_hash, g_direct_equal);

      pika_tools_copy_structure (pika, src_container, container, tools);

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

          if (! tool_info->hidden && ! g_hash_table_contains (tools, tool_info))
            {
              if (tool_info->experimental)
                {
                  /* if an experimental tool is not in the file, just add it to
                   * the tool-item list.
                   */
                  pika_container_add (container, PIKA_OBJECT (tool_info));
                }
              else
                {
                  /* otherwise, it means we added a new stable tool.  this must
                   * be the user toolrc file; rejct it, so that we fall back to
                   * the default toolrc file, which should contain the missing
                   * tool.
                   */
                  g_scanner_error (scanner, "missing tools in toolrc file");

                  result = FALSE;

                  break;
                }
            }
        }

      g_hash_table_unref (tools);
    }

  g_object_unref (src_container);

  pika_container_thaw (container);

  return result;
}

void
pika_tools_reset (Pika          *pika,
                  PikaContainer *container,
                  gboolean       user_toolrc)
{
  GList *files = NULL;
  GList *list;

  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (PIKA_IS_CONTAINER (container));

  if (user_toolrc)
    files = g_list_prepend (files, pika_directory_file       ("toolrc", NULL));
  files = g_list_prepend (files, pika_sysconf_directory_file ("toolrc", NULL));

  files = g_list_reverse (files);

  pika_container_freeze (container);

  pika_container_clear (container);

  for (list = files; list; list = g_list_next (list))
    {
      GScanner *scanner;
      GFile    *file  = list->data;
      GError   *error = NULL;

      if (pika->be_verbose)
        g_print ("Parsing '%s'\n", pika_file_get_utf8_name (file));

      scanner = pika_scanner_new_file (file, &error);

      if (scanner && pika_tools_deserialize (pika, container, scanner))
        {
          pika_scanner_unref (scanner);

          break;
        }
      else
        {
          if (error->code != G_IO_ERROR_NOT_FOUND)
            {
              pika_message_literal (pika, NULL,
                                    PIKA_MESSAGE_WARNING, error->message);
            }

          g_clear_error (&error);

          pika_container_clear (container);
        }

      g_clear_pointer (&scanner, pika_scanner_unref);
    }

  g_list_free_full (files, g_object_unref);

  if (pika_container_is_empty (container))
    {
      if (pika->be_verbose)
        g_print ("Using default tool order\n");

      pika_tools_copy_structure (pika, pika->tool_info_list, container, NULL);
    }

  pika_container_thaw (container);
}


/*  private functions  */

static void
pika_tools_register (GType                   tool_type,
                     GType                   tool_options_type,
                     PikaToolOptionsGUIFunc  options_gui_func,
                     PikaContextPropMask     context_props,
                     const gchar            *identifier,
                     const gchar            *label,
                     const gchar            *tooltip,
                     const gchar            *menu_label,
                     const gchar            *menu_accel,
                     const gchar            *help_domain,
                     const gchar            *help_data,
                     const gchar            *icon_name,
                     gpointer                data)
{
  Pika         *pika = (Pika *) data;
  PikaToolInfo *tool_info;
  const gchar  *paint_core_name;
  gboolean      visible;

  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (g_type_is_a (tool_type, PIKA_TYPE_TOOL));
  g_return_if_fail (tool_options_type == G_TYPE_NONE ||
                    g_type_is_a (tool_options_type, PIKA_TYPE_TOOL_OPTIONS));

  if (tool_options_type == G_TYPE_NONE)
    tool_options_type = PIKA_TYPE_TOOL_OPTIONS;

  if (tool_type == PIKA_TYPE_PENCIL_TOOL)
    {
      paint_core_name = "pika-pencil";
    }
  else if (tool_type == PIKA_TYPE_PAINTBRUSH_TOOL)
    {
      paint_core_name = "pika-paintbrush";
    }
  else if (tool_type == PIKA_TYPE_ERASER_TOOL)
    {
      paint_core_name = "pika-eraser";
    }
  else if (tool_type == PIKA_TYPE_AIRBRUSH_TOOL)
    {
      paint_core_name = "pika-airbrush";
    }
  else if (tool_type == PIKA_TYPE_CLONE_TOOL)
    {
      paint_core_name = "pika-clone";
    }
  else if (tool_type == PIKA_TYPE_HEAL_TOOL)
    {
      paint_core_name = "pika-heal";
    }
  else if (tool_type == PIKA_TYPE_PERSPECTIVE_CLONE_TOOL)
    {
      paint_core_name = "pika-perspective-clone";
    }
  else if (tool_type == PIKA_TYPE_CONVOLVE_TOOL)
    {
      paint_core_name = "pika-convolve";
    }
  else if (tool_type == PIKA_TYPE_SMUDGE_TOOL)
    {
      paint_core_name = "pika-smudge";
    }
  else if (tool_type == PIKA_TYPE_DODGE_BURN_TOOL)
    {
      paint_core_name = "pika-dodge-burn";
    }
  else if (tool_type == PIKA_TYPE_INK_TOOL)
    {
      paint_core_name = "pika-ink";
    }
  else if (tool_type == PIKA_TYPE_MYBRUSH_TOOL)
    {
      paint_core_name = "pika-mybrush";
    }
  else
    {
      paint_core_name = "pika-paintbrush";
    }

  tool_info = pika_tool_info_new (pika,
                                  tool_type,
                                  tool_options_type,
                                  context_props,
                                  identifier,
                                  label,
                                  tooltip,
                                  menu_label,
                                  menu_accel,
                                  help_domain,
                                  help_data,
                                  paint_core_name,
                                  icon_name);

  visible = (! g_type_is_a (tool_type, PIKA_TYPE_FILTER_TOOL));

  pika_tool_item_set_visible (PIKA_TOOL_ITEM (tool_info), visible);

  /* hack to hide the operation tool entirely */
  if (tool_type == PIKA_TYPE_OPERATION_TOOL)
    tool_info->hidden = TRUE;

  /* hack to not require experimental tools to be present in toolrc */
  if (tool_type == PIKA_TYPE_N_POINT_DEFORMATION_TOOL ||
      tool_type == PIKA_TYPE_SEAMLESS_CLONE_TOOL      ||
      tool_type == PIKA_TYPE_PAINT_SELECT_TOOL)
    {
      tool_info->experimental = TRUE;
    }

  g_object_set_data (G_OBJECT (tool_info), "pika-tool-options-gui-func",
                     options_gui_func);

  pika_container_add (pika->tool_info_list, PIKA_OBJECT (tool_info));
  g_object_unref (tool_info);

  if (tool_type == PIKA_TYPE_PAINTBRUSH_TOOL)
    pika_tool_info_set_standard (pika, tool_info);
}

static void
pika_tools_copy_structure (Pika          *pika,
                           PikaContainer *src_container,
                           PikaContainer *dest_container,
                           GHashTable    *tools)
{
  GList *list;

  for (list = PIKA_LIST (src_container)->queue->head;
       list;
       list = g_list_next (list))
    {
      PikaToolItem *src_tool_item  = list->data;
      PikaToolItem *dest_tool_item = NULL;

      if (PIKA_IS_TOOL_GROUP (src_tool_item))
        {
          dest_tool_item = PIKA_TOOL_ITEM (pika_tool_group_new ());

          pika_tools_copy_structure (
            pika,
            pika_viewable_get_children (PIKA_VIEWABLE (src_tool_item)),
            pika_viewable_get_children (PIKA_VIEWABLE (dest_tool_item)),
            tools);

          pika_tool_group_set_active_tool (
            PIKA_TOOL_GROUP (dest_tool_item),
            pika_tool_group_get_active_tool (PIKA_TOOL_GROUP (src_tool_item)));
        }
      else
        {
          dest_tool_item = PIKA_TOOL_ITEM (
            pika_get_tool_info (pika, pika_object_get_name (src_tool_item)));

          if (dest_tool_item)
            {
              if (! PIKA_TOOL_INFO (dest_tool_item)->hidden)
                {
                  g_object_ref (dest_tool_item);

                  if (tools)
                    g_hash_table_add (tools, dest_tool_item);
                }
              else
                {
                  dest_tool_item = NULL;
                }
            }
        }

      if (dest_tool_item)
        {
          pika_tool_item_set_visible (
            dest_tool_item,
            pika_tool_item_get_visible (src_tool_item));

          pika_container_add (dest_container,
                              PIKA_OBJECT (dest_tool_item));

          g_object_unref (dest_tool_item);
        }
    }
}
