#include <glib.h>

#include <folks/folks.h>
#include <folks/folks-telepathy.h>

#include <telepathy-glib/telepathy-glib.h>

static GMainLoop *loop = NULL;
static gint tot = 0;

static void
aggregator_individuals_changed_cb (FolksIndividualAggregator *aggregator,
    GeeMultiMap *changes,
    gpointer unused)
{
  GeeCollection *added;
  guint size;

  added = gee_multi_map_get_values (changes);

  size = gee_collection_get_size (GEE_COLLECTION (added));

  //g_print ("%d invidivduals added on %p\n", size, aggregator);

  tot += size;
}

static void
prepare_cb (GObject *source,
    GAsyncResult *result,
    gpointer user_data)
{
  GError *error = NULL;

  folks_individual_aggregator_prepare_finish (
      FOLKS_INDIVIDUAL_AGGREGATOR (source), result, &error);
  g_assert_no_error (error);
}

static gboolean
create_aggregator (void)
{
  static FolksIndividualAggregator *aggregator = NULL;

  if (aggregator != NULL)
    {
      g_print ("TOTAL %d individuals\n", tot);

      g_object_unref (aggregator);
      tot = 0;
    }

  aggregator = folks_individual_aggregator_new ();

  g_print ("CREATE aggregator %p\n", aggregator);

  g_signal_connect (aggregator, "individuals-changed-detailed",
      G_CALLBACK (aggregator_individuals_changed_cb), NULL);

  folks_individual_aggregator_prepare (aggregator, prepare_cb, NULL);

  return TRUE;
}

int
main (int argc,
    gchar *argv)
{
  guint i;

  g_type_init ();

  create_aggregator ();

  g_timeout_add_seconds (10, (GSourceFunc) create_aggregator, NULL);

  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);
  return 0;
}
