/*
 * The launch splash: a tile-coloured square grows from what was clicked
 * to fill the screen with the app's icon in the middle, and stays until
 * the app's window turns up (or a few seconds pass, for apps that never
 * show one). It has its own surface above everything; in XR
 * the compositor makes that a slate of a window's size where the window
 * will open.
 */
#include <math.h>
#include "shell.h"

#define SPLASH_MS 260
#define SPLASH_TIMEOUT_MS 5000
#define SPLASH_ICON 128

static struct {
	struct shell_surface *surface;
	struct c8ui_widget *w;
	bool active;
	struct rect from;
	uint32_t color;
	struct c8ui_icon *icon;
	int64_t start;
	guint timeout;
} sp;

static void splash_draw(struct c8ui_widget *w, cairo_t *cr) {
	if (!sp.active) {
		return;
	}
	if (sp.start == 0) {
		sp.start = now_ms();
	}
	double p = ease_out((double)(now_ms() - sp.start) /
		SPLASH_MS);
	double W = w->rect.width, H = w->rect.height;
	struct rect from = sp.from;
	if (shell.appearance.xr) {
		/* Its own slate where the window will be: the tile clicked is
		 * elsewhere, so it grows from the middle */
		from = (struct rect){ (int)W / 2 - SPLASH_ICON,
			(int)H / 2 - SPLASH_ICON,
			2 * SPLASH_ICON, 2 * SPLASH_ICON };
	}
	double x = from.x * (1 - p), y = from.y * (1 - p);
	double bw = from.width + (W - from.width) * p;
	double bh = from.height + (H - from.height) * p;
	c8ui_set_source(cr, sp.color, 1);
	cairo_rectangle(cr, x, y, bw, bh);
	cairo_fill(cr);
	if (sp.icon) {
		cairo_push_group(cr);
		c8ui_icon_draw_fit(cr, sp.icon, x + bw / 2, y + bh / 2, SPLASH_ICON);
		cairo_pop_group_to_source(cr);
		cairo_paint_with_alpha(cr, p);
	}
}

static bool splash_tick(struct c8ui_widget *w, int64_t now) {
	c8ui_widget_queue_draw(w);
	return sp.active && (sp.start == 0 || now - sp.start < SPLASH_MS);
}

static void splash_destroy(struct c8ui_widget *w) {
	sp.w = NULL;
}

static const struct c8ui_widget_impl splash_impl = {
	.name = "splash",
	.draw = splash_draw,
	.tick = splash_tick,
	.destroy = splash_destroy,
};

static gboolean timed_out(gpointer data) {
	sp.timeout = 0;
	splash_end();
	return G_SOURCE_REMOVE;
}

void splash_show(struct rect from, uint32_t color, const char *icon) {
	if (sp.surface == NULL) {
		return;
	}
	c8ui_icon_destroy(sp.icon);
	/* Loaded right here: one icon, and it has to be there on the
	 * first frame */
	sp.icon = icon && icon[0] ?
		c8ui_icon_load(shell.appearance.icon_theme, icon) : NULL;
	sp.from = from;
	sp.color = color;
	sp.start = 0;
	sp.active = true;
	if (sp.timeout) {
		g_source_remove(sp.timeout);
	}
	sp.timeout = g_timeout_add(SPLASH_TIMEOUT_MS, timed_out, NULL);
	shell_surface_show(sp.surface);
	if (sp.w) {
		c8ui_widget_animate(sp.w);
	}
}

void splash_end(void) {
	if (!sp.active) {
		return;
	}
	sp.active = false;
	g_clear_handle_id(&sp.timeout, g_source_remove);
	c8ui_icon_destroy(sp.icon);
	sp.icon = NULL;
	shell_surface_hide(sp.surface);
}

void splash_init(void) {
	sp.surface = shell_surface_new(C8_SHELL_V1_ROLE_SPLASH);
	shell_surface_hide(sp.surface);
	sp.w = c8ui_widget_new(&splash_impl, NULL);
	sp.w->fill = true;
	sp.w->expand = true;
	c8ui_root_set_child(shell_surface_root(sp.surface), sp.w);
}
