/* 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
 *
 * pikasinglewindowstrategy.c
 * Copyright (C) 2011 Martin Nordholts <martinn@src.gnome.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 "display-types.h"

#include "core/pika.h"

#include "menus/menus.h"

#include "widgets/pikadialogfactory.h"
#include "widgets/pikadock.h"
#include "widgets/pikadockbook.h"
#include "widgets/pikadockcolumns.h"
#include "widgets/pikawindowstrategy.h"

#include "pikaimagewindow.h"
#include "pikasinglewindowstrategy.h"


static void        pika_single_window_strategy_window_strategy_iface_init (PikaWindowStrategyInterface *iface);
static GtkWidget * pika_single_window_strategy_show_dockable_dialog       (PikaWindowStrategy          *strategy,
                                                                           Pika                        *pika,
                                                                           PikaDialogFactory           *factory,
                                                                           GdkMonitor                  *monitor,
                                                                           const gchar                 *identifiers);


G_DEFINE_TYPE_WITH_CODE (PikaSingleWindowStrategy, pika_single_window_strategy, PIKA_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_WINDOW_STRATEGY,
                                                pika_single_window_strategy_window_strategy_iface_init))

#define parent_class pika_single_window_strategy_parent_class


static void
pika_single_window_strategy_class_init (PikaSingleWindowStrategyClass *klass)
{
}

static void
pika_single_window_strategy_init (PikaSingleWindowStrategy *strategy)
{
}

static void
pika_single_window_strategy_window_strategy_iface_init (PikaWindowStrategyInterface *iface)
{
  iface->show_dockable_dialog = pika_single_window_strategy_show_dockable_dialog;
}

static GtkWidget *
pika_single_window_strategy_show_dockable_dialog (PikaWindowStrategy *strategy,
                                                  Pika               *pika,
                                                  PikaDialogFactory  *factory,
                                                  GdkMonitor         *monitor,
                                                  const gchar        *identifiers)
{
  GList           *windows = pika_get_image_windows (pika);
  GtkWidget       *widget  = NULL;
  PikaImageWindow *window;

  g_return_val_if_fail (windows != NULL, NULL);

  /* In single-window mode, there should only be one window... */
  window = PIKA_IMAGE_WINDOW (windows->data);

  if (strcmp ("pika-toolbox", identifiers) == 0)
    {
      /* Only allow one toolbox... */
      if (! pika_image_window_has_toolbox (window))
        {
          PikaDockColumns *columns;
          PikaUIManager   *ui_manager = menus_get_image_manager_singleton (pika);

          widget = pika_dialog_factory_dialog_new (factory, monitor,
                                                   ui_manager,
                                                   GTK_WIDGET (window),
                                                   "pika-toolbox",
                                                   -1 /*view_size*/,
                                                   FALSE /*present*/);
          gtk_widget_show (widget);

          columns = pika_image_window_get_left_docks (window);
          pika_dock_columns_add_dock (columns,
                                      PIKA_DOCK (widget),
                                      -1 /*index*/);
        }
      else
        {
          widget = pika_dialog_factory_find_widget (factory, "pika-toolbox");
        }
    }
  else if (pika_dialog_factory_find_widget (factory, identifiers))
    {
      /* if the dialog is already open, simply raise it */
      return pika_dialog_factory_dialog_raise (factory, monitor,
                                               GTK_WIDGET (window),
                                               identifiers, -1);
   }
  else
    {
      GtkWidget *dockbook;

      dockbook = pika_image_window_get_default_dockbook (window);

      if (! dockbook)
        {
          PikaDockColumns *dock_columns;

          /* No dock, need to add one */
          dock_columns = pika_image_window_get_right_docks (window);
          pika_dock_columns_prepare_dockbook (dock_columns,
                                              -1 /*index*/,
                                              &dockbook);
        }

      widget = pika_dockbook_add_from_dialog_factory (PIKA_DOCKBOOK (dockbook),
                                                      identifiers);
    }


  g_list_free (windows);

  return widget;
}

PikaObject *
pika_single_window_strategy_get_singleton (void)
{
  static PikaObject *singleton = NULL;

  if (! singleton)
    singleton = g_object_new (PIKA_TYPE_SINGLE_WINDOW_STRATEGY, NULL);

  return singleton;
}
