/* 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 "libpikabase/pikabase.h"
#include "libpikawidgets/pikawidgets.h"

#include "actions-types.h"

#include "core/pika.h"
#include "core/pikacontext.h"
#include "core/pikadrawable.h"
#include "core/pikaimage.h"

#include "plug-in/pikapluginmanager.h"
#include "plug-in/pikapluginmanager-help-domain.h"
#include "plug-in/pikapluginmanager-menu-branch.h"
#include "plug-in/pikapluginprocedure.h"

#include "widgets/pikaaction.h"
#include "widgets/pikaactiongroup.h"
#include "widgets/pikaactionimpl.h"
#include "widgets/pikahelp-ids.h"

#include "actions.h"
#include "plug-in-actions.h"
#include "plug-in-commands.h"

#include "pika-intl.h"


/*  local function prototypes  */

static void     plug_in_actions_register_procedure   (PikaPDB             *pdb,
                                                      PikaProcedure       *procedure,
                                                      PikaActionGroup     *group);
static void     plug_in_actions_unregister_procedure (PikaPDB             *pdb,
                                                      PikaProcedure       *procedure,
                                                      PikaActionGroup     *group);
static void     plug_in_actions_add_proc             (PikaActionGroup     *group,
                                                      PikaPlugInProcedure *proc);


/*  private variables  */

static const PikaActionEntry plug_in_actions[] =
{
  { "plug-in-reset-all", PIKA_ICON_RESET,
    NC_("plug-in-action", "Reset all _Filters"), NULL, { NULL },
    NC_("plug-in-action", "Reset all plug-ins to their default settings"),
    plug_in_reset_all_cmd_callback,
    PIKA_HELP_FILTER_RESET_ALL }
};


/*  public functions  */

void
plug_in_actions_setup (PikaActionGroup *group)
{
  PikaPlugInManager *manager = group->pika->plug_in_manager;
  GSList            *list;

  pika_action_group_add_actions (group, "plug-in-action",
                                 plug_in_actions,
                                 G_N_ELEMENTS (plug_in_actions));

  for (list = manager->plug_in_procedures;
       list;
       list = g_slist_next (list))
    {
      PikaPlugInProcedure *plug_in_proc = list->data;

      if (plug_in_proc->file)
        plug_in_actions_register_procedure (group->pika->pdb,
                                            PIKA_PROCEDURE (plug_in_proc),
                                            group);
    }

  g_signal_connect_object (group->pika->pdb, "register-procedure",
                           G_CALLBACK (plug_in_actions_register_procedure),
                           group, 0);
  g_signal_connect_object (group->pika->pdb, "unregister-procedure",
                           G_CALLBACK (plug_in_actions_unregister_procedure),
                           group, 0);
}

void
plug_in_actions_update (PikaActionGroup *group,
                        gpointer         data)
{
  PikaImage         *image    = action_data_get_image (data);
  PikaPlugInManager *manager  = group->pika->plug_in_manager;
  GSList            *list;

  for (list = manager->plug_in_procedures; list; list = g_slist_next (list))
    {
      PikaPlugInProcedure *proc = list->data;

      if (proc->menu_label  &&
          ! proc->file_proc &&
          proc->image_types_val)
        {
          PikaProcedure *procedure = PIKA_PROCEDURE (proc);
          gboolean       sensitive;
          const gchar   *tooltip;
          const gchar   *reason;

          sensitive = pika_procedure_get_sensitive (procedure,
                                                    PIKA_OBJECT (image),
                                                    &reason);

          pika_action_group_set_action_sensitive (group,
                                                  pika_object_get_name (proc),
                                                  sensitive, reason);

          tooltip = pika_procedure_get_blurb (procedure);
          if (tooltip)
            pika_action_group_set_action_tooltip (group,
                                                  pika_object_get_name (proc),
                                                  tooltip);
        }
    }
}


/*  private functions  */

static void
plug_in_actions_register_procedure (PikaPDB         *pdb,
                                    PikaProcedure   *procedure,
                                    PikaActionGroup *group)
{
  if (PIKA_IS_PLUG_IN_PROCEDURE (procedure))
    {
      PikaPlugInProcedure *plug_in_proc = PIKA_PLUG_IN_PROCEDURE (procedure);

      if (plug_in_proc->menu_label &&
          ! plug_in_proc->file_proc)
        {
#if 0
          g_print ("%s: %s\n", G_STRFUNC,
                   pika_object_get_name (procedure));
#endif

          plug_in_actions_add_proc (group, plug_in_proc);
        }
    }
}

static void
plug_in_actions_unregister_procedure (PikaPDB         *pdb,
                                      PikaProcedure   *procedure,
                                      PikaActionGroup *group)
{
  if (PIKA_IS_PLUG_IN_PROCEDURE (procedure))
    {
      PikaPlugInProcedure *plug_in_proc = PIKA_PLUG_IN_PROCEDURE (procedure);

      if (plug_in_proc->menu_label &&
          ! plug_in_proc->file_proc)
        {
          PikaAction *action;

#if 0
          g_print ("%s: %s\n", G_STRFUNC,
                   pika_object_get_name (procedure));
#endif

          action = pika_action_group_get_action (group,
                                                 pika_object_get_name (procedure));

          if (action)
            pika_action_group_remove_action (group, action);
        }
    }
}

static void
plug_in_actions_add_proc (PikaActionGroup     *group,
                          PikaPlugInProcedure *proc)
{
  PikaProcedureActionEntry  entry;

  entry.name        = pika_object_get_name (proc);
  entry.icon_name   = pika_viewable_get_icon_name (PIKA_VIEWABLE (proc));
  entry.label       = pika_procedure_get_menu_label (PIKA_PROCEDURE (proc));
  entry.accelerator = NULL;
  entry.tooltip     = pika_procedure_get_blurb (PIKA_PROCEDURE (proc));
  entry.procedure   = PIKA_PROCEDURE (proc);
  entry.help_id     = pika_procedure_get_help_id (PIKA_PROCEDURE (proc));

  pika_action_group_add_procedure_actions (group, &entry, 1,
                                           plug_in_run_cmd_callback);

  if (proc->image_types_val)
    {
      PikaContext  *context  = pika_get_user_context (group->pika);
      PikaImage    *image    = pika_context_get_image (context);
      gboolean      sensitive;
      const gchar  *tooltip;
      const gchar  *reason;

      sensitive = pika_procedure_get_sensitive (PIKA_PROCEDURE (proc),
                                                PIKA_OBJECT (image),
                                                &reason);

      pika_action_group_set_action_sensitive (group,
                                              pika_object_get_name (proc),
                                              sensitive, reason);

      tooltip = pika_procedure_get_blurb (PIKA_PROCEDURE (proc));
      if (tooltip)
        pika_action_group_set_action_tooltip (group,
                                              pika_object_get_name (proc),
                                              tooltip);
    }
}
