/*
 * Power mode on the Power page: power-profiles-daemon's (or tuned-ppd's)
 * profiles as a choice, kept in step with the daemon, which the Power
 * tile in the Settings flyout also changes.
 */
#include <string.h>
#include <gio/gio.h>
#include "settings.h"
#include "power-profiles.h"

#define PPD_NAME C8_PPD_NAME
#define PPD_PATH C8_PPD_PATH
#define MODES_MAX 4

static struct {
	GDBusProxy *proxy;
	bool asked;
	struct c8ui_widget *box, *choice; // on the page while it's shown
	const char *names[MODES_MAX];
	int count;
} pm;

static int active_index(void) {
	GVariant *v = g_dbus_proxy_get_cached_property(pm.proxy, "ActiveProfile");
	int at = -1;
	for (int i = 0; v && i < pm.count; i++) {
		if (strcmp(g_variant_get_string(v, NULL), pm.names[i]) == 0) {
			at = i;
		}
	}
	if (v) {
		g_variant_unref(v);
	}
	return at;
}

static void set_done(GObject *source, GAsyncResult *result, gpointer data) {
	GError *error = NULL;
	GVariant *ret = g_dbus_proxy_call_finish(G_DBUS_PROXY(source), result,
		&error);
	if (ret) {
		g_variant_unref(ret);
		return;
	}
	show_error("Couldn't change the power mode", error->message);
	g_error_free(error);
}

static void mode_changed(struct c8ui_widget *w, void *data) {
	int i = c8ui_choice_get(w);
	if (i < 0 || i >= pm.count || pm.proxy == NULL) {
		return;
	}
	g_dbus_proxy_call(pm.proxy, "org.freedesktop.DBus.Properties.Set",
		g_variant_new("(ssv)", PPD_NAME, "ActiveProfile",
		g_variant_new_string(pm.names[i])), G_DBUS_CALL_FLAGS_NONE, -1, NULL,
		set_done, NULL);
}

/* The section, from what the daemon says now; nothing without a choice */
static void fill(void) {
	if (pm.box == NULL) {
		return;
	}
	const char *labels[MODES_MAX];
	int count = 0;
	for (size_t i = 0; pm.proxy && i < G_N_ELEMENTS(c8_power_profiles); i++) {
		if (c8_power_profile_offered(pm.proxy, c8_power_profiles[i].name) && count < MODES_MAX) {
			pm.names[count] = c8_power_profiles[i].name;
			labels[count++] = c8_power_profiles[i].label;
		}
	}
	if (pm.choice && count == pm.count) {
		c8ui_choice_set(pm.choice, active_index());
		return;
	}
	pm.count = count;
	c8ui_widget_clear(pm.box);
	pm.choice = NULL;
	pm.box->hidden = count < 2;
	if (count >= 2) {
		add(pm.box, section_title("Power mode"));
		c8ui_widget_track(c8ui_choice_new(NULL, labels, count,
			active_index(), mode_changed, NULL), &pm.choice);
		add(pm.box, pm.choice);
		add(pm.box, caption("Power saver makes the battery last longer; best "
			"performance makes the PC faster but warmer and louder."));
	}
	c8ui_widget_queue_layout(pm.box);
}

static void props_changed(GDBusProxy *proxy, GVariant *changed,
		GStrv invalidated, gpointer data) {
	fill();
}

static void owner_changed(GObject *proxy, GParamSpec *spec, gpointer data) {
	fill();
}

static void got_proxy(GObject *source, GAsyncResult *result, gpointer data) {
	GError *error = NULL;
	pm.proxy = g_dbus_proxy_new_for_bus_finish(result, &error);
	if (pm.proxy == NULL) {
		g_warning("Power modes: %s", error->message);
		g_error_free(error);
		return;
	}
	g_signal_connect(pm.proxy, "g-properties-changed",
		G_CALLBACK(props_changed), NULL);
	g_signal_connect(pm.proxy, "notify::g-name-owner",
		G_CALLBACK(owner_changed), NULL);
	fill();
}

struct c8ui_widget *power_mode_new(void) {
	c8ui_widget_track(c8ui_box_new(C8UI_VERTICAL, 18), &pm.box); // as the page
	pm.box->hidden = true;
	pm.count = 0;
	pm.choice = NULL;
	if (!pm.asked) {
		pm.asked = true;
		g_dbus_proxy_new_for_bus(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
			NULL, PPD_NAME, PPD_PATH, PPD_NAME, NULL, got_proxy, NULL);
	} else {
		fill();
	}
	return pm.box;
}
