/* 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 "libpikawidgets/pikawidgets.h"

#include "dialogs-types.h"

#include "core/pika.h"

#include "widgets/pikadeviceeditor.h"
#include "widgets/pikadevicemanager.h"
#include "widgets/pikadevices.h"
#include "widgets/pikahelp-ids.h"
#include "widgets/pikamessagebox.h"
#include "widgets/pikamessagedialog.h"

#include "input-devices-dialog.h"

#include "pika-intl.h"


/*  local function prototypes  */

static void   input_devices_dialog_response (GtkWidget *dialog,
                                             guint      response_id,
                                             Pika      *pika);


/*  public functions  */

GtkWidget *
input_devices_dialog_new (Pika *pika)
{
  GtkWidget *dialog;
  GtkWidget *content_area;
  GtkWidget *editor;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  dialog = pika_dialog_new (_("Configure Input Devices"),
                            "pika-input-devices-dialog",
                            NULL, 0,
                            pika_standard_help_func,
                            PIKA_HELP_INPUT_DEVICES,

                            _("_Reset"),  GTK_RESPONSE_REJECT,
                            _("_Cancel"), GTK_RESPONSE_CANCEL,
                            _("_OK"),     GTK_RESPONSE_OK,

                            NULL);

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

  g_signal_connect (dialog, "response",
                    G_CALLBACK (input_devices_dialog_response),
                    pika);

  content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));

  editor = pika_device_editor_new (pika);
  gtk_container_set_border_width (GTK_CONTAINER (editor), 12);
  gtk_box_pack_start (GTK_BOX (content_area), editor, TRUE, TRUE, 0);
  gtk_widget_show (editor);

  return dialog;
}


/*  private functions  */

static void
input_devices_dialog_response (GtkWidget *dialog,
                               guint      response_id,
                               Pika      *pika)
{
  switch (response_id)
    {
    case GTK_RESPONSE_OK:
      pika_devices_save (pika, TRUE);
      break;

    case GTK_RESPONSE_DELETE_EVENT:
    case GTK_RESPONSE_CANCEL:
      pika_devices_restore (pika);
      break;

    case GTK_RESPONSE_REJECT:
      {
        GtkWidget *confirm;

        confirm = pika_message_dialog_new (_("Reset Input Device Configuration"),
                                           PIKA_ICON_DIALOG_QUESTION,
                                           dialog,
                                           GTK_DIALOG_MODAL |
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
                                           pika_standard_help_func, NULL,

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

                                           NULL);

        pika_dialog_set_alternative_button_order (GTK_DIALOG (confirm),
                                                  GTK_RESPONSE_OK,
                                                  GTK_RESPONSE_CANCEL,
                                                  -1);

        pika_message_box_set_primary_text (PIKA_MESSAGE_DIALOG (confirm)->box,
                                           _("Do you really want to reset all "
                                             "input devices to default configuration?"));

        if (pika_dialog_run (PIKA_DIALOG (confirm)) == GTK_RESPONSE_OK)
          {
            pika_device_manager_reset (pika_devices_get_manager (pika));
            pika_devices_save (pika, TRUE);
            pika_devices_restore (pika);
          }
        gtk_widget_destroy (confirm);
      }
      return;

    default:
      break;
    }

  gtk_widget_destroy (dialog);
}
