/*
 * SPDX-FileCopyrightText: 2012-2021 Bruce Cowan <bruce@bcowan.me.uk>
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "config.h"

#include "rugby-app-window.h"
#include "rugby-pref-window.h"

static GtkWidget *window = NULL;

static void
on_activate (              GApplication *app,
             G_GNUC_UNUSED gpointer      user_data)
{
    if (!window)
        window = GTK_WIDGET (rugby_app_window_new (GTK_APPLICATION (app)));

    gtk_window_present (GTK_WINDOW (window));
}

static void
about_activated (G_GNUC_UNUSED GSimpleAction *simple,
                 G_GNUC_UNUSED GVariant      *parameter,
                               gpointer       user_data)
{
    GtkApplication *app = GTK_APPLICATION (user_data);
    GtkWindow *window = gtk_application_get_active_window (app);

    const char *authors[] = { "Bruce Cowan", NULL };

    gtk_show_about_dialog (window,
                           "logo-icon-name", "face-wink",
                           "program-name", "Rugby",
                           "copyright", "Copyright 2012–2021 Bruce Cowan",
                           "license-type", GTK_LICENSE_GPL_3_0,
                           "authors", authors,
                           "comments", "Rugby scores possiblities program",
                           "version", VERSION,
                           NULL);
}

static void
preferences_activated (G_GNUC_UNUSED GSimpleAction *simple,
                       G_GNUC_UNUSED GVariant      *parameter,
                                     gpointer       user_data)
{
    GtkApplication *app = GTK_APPLICATION (user_data);
    GtkWindow *window = gtk_application_get_active_window (app);

    RugbyPrefWindow *pref_window = rugby_pref_window_new (RUGBY_APP_WINDOW (window));
    gtk_window_present (GTK_WINDOW (pref_window));
}

static void
on_startup (GApplication          *app,
            G_GNUC_UNUSED gpointer user_data)
{
    const GActionEntry app_entries[] =
    {
        { .name = "about", .activate = about_activated },
        { .name = "prefs", .activate = preferences_activated },
    };

    g_action_map_add_action_entries (G_ACTION_MAP (app),
                                     app_entries,
                                     G_N_ELEMENTS (app_entries),
                                     app);
}

int
main (int    argc,
      char **argv)
{
    GtkApplication *app = gtk_application_new ("uk.me.bcowan.Rugby",
                                               G_APPLICATION_FLAGS_NONE);

    g_signal_connect (app, "startup",
                      G_CALLBACK (on_startup), NULL);
    g_signal_connect (app, "activate",
                      G_CALLBACK (on_activate), NULL);

    return g_application_run (G_APPLICATION (app), argc, argv);
}