/*
 * The live tile, over org.cont8nuum.Tiles1: the forecast template, as
 * Windows 8's Weather tile was. The glyph and temperature now, what it's
 * like and today's high and low under them, and the next days in columns
 * on the wide and large tiles. An alert in force takes the last line.
 *
 * The app's tile and the widget's show the default place; a place pinned
 * on its own (RequestPin) has a tile of its own, updated when it's there.
 */
#include <gio/gio.h>
#include <string.h>
#include "tiles.h"
#include "weather.h"

#define DAYS 4

static void add(GVariantBuilder *b, const char *key, const char *value) {
	if (value && value[0]) {
		g_variant_builder_add(b, "{ss}", key, value);
	}
}

static void add_take(GVariantBuilder *b, const char *key, char *value) {
	add(b, key, value);
	g_free(value);
}

char *ws_place_tile_id(struct ws_place *p) {
	return g_strconcat(PLACE_TILE_PREFIX, p->id, NULL);
}

void ws_tile_update(const char *tile, struct ws_place *p) {
	if (p == NULL) {
		c8_tiles_call("Clear", g_variant_new("(s)", tile));
		return;
	}
	struct ws_forecast *f = p->forecast;
	if (f == NULL && p->now == NULL) {
		return; // nothing to say yet: the tile keeps what it had
	}
	GVariantBuilder b;
	g_variant_builder_init(&b, G_VARIANT_TYPE("a{ss}"));
	add(&b, "template", "forecast");
	/* The town, without its state: the tile is small */
	const char *name = ws_place_label(p);
	char *town = g_strndup(name, strcspn(name, ","));
	add(&b, "label", town);
	g_free(town);
	add(&b, "glyph", ws_glyph(ws_now_icon(p)));
	double temp = p->now ? p->now->temp : NAN;
	if (isnan(temp) && f && f->hours->len) {
		temp = g_array_index(f->hours, struct ws_hour, 0).temp;
	}
	add_take(&b, "block", ws_temp(temp, false));
	add(&b, "text1", ws_now_summary(p));
	int line = 2;
	if (f && f->days->len) {
		struct ws_day *d = &g_array_index(f->days, struct ws_day, 0);
		char *hi = ws_temp(d->high, false), *lo = ws_temp(d->low, false);
		char key[16];
		g_snprintf(key, sizeof(key), "text%d", line++);
		add_take(&b, key, g_strdup_printf("%s / %s", hi, lo));
		g_free(hi);
		g_free(lo);
		for (guint i = 0; i < DAYS && i < f->days->len; i++) {
			struct ws_day *day = &g_array_index(f->days, struct ws_day, i);
			char k[16];
			g_snprintf(k, sizeof(k), "day%u", i + 1);
			char *label = ws_day_label(day, (int)i);
			/* Just the weekday: a column is narrow */
			char *space = strchr(label, ' ');
			if (space && i > 0) {
				*space = '\0';
			}
			add_take(&b, k, label);
			g_snprintf(k, sizeof(k), "glyph%u", i + 1);
			add(&b, k, ws_glyph(day->icon));
			g_snprintf(k, sizeof(k), "value%u", i + 1);
			add_take(&b, k, ws_temp(day->high, false));
			g_snprintf(k, sizeof(k), "low%u", i + 1);
			add_take(&b, k, ws_temp(day->low, false));
		}
	}
	if (p->alerts && p->alerts->len) {
		struct ws_alert *a = p->alerts->pdata[0];
		char key[16];
		g_snprintf(key, sizeof(key), "text%d", line++);
		add_take(&b, key, g_strdup_printf("⚠ %s", a->event));
	}
	c8_tiles_call("Update", g_variant_new("(ssa{ss})", tile, "", &b));
}

/* A place's own tile, only when it is on Start */
static void got_tile(GObject *source, GAsyncResult *res, gpointer data) {
	char *id = data;
	GVariant *r = g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res,
		NULL);
	gboolean pinned = FALSE;
	if (r) {
		const char *size;
		g_variant_get(r, "(b&s)", &pinned, &size);
		g_variant_unref(r);
	}
	if (pinned) {
		for (guint i = 0; i < ws_places->len; i++) {
			struct ws_place *p = ws_places->pdata[i];
			char *tile = ws_place_tile_id(p);
			if (strcmp(tile, id) == 0) {
				ws_tile_update(tile, p);
			}
			g_free(tile);
		}
	}
	g_free(id);
}

void ws_tiles_update(void) {
	struct ws_place *home = ws_home();
	ws_tile_update(APP_ID, home);
	ws_tile_update(TILE_WIDGET_ID, home);
	GDBusConnection *bus = c8_tiles_bus();
	if (bus == NULL) {
		return;
	}
	for (guint i = 0; i < ws_places->len; i++) {
		char *tile = ws_place_tile_id(ws_places->pdata[i]);
		g_dbus_connection_call(bus, C8_TILES_NAME, C8_TILES_PATH,
			C8_TILES_IFACE, "GetTile",
			g_variant_new("(s)", tile), G_VARIANT_TYPE("(bs)"),
			G_DBUS_CALL_FLAGS_NONE, 5000, NULL, got_tile, tile);
	}
}
