/* 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
 *
 * pikaactionfactory.c
 * Copyright (C) 2004 Michael Natterer <mitch@gimp.org>
 *
 * 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 "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pika.h"

#include "pikaaction.h"
#include "pikaactionfactory.h"
#include "pikaactiongroup.h"


static void pika_action_factory_finalize       (GObject           *object);

static void pika_action_factory_action_removed (PikaActionGroup   *group,
                                                gchar             *action_name,
                                                PikaActionFactory *factory);


G_DEFINE_TYPE (PikaActionFactory, pika_action_factory, PIKA_TYPE_OBJECT)

#define parent_class pika_action_factory_parent_class


static void
pika_action_factory_class_init (PikaActionFactoryClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = pika_action_factory_finalize;
}

static void
pika_action_factory_init (PikaActionFactory *factory)
{
  factory->pika              = NULL;
  factory->registered_groups = NULL;
}

static void
pika_action_factory_finalize (GObject *object)
{
  PikaActionFactory *factory = PIKA_ACTION_FACTORY (object);
  GList             *list;

  for (list = factory->registered_groups; list; list = g_list_next (list))
    {
      PikaActionFactoryEntry *entry = list->data;

      g_free (entry->identifier);
      g_free (entry->label);
      g_free (entry->icon_name);
      g_hash_table_unref (entry->groups);

      g_slice_free (PikaActionFactoryEntry, entry);
    }

  g_list_free (factory->registered_groups);
  factory->registered_groups = NULL;

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
pika_action_factory_action_removed (PikaActionGroup   *group,
                                    gchar             *action_name,
                                    PikaActionFactory *factory)
{
  GList *list;

  for (list = factory->registered_groups; list; list = g_list_next (list))
    {
      PikaActionFactoryEntry *entry = list->data;
      GList                  *groups;
      GList                  *iter;

      groups = g_hash_table_get_values (entry->groups);

      for (iter = groups; iter; iter = iter->next)
        {
          if (group != iter->data)
            {
              if (g_action_group_has_action (G_ACTION_GROUP (iter->data), action_name))
                break;
            }
        }

      g_list_free (groups);

      if (iter != NULL)
        break;
    }

  if (list == NULL)
    g_action_map_remove_action (G_ACTION_MAP (factory->pika->app), action_name);
}


/* Public functions */

PikaActionFactory *
pika_action_factory_new (Pika *pika)
{
  PikaActionFactory *factory;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  factory = g_object_new (PIKA_TYPE_ACTION_FACTORY, NULL);

  factory->pika = pika;

  return factory;
}

void
pika_action_factory_group_register (PikaActionFactory         *factory,
                                    const gchar               *identifier,
                                    const gchar               *label,
                                    const gchar               *icon_name,
                                    PikaActionGroupSetupFunc   setup_func,
                                    PikaActionGroupUpdateFunc  update_func)
{
  PikaActionFactoryEntry *entry;

  g_return_if_fail (PIKA_IS_ACTION_FACTORY (factory));
  g_return_if_fail (identifier != NULL);
  g_return_if_fail (label != NULL);
  g_return_if_fail (setup_func != NULL);
  g_return_if_fail (update_func != NULL);

  entry = g_slice_new0 (PikaActionFactoryEntry);

  entry->identifier  = g_strdup (identifier);
  entry->label       = g_strdup (label);
  entry->icon_name   = g_strdup (icon_name);
  entry->setup_func  = setup_func;
  entry->update_func = update_func;
  entry->groups      = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_object_unref);

  factory->registered_groups = g_list_prepend (factory->registered_groups,
                                               entry);
}

PikaActionGroup *
pika_action_factory_get_group (PikaActionFactory *factory,
                               const gchar       *identifier,
                               gpointer           user_data)
{
  GList *list;

  g_return_val_if_fail (PIKA_IS_ACTION_FACTORY (factory), NULL);
  g_return_val_if_fail (identifier != NULL, NULL);

  for (list = factory->registered_groups; list; list = g_list_next (list))
    {
      PikaActionFactoryEntry *entry = list->data;

      if (! strcmp (entry->identifier, identifier))
        {
          PikaActionGroup *group;

          group = g_hash_table_lookup (entry->groups, user_data);

          if (group == NULL)
            {
              group = pika_action_group_new (factory->pika,
                                             entry->identifier,
                                             entry->label,
                                             entry->icon_name,
                                             user_data,
                                             entry->update_func);

              if (entry->setup_func)
                entry->setup_func (group);

              g_hash_table_insert (entry->groups, user_data, group);

              g_signal_connect_object (group, "action-removed",
                                       G_CALLBACK (pika_action_factory_action_removed),
                                       factory, G_CONNECT_AFTER);
            }

          return group;
        }
    }

  g_warning ("%s: no entry registered for \"%s\"",
             G_STRFUNC, identifier);

  return NULL;
}

void
pika_action_factory_delete_group (PikaActionFactory *factory,
                                  const gchar       *identifier,
                                  gpointer           user_data)
{
  GList *list;

  g_return_if_fail (PIKA_IS_ACTION_FACTORY (factory));
  g_return_if_fail (identifier != NULL);

  for (list = factory->registered_groups; list; list = g_list_next (list))
    {
      PikaActionFactoryEntry *entry = list->data;

      if (g_strcmp0 (entry->identifier, identifier) == 0)
        {
          if (! g_hash_table_remove (entry->groups, user_data))
            g_warning ("%s: no PikaActionGroup for (id \"%s\", data %p)",
                       G_STRFUNC, identifier, user_data);

          return;
        }
    }

  g_warning ("%s: no entry registered for \"%s\"",
             G_STRFUNC, identifier);
}
