/* 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/pikaimage.h"
#include "core/pikatoolinfo.h"

#include "widgets/pikahelp-ids.h"
#include "widgets/pikatextbuffer.h"
#include "widgets/pikauimanager.h"
#include "widgets/pikawidgets-utils.h"

#include "display/pikadisplay.h"

#include "tools/pikatexttool.h"

#include "dialogs/dialogs.h"

#include "text-tool-commands.h"

#include "pika-intl.h"


/*  local function prototypes  */

static void   text_tool_load_dialog_response (GtkWidget    *dialog,
                                              gint          response_id,
                                              PikaTextTool *tool);


/*  public functions  */

void
text_tool_cut_cmd_callback (PikaAction *action,
                            GVariant   *value,
                            gpointer    data)
{
  PikaTextTool *text_tool = PIKA_TEXT_TOOL (data);

  pika_text_tool_cut_clipboard (text_tool);
}

void
text_tool_copy_cmd_callback (PikaAction *action,
                             GVariant   *value,
                             gpointer    data)
{
  PikaTextTool *text_tool = PIKA_TEXT_TOOL (data);

  pika_text_tool_copy_clipboard (text_tool);
}

void
text_tool_paste_cmd_callback (PikaAction *action,
                              GVariant   *value,
                              gpointer    data)
{
  PikaTextTool *text_tool = PIKA_TEXT_TOOL (data);

  pika_text_tool_paste_clipboard (text_tool);
}

void
text_tool_delete_cmd_callback (PikaAction *action,
                               GVariant   *value,
                               gpointer    data)
{
  PikaTextTool *text_tool = PIKA_TEXT_TOOL (data);

  pika_text_tool_delete_selection (text_tool);
}

void
text_tool_load_cmd_callback (PikaAction *action,
                             GVariant   *value,
                             gpointer    data)
{
  PikaTextTool *text_tool = PIKA_TEXT_TOOL (data);
  GtkWidget    *dialog;

  dialog = dialogs_get_dialog (G_OBJECT (text_tool), "pika-text-file-dialog");

  if (! dialog)
    {
      GtkWidget *parent = NULL;

      if (PIKA_TOOL (text_tool)->display)
        {
          PikaDisplayShell *shell;

          shell = pika_display_get_shell (PIKA_TOOL (text_tool)->display);

          parent = gtk_widget_get_toplevel (GTK_WIDGET (shell));
        }

      dialog = gtk_file_chooser_dialog_new (_("Open Text File (UTF-8)"),
                                            parent ? GTK_WINDOW (parent) : NULL,
                                            GTK_FILE_CHOOSER_ACTION_OPEN,

                                            _("_Cancel"), GTK_RESPONSE_CANCEL,
                                            _("_Open"),   GTK_RESPONSE_OK,

                                            NULL);

      gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
      pika_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                               GTK_RESPONSE_OK,
                                               GTK_RESPONSE_CANCEL,
                                               -1);

      gtk_window_set_role (GTK_WINDOW (dialog), "pika-text-load-file");
      gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);

      g_signal_connect (dialog, "response",
                        G_CALLBACK (text_tool_load_dialog_response),
                        text_tool);
      g_signal_connect (dialog, "delete-event",
                        G_CALLBACK (gtk_true),
                        NULL);

      dialogs_attach_dialog (G_OBJECT (text_tool),
                             "pika-text-file-dialog", dialog);
    }

  gtk_window_present (GTK_WINDOW (dialog));
}

void
text_tool_clear_cmd_callback (PikaAction *action,
                              GVariant   *value,
                              gpointer    data)
{
  PikaTextTool  *text_tool = PIKA_TEXT_TOOL (data);
  GtkTextBuffer *buffer    = GTK_TEXT_BUFFER (text_tool->buffer);
  GtkTextIter    start, end;

  gtk_text_buffer_get_bounds (buffer, &start, &end);
  gtk_text_buffer_select_range (buffer, &start, &end);
  pika_text_tool_delete_selection (text_tool);
}

void
text_tool_text_to_path_cmd_callback (PikaAction *action,
                                     GVariant   *value,
                                     gpointer    data)
{
  PikaTextTool *text_tool = PIKA_TEXT_TOOL (data);

  pika_text_tool_create_vectors (text_tool);
}

void
text_tool_text_along_path_cmd_callback (PikaAction *action,
                                        GVariant   *value,
                                        gpointer    data)
{
  PikaTextTool *text_tool = PIKA_TEXT_TOOL (data);
  GError       *error     = NULL;

  if (! pika_text_tool_create_vectors_warped (text_tool, &error))
    {
      pika_message (text_tool->image->pika, G_OBJECT (text_tool),
                    PIKA_MESSAGE_ERROR,
                    _("Text along path failed: %s"),
                    error->message);
      g_clear_error (&error);
    }
}

void
text_tool_direction_cmd_callback (PikaAction *action,
                                  GVariant   *value,
                                  gpointer    data)
{
  PikaTextTool      *text_tool = PIKA_TEXT_TOOL (data);
  PikaTextDirection  direction;

  direction = (PikaTextDirection) g_variant_get_int32 (value);

  g_object_set (text_tool->proxy,
                "base-direction", direction,
                NULL);
}


/*  private functions  */

static void
text_tool_load_dialog_response (GtkWidget    *dialog,
                                gint          response_id,
                                PikaTextTool *tool)
{
  if (response_id == GTK_RESPONSE_OK)
    {
      GFile  *file  = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
      GError *error = NULL;

      if (! pika_text_buffer_load (tool->buffer, file, &error))
        {
          pika_message (PIKA_TOOL (tool)->tool_info->pika, G_OBJECT (dialog),
                        PIKA_MESSAGE_ERROR,
                        _("Could not open '%s' for reading: %s"),
                        pika_file_get_utf8_name (file),
                        error->message);
          g_clear_error (&error);
          g_object_unref (file);
          return;
        }

      g_object_unref (file);
    }

  gtk_widget_hide (dialog);
}
