/* 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
 *
 * 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 "libpikaconfig/pikaconfig.h"

#include "core-types.h"

#include "config/pikarc.h"

#include "pika.h"
#include "pika-data-factories.h"
#include "pika-gradients.h"
#include "pika-memsize.h"
#include "pika-palettes.h"
#include "pikacontainer.h"
#include "pikabrush-load.h"
#include "pikabrush.h"
#include "pikabrushclipboard.h"
#include "pikabrushgenerated-load.h"
#include "pikabrushpipe-load.h"
#include "pikadataloaderfactory.h"
#include "pikadynamics.h"
#include "pikadynamics-load.h"
#include "pikagradient-load.h"
#include "pikagradient.h"
#include "pikamybrush-load.h"
#include "pikamybrush.h"
#include "pikapalette-load.h"
#include "pikapalette.h"
#include "pikapattern-load.h"
#include "pikapattern.h"
#include "pikapatternclipboard.h"
#include "pikatagcache.h"
#include "pikatoolpreset.h"
#include "pikatoolpreset-load.h"

#include "text/pikafontfactory.h"

#include "pika-intl.h"


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

  pika->brush_factory =
    pika_data_loader_factory_new (pika,
                                  PIKA_TYPE_BRUSH,
                                  "brush-path",
                                  "brush-path-writable",
                                  "brush-paths",
                                  pika_brush_new,
                                  pika_brush_get_standard);
  pika_object_set_static_name (PIKA_OBJECT (pika->brush_factory),
                               "brush factory");
  pika_data_loader_factory_add_loader (pika->brush_factory,
                                       "PIKA Brush",
                                       pika_brush_load,
                                       PIKA_BRUSH_FILE_EXTENSION,
                                       TRUE);
  pika_data_loader_factory_add_loader (pika->brush_factory,
                                       "PIKA Brush Pixmap",
                                       pika_brush_load,
                                       PIKA_BRUSH_PIXMAP_FILE_EXTENSION,
                                       FALSE);
  pika_data_loader_factory_add_loader (pika->brush_factory,
                                       "Photoshop ABR Brush",
                                       pika_brush_load_abr,
                                       PIKA_BRUSH_PS_FILE_EXTENSION,
                                       FALSE);
  pika_data_loader_factory_add_loader (pika->brush_factory,
                                       "Paint Shop Pro JBR Brush",
                                       pika_brush_load_abr,
                                       PIKA_BRUSH_PSP_FILE_EXTENSION,
                                       FALSE);
 pika_data_loader_factory_add_loader (pika->brush_factory,
                                       "PIKA Generated Brush",
                                       pika_brush_generated_load,
                                       PIKA_BRUSH_GENERATED_FILE_EXTENSION,
                                       TRUE);
  pika_data_loader_factory_add_loader (pika->brush_factory,
                                       "PIKA Brush Pipe",
                                       pika_brush_pipe_load,
                                       PIKA_BRUSH_PIPE_FILE_EXTENSION,
                                       TRUE);

  pika->dynamics_factory =
    pika_data_loader_factory_new (pika,
                                  PIKA_TYPE_DYNAMICS,
                                  "dynamics-path",
                                  "dynamics-path-writable",
                                  "dynamics-paths",
                                  pika_dynamics_new,
                                  pika_dynamics_get_standard);
  pika_object_set_static_name (PIKA_OBJECT (pika->dynamics_factory),
                               "dynamics factory");
  pika_data_loader_factory_add_loader (pika->dynamics_factory,
                                       "PIKA Paint Dynamics",
                                       pika_dynamics_load,
                                       PIKA_DYNAMICS_FILE_EXTENSION,
                                       TRUE);

  pika->mybrush_factory =
    pika_data_loader_factory_new (pika,
                                  PIKA_TYPE_MYBRUSH,
                                  "mypaint-brush-path",
                                  "mypaint-brush-path-writable",
                                  "mypaint-brush-paths",
                                  NULL,
                                  NULL);
  pika_object_set_static_name (PIKA_OBJECT (pika->mybrush_factory),
                               "mypaint brush factory");
  pika_data_loader_factory_add_loader (pika->mybrush_factory,
                                       "MyPaint Brush",
                                       pika_mybrush_load,
                                       PIKA_MYBRUSH_FILE_EXTENSION,
                                       FALSE);

  pika->pattern_factory =
    pika_data_loader_factory_new (pika,
                                  PIKA_TYPE_PATTERN,
                                  "pattern-path",
                                  "pattern-path-writable",
                                  "pattern-paths",
                                  NULL,
                                  pika_pattern_get_standard);
  pika_object_set_static_name (PIKA_OBJECT (pika->pattern_factory),
                               "pattern factory");
  pika_data_loader_factory_add_loader (pika->pattern_factory,
                                       "PIKA Pattern",
                                       pika_pattern_load,
                                       PIKA_PATTERN_FILE_EXTENSION,
                                       TRUE);
  pika_data_loader_factory_add_fallback (pika->pattern_factory,
                                         "Pattern from GdkPixbuf",
                                         pika_pattern_load_pixbuf);

  pika->gradient_factory =
    pika_data_loader_factory_new (pika,
                                  PIKA_TYPE_GRADIENT,
                                  "gradient-path",
                                  "gradient-path-writable",
                                  "gradient-paths",
                                  pika_gradient_new,
                                  pika_gradient_get_standard);
  pika_object_set_static_name (PIKA_OBJECT (pika->gradient_factory),
                               "gradient factory");
  pika_data_loader_factory_add_loader (pika->gradient_factory,
                                       "PIKA Gradient",
                                       pika_gradient_load,
                                       PIKA_GRADIENT_FILE_EXTENSION,
                                       TRUE);
  pika_data_loader_factory_add_loader (pika->gradient_factory,
                                       "SVG Gradient",
                                       pika_gradient_load_svg,
                                       PIKA_GRADIENT_SVG_FILE_EXTENSION,
                                       FALSE);

  pika->palette_factory =
    pika_data_loader_factory_new (pika,
                                  PIKA_TYPE_PALETTE,
                                  "palette-path",
                                  "palette-path-writable",
                                  "palette-paths",
                                  pika_palette_new,
                                  pika_palette_get_standard);
  pika_object_set_static_name (PIKA_OBJECT (pika->palette_factory),
                               "palette factory");
  pika_data_loader_factory_add_loader (pika->palette_factory,
                                       "PIKA Palette",
                                       pika_palette_load,
                                       PIKA_PALETTE_FILE_EXTENSION,
                                       TRUE);

  pika->font_factory =
    pika_font_factory_new (pika,
                           "font-path");
  pika_object_set_static_name (PIKA_OBJECT (pika->font_factory),
                               "font factory");

  pika->tool_preset_factory =
    pika_data_loader_factory_new (pika,
                                  PIKA_TYPE_TOOL_PRESET,
                                  "tool-preset-path",
                                  "tool-preset-path-writable",
                                  "tool-preset-paths",
                                  pika_tool_preset_new,
                                  NULL);
  pika_object_set_static_name (PIKA_OBJECT (pika->tool_preset_factory),
                               "tool preset factory");
  pika_data_loader_factory_add_loader (pika->tool_preset_factory,
                                       "PIKA Tool Preset",
                                       pika_tool_preset_load,
                                       PIKA_TOOL_PRESET_FILE_EXTENSION,
                                       TRUE);

  pika->tag_cache = pika_tag_cache_new ();
}

void
pika_data_factories_add_builtin (Pika *pika)
{
  PikaData *data;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  /*  add the builtin FG -> BG etc. gradients  */
  pika_gradients_init (pika);

  /*  add the color history palette  */
  pika_palettes_init (pika);

  /*  add the clipboard brushes  */
  data = pika_brush_clipboard_new (pika, FALSE);
  pika_data_make_internal (data, "pika-brush-clipboard-image");
  pika_container_add (pika_data_factory_get_container (pika->brush_factory),
                      PIKA_OBJECT (data));
  g_object_unref (data);

  data = pika_brush_clipboard_new (pika, TRUE);
  pika_data_make_internal (data, "pika-brush-clipboard-mask");
  pika_container_add (pika_data_factory_get_container (pika->brush_factory),
                      PIKA_OBJECT (data));
  g_object_unref (data);

  /*  add the clipboard pattern  */
  data = pika_pattern_clipboard_new (pika);
  pika_data_make_internal (data, "pika-pattern-clipboard-image");
  pika_container_add (pika_data_factory_get_container (pika->pattern_factory),
                      PIKA_OBJECT (data));
  g_object_unref (data);
}

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

  if (pika->brush_factory)
    pika_data_factory_data_free (pika->brush_factory);

  if (pika->dynamics_factory)
    pika_data_factory_data_free (pika->dynamics_factory);

  if (pika->mybrush_factory)
    pika_data_factory_data_free (pika->mybrush_factory);

  if (pika->pattern_factory)
    pika_data_factory_data_free (pika->pattern_factory);

  if (pika->gradient_factory)
    pika_data_factory_data_free (pika->gradient_factory);

  if (pika->palette_factory)
    pika_data_factory_data_free (pika->palette_factory);

  if (pika->font_factory)
    pika_data_factory_data_free (pika->font_factory);

  if (pika->tool_preset_factory)
    pika_data_factory_data_free (pika->tool_preset_factory);
}

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

  g_clear_object (&pika->brush_factory);
  g_clear_object (&pika->dynamics_factory);
  g_clear_object (&pika->mybrush_factory);
  g_clear_object (&pika->pattern_factory);
  g_clear_object (&pika->gradient_factory);
  g_clear_object (&pika->palette_factory);
  g_clear_object (&pika->font_factory);
  g_clear_object (&pika->tool_preset_factory);
  g_clear_object (&pika->tag_cache);
}

