/* 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
 *
 * 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 "actions-types.h"

#include "core/pika.h"
#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikadata.h"
#include "core/pikadatafactory.h"

#include "file/file-open.h"

#include "widgets/pikaclipboard.h"
#include "widgets/pikacontainerview.h"
#include "widgets/pikadataeditor.h"
#include "widgets/pikadatafactoryview.h"
#include "widgets/pikadialogfactory.h"
#include "widgets/pikamessagebox.h"
#include "widgets/pikamessagedialog.h"
#include "widgets/pikawidgets-utils.h"
#include "widgets/pikawindowstrategy.h"
#include "widgets/pikawidgets-utils.h"

#include "dialogs/data-delete-dialog.h"

#include "actions.h"
#include "data-commands.h"

#include "pika-intl.h"


/*  public functions  */

void
data_open_as_image_cmd_callback (PikaAction *action,
                                 GVariant   *value,
                                 gpointer    user_data)
{
  PikaDataFactoryView *view = PIKA_DATA_FACTORY_VIEW (user_data);
  PikaContext         *context;
  PikaData            *data;

  context =
    pika_container_view_get_context (PIKA_CONTAINER_EDITOR (view)->view);

  data = (PikaData *)
    pika_context_get_by_type (context,
                              pika_data_factory_view_get_children_type (view));

  if (data && pika_data_get_file (data))
    {
      GFile             *file   = pika_data_get_file (data);
      GtkWidget         *widget = GTK_WIDGET (view);
      PikaImage         *image;
      PikaPDBStatusType  status;
      GError            *error = NULL;

      image = file_open_with_display (context->pika, context, NULL,
                                      file, FALSE,
                                      G_OBJECT (pika_widget_get_monitor (widget)),
                                      &status, &error);

      if (! image && status != PIKA_PDB_CANCEL)
        {
          pika_message (context->pika, G_OBJECT (view),
                        PIKA_MESSAGE_ERROR,
                        _("Opening '%s' failed:\n\n%s"),
                        pika_file_get_utf8_name (file), error->message);
          g_clear_error (&error);
        }
    }
}

void
data_new_cmd_callback (PikaAction *action,
                       GVariant   *value,
                       gpointer    user_data)
{
  PikaDataFactoryView *view = PIKA_DATA_FACTORY_VIEW (user_data);

  if (pika_data_factory_view_has_data_new_func (view))
    {
      PikaDataFactory *factory;
      PikaContext     *context;
      PikaData        *data;

      factory = pika_data_factory_view_get_data_factory (view);

      context =
        pika_container_view_get_context (PIKA_CONTAINER_EDITOR (view)->view);

      data = pika_data_factory_data_new (factory, context, _("Untitled"));

      if (data)
        {
          pika_context_set_by_type (context,
                                    pika_data_factory_view_get_children_type (view),
                                    PIKA_OBJECT (data));

          gtk_button_clicked (GTK_BUTTON (pika_data_factory_view_get_edit_button (view)));
        }
    }
}

void
data_duplicate_cmd_callback (PikaAction *action,
                             GVariant   *value,
                             gpointer    user_data)
{
  PikaDataFactoryView *view = PIKA_DATA_FACTORY_VIEW (user_data);
  PikaContext         *context;
  PikaData            *data;

  context = pika_container_view_get_context (PIKA_CONTAINER_EDITOR (view)->view);

  data = (PikaData *)
    pika_context_get_by_type (context,
                              pika_data_factory_view_get_children_type (view));

  if (data && pika_data_factory_view_have (view, PIKA_OBJECT (data)))
    {
      PikaData *new_data;

      new_data = pika_data_factory_data_duplicate (pika_data_factory_view_get_data_factory (view), data);

      if (new_data)
        {
          pika_context_set_by_type (context,
                                    pika_data_factory_view_get_children_type (view),
                                    PIKA_OBJECT (new_data));

          gtk_button_clicked (GTK_BUTTON (pika_data_factory_view_get_edit_button (view)));
        }
    }
}

