#include <glib-unix.h>
#include <signal.h>
#include <string.h>
#include "tiles.h"

#define SHELL_GONE_S 60 // longer than c8's backoff before it runs the shell again

GDBusConnection *c8_tiles_bus(void) {
	static GDBusConnection *bus;
	if (bus == NULL) {
		bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
	}
	return bus;
}

void c8_tiles_call(const char *method, GVariant *params) {
	GDBusConnection *bus = c8_tiles_bus();
	if (bus == NULL) {
		g_variant_unref(g_variant_ref_sink(params));
		return;
	}
	g_dbus_connection_call(bus, C8_TILES_NAME, C8_TILES_PATH, C8_TILES_IFACE,
		method, params, NULL, G_DBUS_CALL_FLAGS_NONE, 5000, NULL, NULL, NULL);
}

static struct {
	const struct c8_tile_provider *p;
	void *data;
	guint gone_timer;
	bool seen; // a shell owned the name at some point
} watch;

static gboolean quit(gpointer unused) {
	watch.gone_timer = 0;
	watch.p->quit(watch.data);
	return G_SOURCE_REMOVE;
}

static gboolean on_unix_signal(gpointer unused) {
	watch.p->quit(watch.data);
	return G_SOURCE_CONTINUE;
}

static void on_signal(GDBusConnection *bus, const char *sender,
		const char *path, const char *iface, const char *signal,
		GVariant *params, gpointer unused) {
	if (strcmp(signal, "Restarting") == 0) {
		const char *id;
		g_variant_get(params, "(&s)", &id);
		if (strcmp(id, watch.p->tile_id) == 0) {
			watch.p->quit(watch.data);
		}
	} else if (strcmp(signal, "TileChanged") == 0) {
		const char *id, *size;
		gboolean pinned;
		g_variant_get(params, "(&sb&s)", &id, &pinned, &size);
		/* Unpinned, there's nothing left to keep fresh */
		if (watch.p->quit_unpinned && !pinned &&
				strcmp(id, watch.p->tile_id) == 0) {
			watch.p->quit(watch.data);
		}
	} else if (strcmp(signal, "Activated") == 0) {
		const char *id;
		g_variant_get(params, "(&s)", &id);
		if (watch.p->activated && strcmp(id, watch.p->tile_id) == 0) {
			watch.p->activated(watch.data);
		}
	} else if (strcmp(signal, "PropertiesChanged") == 0 && watch.p->visible) {
		GVariant *changed = g_variant_get_child_value(params, 1);
		gboolean visible;
		if (g_variant_lookup(changed, "StartVisible", "b", &visible)) {
			watch.p->visible(visible, watch.data);
		}
		g_variant_unref(changed);
	}
}

static void got_visible(GObject *source, GAsyncResult *result,
		gpointer unused) {
	GVariant *r = g_dbus_connection_call_finish(G_DBUS_CONNECTION(source),
		result, NULL);
	if (r) {
		GVariant *v;
		g_variant_get(r, "(v)", &v);
		watch.p->visible(g_variant_get_boolean(v), watch.data);
		g_variant_unref(v);
		g_variant_unref(r);
	}
}

static void shell_appeared(GDBusConnection *bus, const char *name,
		const char *owner, gpointer unused) {
	watch.seen = true;
	if (watch.gone_timer) {
		g_source_remove(watch.gone_timer);
		watch.gone_timer = 0;
	}
	if (watch.p->shell) {
		watch.p->shell(watch.data);
	}
	if (watch.p->visible) {
		g_dbus_connection_call(bus, C8_TILES_NAME, C8_TILES_PATH,
			"org.freedesktop.DBus.Properties", "Get",
			g_variant_new("(ss)", C8_TILES_IFACE, "StartVisible"),
			G_VARIANT_TYPE("(v)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
			got_visible, NULL);
	}
}

static void shell_vanished(GDBusConnection *bus, const char *name,
		gpointer unused) {
	if (watch.gone_timer == 0 && (watch.seen || !watch.p->wait_for_shell)) {
		watch.gone_timer = g_timeout_add_seconds(SHELL_GONE_S, quit, NULL);
	}
	if (watch.seen && watch.p->gone) {
		watch.p->gone(watch.data);
	}
}

void c8_tile_provider_watch(const struct c8_tile_provider *provider,
		void *data) {
	watch.p = provider;
	watch.data = data;
	g_unix_signal_add(SIGTERM, on_unix_signal, NULL);
	g_unix_signal_add(SIGINT, on_unix_signal, NULL);
	GDBusConnection *bus = c8_tiles_bus();
	if (bus == NULL) {
		return;
	}
	g_dbus_connection_signal_subscribe(bus, C8_TILES_NAME, C8_TILES_IFACE,
		NULL, C8_TILES_PATH, NULL, G_DBUS_SIGNAL_FLAGS_NONE, on_signal, NULL,
		NULL);
	g_dbus_connection_signal_subscribe(bus, C8_TILES_NAME,
		"org.freedesktop.DBus.Properties", "PropertiesChanged",
		C8_TILES_PATH, C8_TILES_IFACE, G_DBUS_SIGNAL_FLAGS_NONE, on_signal,
		NULL, NULL);
	g_bus_watch_name_on_connection(bus, C8_TILES_NAME,
		G_BUS_NAME_WATCHER_FLAGS_NONE, shell_appeared, shell_vanished, NULL,
		NULL);
}
