/* 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-2003 Spencer Kimball and Peter Mattis
 *
 * pika-filter-history.c
 *
 * 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libpikabase/pikabase.h"

#include "core-types.h"

#include "config/pikacoreconfig.h"

#include "pika.h"
#include "pika-filter-history.h"

#include "pdb/pikaprocedure.h"


/*  local function prototypes  */

static gint   pika_filter_history_compare (PikaProcedure *proc1,
                                           PikaProcedure *proc2);


/*  public functions  */

gint
pika_filter_history_size (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), 0);

  return MAX (1, pika->config->filter_history_size);
}

gint
pika_filter_history_length (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), 0);

  return g_list_length (pika->filter_history);
}

PikaProcedure *
pika_filter_history_nth (Pika  *pika,
                         gint   n)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return g_list_nth_data (pika->filter_history, n);
}

void
pika_filter_history_add (Pika          *pika,
                         PikaProcedure *procedure)
{
  GList *link;

  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (PIKA_IS_PROCEDURE (procedure));

  /* return early if the procedure is already at the top */
  if (pika->filter_history &&
      pika_filter_history_compare (pika->filter_history->data, procedure) == 0)
    return;

  /* ref new first then unref old, they might be the same */
  g_object_ref (procedure);

  link = g_list_find_custom (pika->filter_history, procedure,
                             (GCompareFunc) pika_filter_history_compare);

  if (link)
    {
      g_object_unref (link->data);
      pika->filter_history = g_list_delete_link (pika->filter_history, link);
    }

  pika->filter_history = g_list_prepend (pika->filter_history, procedure);

  link = g_list_nth (pika->filter_history, pika_filter_history_size (pika));

  if (link)
    {
      g_object_unref (link->data);
      pika->filter_history = g_list_delete_link (pika->filter_history, link);
    }

  pika_filter_history_changed (pika);
}

void
pika_filter_history_remove (Pika          *pika,
                            PikaProcedure *procedure)
{
  GList *link;

  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (PIKA_IS_PROCEDURE (procedure));

  link = g_list_find_custom (pika->filter_history, procedure,
                             (GCompareFunc) pika_filter_history_compare);

  if (link)
    {
      g_object_unref (link->data);
      pika->filter_history = g_list_delete_link (pika->filter_history, link);

      pika_filter_history_changed (pika);
    }
}

void
pika_filter_history_clear (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  if (pika->filter_history)
    {
      g_list_free_full (pika->filter_history, (GDestroyNotify) g_object_unref);
      pika->filter_history = NULL;

      pika_filter_history_changed (pika);
    }
}


/*  private functions  */

static gint
pika_filter_history_compare (PikaProcedure *proc1,
                             PikaProcedure *proc2)
{
  /*  the procedures can have the same name, but could still be two
   *  different filters using the same operation, so also compare
   *  their menu labels
   */
  return (pika_procedure_name_compare (proc1, proc2) ||
          strcmp (pika_procedure_get_menu_label (proc1),
                  pika_procedure_get_menu_label (proc2)));
}
