/*
 * main.c
 *
 *  Created on: Nov 27, 2013
 *      Author: memphis
 */

#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <gio/gio.h>
#include "ast_dbus_client.h"
#include "ast_dbus_client_core.h"
#include "ast_dbus_client_device.h"
#include "ast_dbus_client_source.h"
#include "ast_xml.h"

struct _CustomData {
	guint timeout;
	GDBusConnection* connection;
	GDBusProxy* core_proxy;
	GDBusProxy* main_device_proxy;
};
typedef struct _CustomData CustomData;

static guint compteur = 0;

gboolean tick(gpointer user_data) {
	CustomData* data = (CustomData*) user_data;

	GSList* sources = ast_dbus_client_device_get_all_sources_by_type(data->main_device_proxy, "AstBaseSourceVideo");

	GSList* iter = NULL;
	for (iter = sources; iter ; iter = iter->next) {
		GDBusProxy* sourceProxy = ast_dbus_client_source_get_proxy(data->connection, (gchar*) iter->data);
		if (sourceProxy != NULL) {
			gchar* sourceName = ast_dbus_client_source_get_name(sourceProxy);
			gint sourceID = ast_dbus_client_source_get_id(sourceProxy);
			if (sourceName != NULL) {
				g_print("Update source : %d %s\n", sourceID, sourceName);
				gchar* newName = g_strdup_printf("Cam%d", compteur);
				if (!ast_dbus_client_source_reconfigure(sourceProxy, sourceID, newName, NULL)) {
					g_warning("Failed to update source name.");
				}

				if (compteur % 3 == 0) {
					gchar* sourceConfiguration = ast_dbus_client_source_get_configuration(sourceProxy);
					gchar* newConfiguration = NULL;
					if (sourceConfiguration != NULL) {
						gchar* filepath = ast_xml_xpath_get_inner_text(sourceConfiguration, "/Configuration/AST_VIDEO_FILE_TYPE_SOURCE/Filename");
						if (g_strcmp0(filepath, "/home/memphis/samples/Monster.mkv") == 0) {
							newConfiguration = ast_xml_xpath_set_inner_text(sourceConfiguration, "/Configuration/AST_VIDEO_FILE_TYPE_SOURCE/Filename", "/home/memphis/samples/axis.asf");
						} else if (g_strcmp0(filepath, "/home/memphis/samples/axis.asf") == 0) {
							newConfiguration = ast_xml_xpath_set_inner_text(sourceConfiguration, "/Configuration/AST_VIDEO_FILE_TYPE_SOURCE/Filename", "/home/memphis/samples/Monster.mkv");
						} else {
							newConfiguration = ast_xml_xpath_set_inner_text(sourceConfiguration, "/Configuration/AST_VIDEO_FILE_TYPE_SOURCE/Filename", "/home/memphis/samples/axis.asf");
						}
						if (!ast_dbus_client_source_reconfigure(sourceProxy, sourceID, NULL, newConfiguration)) {
							g_warning("Failed to reconfigure the source.");
						}
						g_free(newConfiguration);
						g_free(sourceConfiguration);
					} else {
						g_warning("Failed to get the source configuration.");
					}
				}

				g_free(newName);
				g_free(sourceName);
			}
			g_object_unref(sourceProxy);
		} else {
			g_print("Failed to get the proxy for %s\n", (gchar*)iter->data);
		}
	}

	g_slist_free_full(sources, g_free);

	compteur++;
	if (compteur > 100)
		compteur = 0;

	return G_SOURCE_CONTINUE;
}

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

	CustomData* data = g_new(CustomData, 1);
	data->timeout = 20000;
	data->core_proxy = NULL;
	data->main_device_proxy = NULL;

	data->connection = ast_dbus_client_get_connection();
	if (data->connection != NULL ) {
		data->core_proxy = ast_dbus_client_core_get_proxy(data->connection);
		if (data->core_proxy != NULL) {
			gint state = ast_dbus_client_core_get_state(data->core_proxy);
			if (state >= 6) {
				gchar* mainDevicePath = ast_dbus_client_core_get_main_object_dbus_path(data->core_proxy);
				if (mainDevicePath != NULL) {
					data->main_device_proxy = ast_dbus_client_device_get_proxy(data->connection, mainDevicePath);
					if (data->main_device_proxy != NULL) {
						g_print("Starting loop\n");
						GMainLoop* loop = g_main_loop_new(NULL, FALSE);
						guint tickID = g_timeout_add(data->timeout, tick, data);
						g_main_loop_run(loop);
						g_source_remove(tickID);
						g_main_loop_unref(loop);
						g_object_unref(data->main_device_proxy);
					}
					g_free(mainDevicePath);
				}

			} else {
				g_warning("Core state invalid.");
			}
			g_object_unref(data->core_proxy);
		}
		g_object_unref(data->connection);
	}



	return EXIT_SUCCESS;
}
