
/*
 * gcc -g3 -O0 -o favourite `pkg-config --cflags --libs grilo-0.2` favourite.c
 */

#include <grilo.h>

#define GRL_LOG_DOMAIN_DEFAULT  example_log_domain
GRL_LOG_DOMAIN_STATIC(example_log_domain);


static void
resolve_cb (GrlSource *source,
            guint operation_id,
            GrlMedia *media,
            gpointer user_data,
            const GError *error)
{
  g_print("Resolve callback ...\n");

  if (error) {
    g_print ("Error: %s\n", error->message);
  }

  g_print ("Media: "
           "playcount (%i) "
           "favourite (%i) "
           "last position (%i)"
           "last played (%s) "
           "\n",
           grl_media_get_play_count (media),
           grl_media_get_favourite (media),
           grl_media_get_last_position (media),
           grl_media_get_last_played (media)
    );
}

static void
resolve (GrlSource *source)
{

  GList *keys = grl_metadata_key_list_new (GRL_METADATA_KEY_FAVOURITE,
                                           GRL_METADATA_KEY_PLAY_COUNT,
                                           GRL_METADATA_KEY_LAST_POSITION,
                                           GRL_METADATA_KEY_LAST_PLAYED,
                                           GRL_METADATA_KEY_INVALID);

  GrlMedia *media = grl_media_audio_new ();
  grl_media_set_id (media, "id1");
  grl_media_set_source (media, "grl-jamendo");

  if (grl_source_may_resolve(source, media, GRL_METADATA_KEY_FAVOURITE, NULL)) {
    GrlCaps *caps = grl_source_get_caps (source, GRL_OP_RESOLVE);
    GrlOperationOptions *options = grl_operation_options_new (caps);
    grl_operation_options_set_flags (options, GRL_RESOLVE_NORMAL);

    grl_source_resolve (source, media, keys, options, resolve_cb, NULL);

    g_object_unref (caps);
    g_object_unref (options);

  } else {
    g_print("Can't resolve metadata\n");
  }

    g_list_free (keys);
    g_object_unref (media);
}

static void
store_metadata_cb (GrlSource *source,
                   GrlMedia *media,
                   GList *failed_keys,
                   gpointer user_data,
                   const GError *error)
{
  g_print ("Store metadata callback...\n");

  if (failed_keys) {
    g_print ("Failed keys:");
    int i;
    for (i = 0; i < g_list_length(failed_keys); i++) {
      g_print ("%s  ", grl_metadata_key_get_name(GRLPOINTER_TO_KEYID(g_list_nth_data(failed_keys, i))));
    }

    g_print ("\n");
  }

  if (error) {
    g_print ("Error: %s\n", error->message);
    return;
  }

  resolve (source);
}

static void
store_metadata (GrlSource *source)
{
  g_print ("Storing metadata...\n");

  GrlMedia *media = grl_media_audio_new ();
  grl_media_set_id (media, "id1");
  grl_media_set_source (media, "grl-jamendo");
  grl_media_set_favourite (media, TRUE);
  grl_media_set_play_count (media, 23);
  grl_media_set_rating (media, 1.7, 5.0);
  grl_media_set_last_position (media, 220);
  grl_media_set_last_played (media, "16 january 2012");

  GList *keys = grl_metadata_key_list_new (GRL_METADATA_KEY_PLAY_COUNT,
                                           GRL_METADATA_KEY_FAVOURITE,
                                           GRL_METADATA_KEY_RATING,
                                           GRL_METADATA_KEY_LAST_POSITION,
                                           GRL_METADATA_KEY_LAST_PLAYED,
                                           GRL_METADATA_KEY_INVALID);
  grl_source_store_metadata (source, media, keys, GRL_WRITE_NORMAL, store_metadata_cb, NULL);

  g_list_free (keys);
  g_object_unref (media);
}

static void
source_added_cb (GrlRegistry *registry, GrlSource *source, gpointer user_data)
{
  g_debug ("Detected new source available: '%s'",
           grl_source_get_name (source));

  const gchar *source_id = grl_source_get_id (source);

  if (strcmp(source_id, "grl-metadata-store")) {
    return;
  }

  store_metadata (source);
}

static void
source_removed_cb (GrlRegistry *registry, GrlSource *source, gpointer user_data)
{
  g_debug ("Source '%s' is gone",
           grl_source_get_name (source));

  /* Usually you would inform the user that this service is no longer
     available (for example a UPnP server was shutdown) and remove it
     from the user interface. */
}

static void
load_plugins (void)
{
  GrlRegistry *registry;
  GError *error = NULL;

  registry = grl_registry_get_default ();

  /* These callback will be invoked when media providers
     are loaded/unloaded */
  g_signal_connect (registry, "source-added",
                    G_CALLBACK (source_added_cb), NULL);
  g_signal_connect (registry, "source-removed",
                    G_CALLBACK (source_removed_cb), NULL);

  /* Command the registry to load all available plugins.
     The registry will look for plugins in the default
     plugin path and directories specified using the
     GRL_PLUGIN_PATH environment variable */
  if (!grl_registry_load_all_plugins (registry, &error)) {
    g_error ("Failed to load plugins: %s", error->message);
  }
}

gint
main (int argc, gchar *argv[])
{
  GMainLoop *loop;

  grl_init (&argc, &argv);
  GRL_LOG_DOMAIN_INIT (example_log_domain, "example");
  load_plugins ();            /* Load Grilo plugins */

  /* Run the main loop */
  loop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (loop);

  return 0;
}