void
data_copy_location_cmd_callback (PikaAction *action,
                                 GVariant   *value,
                                 gpointer    user_data)
{
  PikaDataFactoryView *view = PIKA_DATA_FACTORY_VIEW (user_data);
  PikaContext         *context;
  PikaData            *data;

  context = pika_container_view_get_context (PIKA_CONTAINER_EDITOR (view)->view);

  data = (PikaData *)
    pika_context_get_by_type (context,
                              pika_data_factory_view_get_children_type (view));

  if (data)
    {
      GFile *file = pika_data_get_file (data);

      if (file)
        {
          gchar *uri = g_file_get_uri (file);

          pika_clipboard_set_text (context->pika, uri);
          g_free (uri);
        }
    }
}

void
data_show_in_file_manager_cmd_callback (PikaAction *action,
                                        GVariant   *value,
                                        gpointer    user_data)
{
  PikaDataFactoryView *view = PIKA_DATA_FACTORY_VIEW (user_data);
  PikaContext         *context;
  PikaData            *data;

  context = pika_container_view_get_context (PIKA_CONTAINER_EDITOR (view)->view);

  data = (PikaData *)
    pika_context_get_by_type (context,
                              pika_data_factory_view_get_children_type (view));

  if (data)
    {
      GFile *file = pika_data_get_file (data);

      if (file)
        {
          GError *error = NULL;

          if (! pika_file_show_in_file_manager (file, &error))
            {
              pika_message (context->pika, G_OBJECT (view),
                            PIKA_MESSAGE_ERROR,
                            _("Can't show file in file manager: %s"),
                            error->message);
              g_clear_error (&error);
            }
        }
    }
}

void
data_delete_cmd_callback (PikaAction *action,
                          GVariant   *value,
                          gpointer    user_data)
{
  PikaDataFactoryView *view = PIKA_DATA_FACTORY_VIEW (user_data);
  PikaContext         *context;
  PikaData            *data;

  context =
    pika_container_view_get_context (PIKA_CONTAINER_EDITOR (view)->view);

  data = (PikaData *)
    pika_context_get_by_type (context,
                              pika_data_factory_view_get_children_type (view));

  if (data                          &&
      pika_data_is_deletable (data) &&
      pika_data_factory_view_have (view, PIKA_OBJECT (data)))
    {
      PikaDataFactory *factory;
      GtkWidget       *dialog;

      factory = pika_data_factory_view_get_data_factory (view);

      dialog = data_delete_dialog_new (factory, data, context,
                                       GTK_WIDGET (view));
      gtk_widget_show (dialog);
    }
}

void
data_refresh_cmd_callback (PikaAction *action,
                           GVariant   *value,
                           gpointer    user_data)
{
  PikaDataFactoryView *view = PIKA_DATA_FACTORY_VIEW (user_data);
  Pika                *pika;
  return_if_no_pika (pika, user_data);

  pika_set_busy (pika);
  pika_data_factory_data_refresh (pika_data_factory_view_get_data_factory (view),
                                  action_data_get_context (user_data));
  pika_unset_busy (pika);
}

void
data_edit_cmd_callback (PikaAction *action,
                        GVariant   *value,
                        gpointer    user_data)
{
  PikaDataFactoryView *view = PIKA_DATA_FACTORY_VIEW (user_data);
  PikaContext         *context;
  PikaData            *data;

  context = pika_container_view_get_context (PIKA_CONTAINER_EDITOR (view)->view);

  data = (PikaData *)
    pika_context_get_by_type (context,
                              pika_data_factory_view_get_children_type (view));

  if (data && pika_data_factory_view_have (view, PIKA_OBJECT (data)))
    {
      GdkMonitor *monitor = pika_widget_get_monitor (GTK_WIDGET (view));
      GtkWidget  *dockable;

      dockable =
        pika_window_strategy_show_dockable_dialog (PIKA_WINDOW_STRATEGY (pika_get_window_strategy (context->pika)),
                                                   context->pika,
                                                   pika_dialog_factory_get_singleton (),
                                                   monitor,
                                                   g_variant_get_string (value,
                                                                         NULL));

      pika_data_editor_set_data (PIKA_DATA_EDITOR (gtk_bin_get_child (GTK_BIN (dockable))),
                                 data);
    }
}