gint64
pika_data_factories_get_memsize (Pika   *pika,
                                 gint64 *gui_size)
{
  gint64 memsize = 0;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), 0);

  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->named_buffers),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->brush_factory),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->dynamics_factory),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->mybrush_factory),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->pattern_factory),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->gradient_factory),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->palette_factory),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->font_factory),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->tool_preset_factory),
                                      gui_size);

  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->tag_cache),
                                      gui_size);

  return memsize;
}

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

  pika_data_factory_data_clean (pika->brush_factory);
  pika_data_factory_data_clean (pika->dynamics_factory);
  pika_data_factory_data_clean (pika->mybrush_factory);
  pika_data_factory_data_clean (pika->pattern_factory);
  pika_data_factory_data_clean (pika->gradient_factory);
  pika_data_factory_data_clean (pika->palette_factory);
  pika_data_factory_data_clean (pika->font_factory);
  pika_data_factory_data_clean (pika->tool_preset_factory);
}

void
pika_data_factories_load (Pika               *pika,
                          PikaInitStatusFunc  status_callback)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  /*  initialize the list of pika brushes    */
  status_callback (NULL, _("Brushes"), 0.1);
  pika_data_factory_data_init (pika->brush_factory, pika->user_context,
                               pika->no_data);

  /*  initialize the list of pika dynamics   */
  status_callback (NULL, _("Dynamics"), 0.15);
  pika_data_factory_data_init (pika->dynamics_factory, pika->user_context,
                               pika->no_data);

  /*  initialize the list of mypaint brushes    */
  status_callback (NULL, _("MyPaint Brushes"), 0.2);
  pika_data_factory_data_init (pika->mybrush_factory, pika->user_context,
                               pika->no_data);

  /*  initialize the list of pika patterns   */
  status_callback (NULL, _("Patterns"), 0.3);
  pika_data_factory_data_init (pika->pattern_factory, pika->user_context,
                               pika->no_data);

  /*  initialize the list of pika palettes   */
  status_callback (NULL, _("Palettes"), 0.4);
  pika_data_factory_data_init (pika->palette_factory, pika->user_context,
                               pika->no_data);

  /*  initialize the list of pika gradients  */
  status_callback (NULL, _("Gradients"), 0.5);
  pika_data_factory_data_init (pika->gradient_factory, pika->user_context,
                               pika->no_data);

  /*  initialize the color history   */
  status_callback (NULL, _("Color History"), 0.55);
  pika_palettes_load (pika);

  /*  initialize the list of pika fonts   */
  status_callback (NULL, _("Fonts"), 0.6);
  pika_data_factory_data_init (pika->font_factory, pika->user_context,
                               pika->no_fonts);

  /*  initialize the list of pika tool presets if we have a GUI  */
  if (! pika->no_interface)
    {
      status_callback (NULL, _("Tool Presets"), 0.7);
      pika_data_factory_data_init (pika->tool_preset_factory, pika->user_context,
                                   pika->no_data);
    }

  /* update tag cache */
  status_callback (NULL, _("Updating tag cache"), 0.75);
  pika_tag_cache_load (pika->tag_cache);
  pika_tag_cache_add_container (pika->tag_cache,
                                pika_data_factory_get_container (pika->brush_factory));
  pika_tag_cache_add_container (pika->tag_cache,
                                pika_data_factory_get_container (pika->dynamics_factory));
  pika_tag_cache_add_container (pika->tag_cache,
                                pika_data_factory_get_container (pika->mybrush_factory));
  pika_tag_cache_add_container (pika->tag_cache,
                                pika_data_factory_get_container (pika->pattern_factory));
  pika_tag_cache_add_container (pika->tag_cache,
                                pika_data_factory_get_container (pika->gradient_factory));
  pika_tag_cache_add_container (pika->tag_cache,
                                pika_data_factory_get_container (pika->palette_factory));
  pika_tag_cache_add_container (pika->tag_cache,
                                pika_data_factory_get_container (pika->font_factory));
  pika_tag_cache_add_container (pika->tag_cache,
                                pika_data_factory_get_container (pika->tool_preset_factory));
}

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

  pika_tag_cache_save (pika->tag_cache);

  pika_data_factory_data_save (pika->brush_factory);
  pika_data_factory_data_save (pika->dynamics_factory);
  pika_data_factory_data_save (pika->mybrush_factory);
  pika_data_factory_data_save (pika->pattern_factory);
  pika_data_factory_data_save (pika->gradient_factory);
  pika_data_factory_data_save (pika->palette_factory);
  pika_data_factory_data_save (pika->font_factory);
  pika_data_factory_data_save (pika->tool_preset_factory);

  pika_palettes_save (pika);
}
