/* gcc `pkg-config --cflags --libs gtk+-3.0 libsoup-2.4 rest-0.7`  -I libgfbgraph/ gtk_simple_auth.c -lgfbgraph-0.2 */


#include <stdio.h>
#include <gfbgraph/gfbgraph.h>
#include <gfbgraph/gfbgraph-simple-authorizer.h>
#include <gio/gio.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>


static void
facebook_upload (GFile *upload_file, gpointer data)
{
  GFBGraphSimpleAuthorizer *authorizer;
  GFBGraphPhoto *upload_photo = NULL;
  gchar *status_text;
  gint status;

  authorizer = gfbgraph_simple_authorizer_new ("PUT ACCESS TOKEN HERE");

  gfbgraph_authorizer_refresh_authorization (GFBGRAPH_AUTHORIZER (authorizer), NULL, NULL);
  upload_photo = gfbgraph_photo_new_from_file_source (g_file_get_uri (upload_file), "image/jpg");
  status = gfbgraph_photo_upload_photo (upload_photo, GFBGRAPH_AUTHORIZER (authorizer));

  status_text = g_strdup_printf ("Status: %d", status);
  gtk_window_set_title (GTK_WINDOW (data), status_text);

  g_object_unref (upload_file);
  g_free (status_text);
}


static void
get_file_from_file_chooser (GtkWidget *widget,
                            gpointer   data)
{
  GtkWidget *dialog;
  GtkFileFilter *filter;
  GFile *upload_file = NULL;
  gint res;

  dialog = gtk_file_chooser_dialog_new ("Open File",
                                        GTK_WINDOW (data),
                                        GTK_FILE_CHOOSER_ACTION_OPEN,
                                        _("_Cancel"),
                                        GTK_RESPONSE_CANCEL,
                                        _("_Open"),
                                        GTK_RESPONSE_ACCEPT,
                                        NULL);

  filter = gtk_file_filter_new ();
  gtk_file_filter_set_name (filter, _("JPG images"));
  gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), filter);
  gtk_file_filter_add_pattern (filter, "*.jpg");

  res = gtk_dialog_run (GTK_DIALOG (dialog));
  if (res == GTK_RESPONSE_ACCEPT)
    {
      char *filename;
      GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
      filename = gtk_file_chooser_get_filename (chooser);
      upload_file = g_file_new_for_path (filename);
      g_free (filename);
    }
  else
    {
      gtk_widget_destroy (dialog);
      return;
    }

  gtk_widget_destroy (dialog);

  g_assert (upload_file != NULL);

  facebook_upload (upload_file, data);
}


static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *button_box;
  GtkWidget *close_button;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 300, 300);

  button_box = gtk_button_box_new (GTK_ORIENTATION_VERTICAL);
  gtk_button_box_set_layout (GTK_BUTTON_BOX (button_box), GTK_BUTTONBOX_CENTER);
  gtk_container_add (GTK_CONTAINER (window), button_box);

  button = gtk_button_new_with_label ("Open File Chooser");
  g_signal_connect (button, "clicked", G_CALLBACK (get_file_from_file_chooser), window);
  gtk_container_add (GTK_CONTAINER (button_box), button);

  close_button = gtk_button_new_with_label ("Close app");
  g_signal_connect_swapped (close_button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
  gtk_container_add (GTK_CONTAINER (button_box), close_button);

  gtk_widget_show_all (window);
}


int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}
