/* 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-2002 Spencer Kimball, Peter Mattis, and others
 *
 * pika-gradients.c
 * Copyright (C) 2014 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libpikabase/pikabase.h"

#include "core-types.h"

#include "pika.h"
#include "pika-palettes.h"
#include "pikacontext.h"
#include "pikacontainer.h"
#include "pikadatafactory.h"
#include "pikapalettemru.h"

#include "pika-intl.h"


#define COLOR_HISTORY_KEY "pika-palette-color-history"


/*  local function prototypes  */

static PikaPalette * pika_palettes_add_palette (Pika        *pika,
                                                const gchar *name,
                                                const gchar *id);


/*  public functions  */

void
pika_palettes_init (Pika *pika)
{
  PikaPalette *palette;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  palette = pika_palettes_add_palette (pika,
                                       _("Color History"),
                                       COLOR_HISTORY_KEY);
  pika_context_set_palette (pika->user_context, palette);
}

void
pika_palettes_load (Pika *pika)
{
  PikaPalette *palette;
  GFile       *file;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  palette = pika_palettes_get_color_history (pika);

  file = pika_directory_file ("colorrc", NULL);

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

  pika_palette_mru_load (PIKA_PALETTE_MRU (palette), file);

  g_object_unref (file);
}

void
pika_palettes_save (Pika *pika)
{
  PikaPalette *palette;
  GFile       *file;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  palette = pika_palettes_get_color_history (pika);

  file = pika_directory_file ("colorrc", NULL);

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

  pika_palette_mru_save (PIKA_PALETTE_MRU (palette), file);

  g_object_unref (file);
}

PikaPalette *
pika_palettes_get_color_history (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return g_object_get_data (G_OBJECT (pika), COLOR_HISTORY_KEY);
}

void
pika_palettes_add_color_history (Pika          *pika,
                                 const PikaRGB *color)
{
  PikaPalette *history;

  history = pika_palettes_get_color_history (pika);
  pika_palette_mru_add (PIKA_PALETTE_MRU (history), color);
}

/*  private functions  */

static PikaPalette *
pika_palettes_add_palette (Pika        *pika,
                           const gchar *name,
                           const gchar *id)
{
  PikaData *palette;

  palette = pika_palette_mru_new (name);

  pika_data_make_internal (palette, id);

  pika_container_add (pika_data_factory_get_container (pika->palette_factory),
                      PIKA_OBJECT (palette));
  g_object_unref (palette);

  g_object_set_data (G_OBJECT (pika), id, palette);

  return PIKA_PALETTE (palette);
}
