/* 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-1997 Peter Mattis and Spencer Kimball
 *
 * pikacolorselectorpalette.c
 * Copyright (C) 2006 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 <gegl.h>
#include <gtk/gtk.h>

#include "libpikacolor/pikacolor.h"
#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pikacontext.h"
#include "core/pikapalette.h"

#include "pikacolorselectorpalette.h"
#include "pikahelp-ids.h"
#include "pikapaletteview.h"
#include "pikaviewrendererpalette.h"

#include "pika-intl.h"


static void   pika_color_selector_palette_set_color  (PikaColorSelector *selector,
                                                      const PikaRGB     *rgb,
                                                      const PikaHSV     *hsv);
static void   pika_color_selector_palette_set_config (PikaColorSelector *selector,
                                                      PikaColorConfig   *config);


G_DEFINE_TYPE (PikaColorSelectorPalette, pika_color_selector_palette,
               PIKA_TYPE_COLOR_SELECTOR)

#define parent_class pika_color_selector_palette_parent_class


static void
pika_color_selector_palette_class_init (PikaColorSelectorPaletteClass *klass)
{
  PikaColorSelectorClass *selector_class = PIKA_COLOR_SELECTOR_CLASS (klass);

  selector_class->name       = _("Palette");
  selector_class->help_id    = PIKA_HELP_COLORSELECTOR_PALETTE;
  selector_class->icon_name  = PIKA_ICON_PALETTE;
  selector_class->set_color  = pika_color_selector_palette_set_color;
  selector_class->set_config = pika_color_selector_palette_set_config;

  gtk_widget_class_set_css_name (GTK_WIDGET_CLASS (klass),
                                 "PikaColorSelectorPalette");
}

static void
pika_color_selector_palette_init (PikaColorSelectorPalette *select)
{
}

static void
pika_color_selector_palette_set_color (PikaColorSelector *selector,
                                       const PikaRGB     *rgb,
                                       const PikaHSV     *hsv)
{
  PikaColorSelectorPalette *select = PIKA_COLOR_SELECTOR_PALETTE (selector);

  if (select->context)
    {
      PikaPalette *palette = pika_context_get_palette (select->context);

      if (palette && pika_palette_get_n_colors (palette) > 0)
        {
          PikaPaletteEntry *entry;

          entry = pika_palette_find_entry (palette, rgb,
                                           PIKA_PALETTE_VIEW (select->view)->selected);

          if (entry)
            pika_palette_view_select_entry (PIKA_PALETTE_VIEW (select->view),
                                            entry);
        }
    }
}

static void
pika_color_selector_palette_palette_changed (PikaContext              *context,
                                             PikaPalette              *palette,
                                             PikaColorSelectorPalette *select)
{
  pika_view_set_viewable (PIKA_VIEW (select->view), PIKA_VIEWABLE (palette));
}

static void
pika_color_selector_palette_entry_clicked (PikaPaletteView   *view,
                                           PikaPaletteEntry  *entry,
                                           GdkModifierType    state,
                                           PikaColorSelector *selector)
{
  selector->rgb = entry->color;
  pika_rgb_to_hsv (&selector->rgb, &selector->hsv);

  pika_color_selector_emit_color_changed (selector);
}

static void
pika_color_selector_palette_set_config (PikaColorSelector *selector,
                                        PikaColorConfig   *config)
{
  PikaColorSelectorPalette *select = PIKA_COLOR_SELECTOR_PALETTE (selector);

  if (select->context)
    {
      g_signal_handlers_disconnect_by_func (select->context,
                                            pika_color_selector_palette_palette_changed,
                                            select);
      pika_view_renderer_set_context (PIKA_VIEW (select->view)->renderer,
                                      NULL);

      g_clear_object (&select->context);
    }

  if (config)
    select->context = g_object_get_data (G_OBJECT (config), "pika-context");

  if (select->context)
    {
      g_object_ref (select->context);

      if (! select->view)
        {
          select->view = pika_view_new_full_by_types (select->context,
                                                      PIKA_TYPE_PALETTE_VIEW,
                                                      PIKA_TYPE_PALETTE,
                                                      100, 100, 0,
                                                      FALSE, TRUE, FALSE);
          pika_view_set_expand (PIKA_VIEW (select->view), TRUE);
          pika_view_renderer_palette_set_cell_size
            (PIKA_VIEW_RENDERER_PALETTE (PIKA_VIEW (select->view)->renderer),
             -1);
          pika_view_renderer_palette_set_draw_grid
            (PIKA_VIEW_RENDERER_PALETTE (PIKA_VIEW (select->view)->renderer),
             TRUE);
          gtk_box_pack_start (GTK_BOX (select), select->view, TRUE, TRUE, 0);
          gtk_widget_show (select->view);

          g_signal_connect (select->view, "entry-clicked",
                            G_CALLBACK (pika_color_selector_palette_entry_clicked),
                            select);
        }
      else
        {
          pika_view_renderer_set_context (PIKA_VIEW (select->view)->renderer,
                                          select->context);
        }

      g_signal_connect_object (select->context, "palette-changed",
                               G_CALLBACK (pika_color_selector_palette_palette_changed),
                               select, 0);

      pika_color_selector_palette_palette_changed (select->context,
                                                   pika_context_get_palette (select->context),
                                                   select);
    }
}
