#include <grilo.h>
#include <string.h>

static void browse_cb (GrlMediaSource *source,
                       guint           operation_id,
                       GrlMedia       *media,
                       guint           remaining,
                       gpointer        user_data G_GNUC_UNUSED,
                       const GError   *error)
{
    if (error) {
        g_printerr ("%s(%s): opid=%d, error=%s\n",
                    G_STRFUNC, grl_media_plugin_get_id (GRL_MEDIA_PLUGIN (source)),
                    operation_id, error->message);
        return;
    }

    g_print ("%s(%s): opid=%d, remaining=%d, media=\"%s\" %s(id=%s, mime-type=%s, url=%s)\n",
             G_STRFUNC, grl_media_plugin_get_id (GRL_MEDIA_PLUGIN (source)),
             operation_id, remaining,
             media ? grl_media_get_title (media) : "",
             media ? G_OBJECT_TYPE_NAME (media) : "",
             media ? grl_media_get_id (media) : "",
             media ? grl_media_get_mime(media) : "",
             media ? grl_media_get_url(media) : "");

    /*
    if (GRL_IS_MEDIA_BOX (media) && g_str_has_prefix (grl_media_get_id (media), "/media")) {
        GrlCaps *caps = grl_metadata_source_get_caps (GRL_METADATA_SOURCE (source), GRL_OP_BROWSE);
        GrlOperationOptions *options = grl_operation_options_new (caps);
        grl_operation_options_set_flags (options, GRL_RESOLVE_IDLE_RELAY);
        grl_media_source_browse (source, media, NULL, options, browse_cb, NULL);
        g_object_unref (options);
    }
    */
}

static void content_changed_cb (GrlMediaSource          *source,
                                GPtrArray               *changed_medias,
                                GrlMediaSourceChangeType change_type,
                                gboolean                 location_unknown,
                                gpointer                 user_data G_GNUC_UNUSED)
{
    g_print ("%s: #changed_medias=%d, change_type=%d, location_unknown=%c\n",
             grl_media_plugin_get_id (GRL_MEDIA_PLUGIN (source)),
             changed_medias ? changed_medias->len : 0,
             change_type, location_unknown ? 't' : 'f');

    GrlCaps *caps = grl_metadata_source_get_caps (GRL_METADATA_SOURCE (source), GRL_OP_BROWSE);
    GrlOperationOptions *options = grl_operation_options_new (caps);
    grl_operation_options_set_flags (options, GRL_RESOLVE_IDLE_RELAY);
    grl_media_source_browse (GRL_MEDIA_SOURCE (source), NULL, NULL, options, browse_cb, NULL);
    g_object_unref (options);
}

static gboolean try_source (GrlPluginRegistry *registry, const gchar *id)
{
    GError *error = NULL;

    if (!grl_plugin_registry_load_by_id (registry, id, &error)) {
        g_printerr ("%s: %s\n", id, error->message);
        g_clear_error (&error);
        return FALSE;
    }

    GrlMediaPlugin *source = grl_plugin_registry_lookup_source(registry, id);

    if (!GRL_IS_MEDIA_SOURCE (source)) {
        g_printerr ("%s: Bad plugin\n", id);
        return FALSE;
    }

    g_signal_connect (source, "content-changed", G_CALLBACK (content_changed_cb), NULL);
    content_changed_cb (GRL_MEDIA_SOURCE (source), NULL, GRL_CONTENT_CHANGED, TRUE, NULL);

    g_print ("Using %s (id=\"%s\" rank=%d)\n",
             grl_media_plugin_get_name (source),
             grl_media_plugin_get_id (source),
             grl_media_plugin_get_rank (source));

    return TRUE;
}

int main(int argc, char *argv[])
{
    grl_init(&argc, &argv);

    GrlPluginRegistry *registry = grl_plugin_registry_get_default ();

    if (!(try_source (registry, "grl-filesystem")
            && try_source (registry, "grl-optical-media")))
        return 1;

    GMainLoop *loop = g_main_loop_new (NULL, TRUE);
    g_main_loop_run (loop);
    g_main_loop_unref (loop);

    return 0;
}

