/* 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-1999 Spencer Kimball and Peter Mattis
 *
 * pikafilleditor.c
 * Copyright (C) 2008 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 "libpikabase/pikabase.h"
#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pikafilloptions.h"

#include "pikacolorpanel.h"
#include "pikafilleditor.h"
#include "pikapropwidgets.h"
#include "pikaviewablebox.h"
#include "pikawidgets-utils.h"

#include "pika-intl.h"


enum
{
  PROP_0,
  PROP_OPTIONS,
  PROP_EDIT_CONTEXT,
  PROP_USE_CUSTOM_STYLE
};


static void   pika_fill_editor_constructed  (GObject      *object);
static void   pika_fill_editor_finalize     (GObject      *object);
static void   pika_fill_editor_set_property (GObject      *object,
                                             guint         property_id,
                                             const GValue *value,
                                             GParamSpec   *pspec);
static void   pika_fill_editor_get_property (GObject      *object,
                                             guint         property_id,
                                             GValue       *value,
                                             GParamSpec   *pspec);


G_DEFINE_TYPE (PikaFillEditor, pika_fill_editor, GTK_TYPE_BOX)

#define parent_class pika_fill_editor_parent_class


static void
pika_fill_editor_class_init (PikaFillEditorClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed  = pika_fill_editor_constructed;
  object_class->finalize     = pika_fill_editor_finalize;
  object_class->set_property = pika_fill_editor_set_property;
  object_class->get_property = pika_fill_editor_get_property;

  g_object_class_install_property (object_class, PROP_OPTIONS,
                                   g_param_spec_object ("options",
                                                        NULL, NULL,
                                                        PIKA_TYPE_FILL_OPTIONS,
                                                        PIKA_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class, PROP_EDIT_CONTEXT,
                                   g_param_spec_boolean ("edit-context",
                                                         NULL, NULL,
                                                         FALSE,
                                                         PIKA_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT_ONLY));

  g_object_class_install_property (object_class, PROP_EDIT_CONTEXT,
                                   g_param_spec_boolean ("use-custom-style",
                                                         NULL, NULL,
                                                         FALSE,
                                                         PIKA_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT_ONLY));
}

static void
pika_fill_editor_init (PikaFillEditor *editor)
{
  gtk_orientable_set_orientation (GTK_ORIENTABLE (editor),
                                  GTK_ORIENTATION_VERTICAL);

  gtk_box_set_spacing (GTK_BOX (editor), 6);
}

static void
pika_fill_editor_constructed (GObject *object)
{
  PikaFillEditor *editor = PIKA_FILL_EDITOR (object);
  GtkWidget      *box;
  GtkWidget      *button;

  G_OBJECT_CLASS (parent_class)->constructed (object);

  pika_assert (PIKA_IS_FILL_OPTIONS (editor->options));

  g_object_get (object,
                "use-custom-style", &editor->use_custom_style,
                NULL);

  if (editor->use_custom_style)
    box = pika_prop_enum_radio_box_new (G_OBJECT (editor->options),
                                        "custom-style", 0, 0);
  else
    box = pika_prop_enum_radio_box_new (G_OBJECT (editor->options), "style",
                                        0, 0);

  gtk_box_pack_start (GTK_BOX (editor), box, FALSE, FALSE, 0);

  if (editor->edit_context)
    {
      GtkWidget *color_button;
      GtkWidget *pattern_box;

      if (editor->use_custom_style)
        {
          color_button = pika_prop_color_button_new (G_OBJECT (editor->options),
                                                     "foreground",
                                                     _("Fill Color"),
                                                     1, 24,
                                                     PIKA_COLOR_AREA_SMALL_CHECKS);
          pika_color_panel_set_context (PIKA_COLOR_PANEL (color_button),
                                        PIKA_CONTEXT (editor->options));
          pika_enum_radio_box_add (GTK_BOX (box), color_button,
                                   PIKA_CUSTOM_STYLE_SOLID_COLOR, FALSE);
        }
      else
        {
          color_button = pika_prop_color_button_new (G_OBJECT (editor->options),
                                                     "foreground",
                                                     _("Fill Color"),
                                                     1, 24,
                                                     PIKA_COLOR_AREA_SMALL_CHECKS);
          pika_color_panel_set_context (PIKA_COLOR_PANEL (color_button),
                                        PIKA_CONTEXT (editor->options));
          pika_enum_radio_box_add (GTK_BOX (box), color_button,
                                   PIKA_FILL_STYLE_FG_COLOR, FALSE);

          color_button = pika_prop_color_button_new (G_OBJECT (editor->options),
                                                     "background",
                                                     _("Fill BG Color"),
                                                     1, 24,
                                                     PIKA_COLOR_AREA_SMALL_CHECKS);
          pika_color_panel_set_context (PIKA_COLOR_PANEL (color_button),
                                        PIKA_CONTEXT (editor->options));
          pika_enum_radio_box_add (GTK_BOX (box), color_button,
                                   PIKA_FILL_STYLE_BG_COLOR, FALSE);
        }

      pattern_box = pika_prop_pattern_box_new (NULL,
                                               PIKA_CONTEXT (editor->options),
                                               NULL, 2,
                                               "pattern-view-type",
                                               "pattern-view-size");
      pika_enum_radio_box_add (GTK_BOX (box), pattern_box,
                               (editor->use_custom_style ?
                                PIKA_CUSTOM_STYLE_PATTERN :
                                PIKA_FILL_STYLE_PATTERN),
                               FALSE);
    }

  button = pika_prop_check_button_new (G_OBJECT (editor->options),
                                       "antialias",
                                       _("_Antialiasing"));
  gtk_box_pack_start (GTK_BOX (editor), button, FALSE, FALSE, 0);
}

static void
pika_fill_editor_finalize (GObject *object)
{
  PikaFillEditor *editor = PIKA_FILL_EDITOR (object);

  g_clear_object (&editor->options);

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

static void
pika_fill_editor_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  PikaFillEditor *editor = PIKA_FILL_EDITOR (object);

  switch (property_id)
    {
    case PROP_OPTIONS:
      if (editor->options)
        g_object_unref (editor->options);
      editor->options = g_value_dup_object (value);
      break;

    case PROP_EDIT_CONTEXT:
      editor->edit_context = g_value_get_boolean (value);
      break;

    case PROP_USE_CUSTOM_STYLE:
      editor->use_custom_style = g_value_get_boolean (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_fill_editor_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  PikaFillEditor *editor = PIKA_FILL_EDITOR (object);

  switch (property_id)
    {
    case PROP_OPTIONS:
      g_value_set_object (value, editor->options);
      break;

    case PROP_EDIT_CONTEXT:
      g_value_set_boolean (value, editor->edit_context);
      break;

    case PROP_USE_CUSTOM_STYLE:
      g_value_set_boolean (value, editor->use_custom_style);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

GtkWidget *
pika_fill_editor_new (PikaFillOptions *options,
                      gboolean         edit_context,
                      gboolean         use_custom_style)
{
  g_return_val_if_fail (PIKA_IS_FILL_OPTIONS (options), NULL);

  return g_object_new (PIKA_TYPE_FILL_EDITOR,
                       "options",          options,
                       "edit-context",     edit_context ? TRUE : FALSE,
                       "use_custom_style", use_custom_style ? TRUE : FALSE,
                       NULL);
}
