#include <gtk/gtk.h>

static void
on_action_button(GSimpleAction *action, GVariant *parameter, void *user_data)
{
  printf("You are privileged to be seeing this message.\n");
}

static void
on_action_body(GSimpleAction *action, GVariant *parameter, void *user_data)
{
  GApplication *application = user_data;
  g_application_quit (application);
}

static void
on_application_startup(GApplication *application, void *user_data)
{
  GSimpleAction *action = g_simple_action_new ("button", NULL);
  g_signal_connect (G_OBJECT (action), "activate",
                    G_CALLBACK (&on_action_button), NULL);
  g_action_map_add_action(G_ACTION_MAP (application), G_ACTION (action));

  action = g_simple_action_new ("body", NULL);
  g_signal_connect (G_OBJECT (action), "activate",
                    G_CALLBACK (&on_action_body), application);
  g_action_map_add_action(G_ACTION_MAP (application), G_ACTION (action));
}

static void
on_button_clicked(GtkButton *button, void *user_data)
{
  GNotification *notification = g_notification_new ("Where is the button?");
  g_notification_set_body (notification, "Try to find the mysterious button");
  g_notification_set_default_action (notification, "app.body");
  g_notification_add_button (notification, "You are fast and smart!", "app.button");

  GApplication *application = G_APPLICATION (user_data);
  g_application_send_notification(application, "test-noti", notification);

  g_object_unref (notification);
}

static void
on_application_activate(GApplication *application, void *user_data)
{
  GtkWidget *button = gtk_button_new_with_label ("_Click");
  gtk_button_set_use_underline (GTK_BUTTON (button), TRUE);
  g_signal_connect (G_OBJECT (button), "clicked",
                    G_CALLBACK (&on_button_clicked), application);

  GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "test");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 100);
  gtk_container_add (GTK_CONTAINER (window), button);
  gtk_widget_show_all (window);
  gtk_application_add_window (GTK_APPLICATION (application), GTK_WINDOW (window));
}

int
main(int argc, char **argv)
{
  GtkApplication *application = gtk_application_new("org.djb.hx1ed", G_APPLICATION_FLAGS_NONE);

  g_signal_connect (G_OBJECT (application), "startup",
                    G_CALLBACK (&on_application_startup), NULL);
  g_signal_connect (G_OBJECT (application), "activate",
                    G_CALLBACK (&on_application_activate), NULL);

  int status = g_application_run (G_APPLICATION (application), argc, argv);
  g_object_unref(application);
  return status;
}
