/* 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 Spencer Kimball and Peter Mattis
 *
 * pikatemplateview.c
 * Copyright (C) 2003 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 <string.h>

#include <gegl.h>
#include <gtk/gtk.h>

#include "libpikaconfig/pikaconfig.h"
#include "libpikawidgets/pikawidgets.h"

#include "widgets-types.h"

#include "core/pika.h"
#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikaimage.h"
#include "core/pikatemplate.h"

#include "pikacontainertreeview.h"
#include "pikacontainerview.h"
#include "pikamenufactory.h"
#include "pikatemplateview.h"
#include "pikadnd.h"
#include "pikahelp-ids.h"
#include "pikaviewrenderer.h"
#include "pikauimanager.h"

#include "pika-intl.h"


static void   pika_template_view_activate_item (PikaContainerEditor *editor,
                                                PikaViewable        *viewable);


G_DEFINE_TYPE (PikaTemplateView, pika_template_view,
               PIKA_TYPE_CONTAINER_EDITOR);

#define parent_class pika_template_view_parent_class


static void
pika_template_view_class_init (PikaTemplateViewClass *klass)
{
  PikaContainerEditorClass *editor_class = PIKA_CONTAINER_EDITOR_CLASS (klass);

  editor_class->activate_item = pika_template_view_activate_item;
}

static void
pika_template_view_init (PikaTemplateView *view)
{
  view->create_button    = NULL;
  view->new_button       = NULL;
  view->duplicate_button = NULL;
  view->edit_button      = NULL;
  view->delete_button    = NULL;
}

GtkWidget *
pika_template_view_new (PikaViewType     view_type,
                        PikaContainer   *container,
                        PikaContext     *context,
                        gint             view_size,
                        gint             view_border_width,
                        PikaMenuFactory *menu_factory)
{
  PikaTemplateView    *template_view;
  PikaContainerEditor *editor;

  g_return_val_if_fail (PIKA_IS_CONTAINER (container), NULL);
  g_return_val_if_fail (PIKA_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (view_size > 0 &&
                        view_size <= PIKA_VIEWABLE_MAX_PREVIEW_SIZE, NULL);
  g_return_val_if_fail (view_border_width >= 0 &&
                        view_border_width <= PIKA_VIEW_MAX_BORDER_WIDTH,
                        NULL);
  g_return_val_if_fail (menu_factory == NULL ||
                        PIKA_IS_MENU_FACTORY (menu_factory), NULL);

  template_view = g_object_new (PIKA_TYPE_TEMPLATE_VIEW,
                                "view-type",         view_type,
                                "container",         container,
                                "context",           context,
                                "view-size",         view_size,
                                "view-border-width", view_border_width,
                                "menu-factory",      menu_factory,
                                "menu-identifier",   "<Templates>",
                                "ui-path",           "/templates-popup",
                                NULL);

  editor = PIKA_CONTAINER_EDITOR (template_view);

  if (PIKA_IS_CONTAINER_TREE_VIEW (editor->view))
    {
      PikaContainerTreeView *tree_view;

      tree_view = PIKA_CONTAINER_TREE_VIEW (editor->view);

      pika_container_tree_view_connect_name_edited (tree_view,
                                                    G_CALLBACK (pika_container_tree_view_name_edited),
                                                    tree_view);
    }

  template_view->create_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor->view), "templates",
                                   "templates-create-image", NULL);

  template_view->new_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor->view), "templates",
                                   "templates-new", NULL);

  template_view->duplicate_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor->view), "templates",
                                   "templates-duplicate", NULL);

  template_view->edit_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor->view), "templates",
                                   "templates-edit", NULL);

  template_view->delete_button =
    pika_editor_add_action_button (PIKA_EDITOR (editor->view), "templates",
                                   "templates-delete", NULL);

  pika_container_view_enable_dnd (editor->view,
                                  GTK_BUTTON (template_view->create_button),
                                  PIKA_TYPE_TEMPLATE);
  pika_container_view_enable_dnd (editor->view,
                                  GTK_BUTTON (template_view->duplicate_button),
                                  PIKA_TYPE_TEMPLATE);
  pika_container_view_enable_dnd (editor->view,
                                  GTK_BUTTON (template_view->edit_button),
                                  PIKA_TYPE_TEMPLATE);
  pika_container_view_enable_dnd (editor->view,
                                  GTK_BUTTON (template_view->delete_button),
                                  PIKA_TYPE_TEMPLATE);

  pika_ui_manager_update (pika_editor_get_ui_manager (PIKA_EDITOR (editor->view)),
                          editor);

  return GTK_WIDGET (template_view);
}

static void
pika_template_view_activate_item (PikaContainerEditor *editor,
                                  PikaViewable        *viewable)
{
  PikaTemplateView *view = PIKA_TEMPLATE_VIEW (editor);
  PikaContainer    *container;

  if (PIKA_CONTAINER_EDITOR_CLASS (parent_class)->activate_item)
    PIKA_CONTAINER_EDITOR_CLASS (parent_class)->activate_item (editor, viewable);

  container = pika_container_view_get_container (editor->view);

  if (viewable && pika_container_have (container, PIKA_OBJECT (viewable)))
    {
      gtk_button_clicked (GTK_BUTTON (view->create_button));
    }
}
