/*
 * The slate's controls: in XR a row of tiles under a window's
 * slate, on its plane: its icon and title (drag to carry it), then
 * Follow me, Move, Minimise, Maximise, Show as 3D and Close each a tile
 * of its own; and a handle
 * at its bottom right corner to drag it bigger or smaller.
 * The compositor draws it (glasses.c, from the texture made here) and
 * takes its presses (hologram.c); the app never sees it.
 */
#include <stdlib.h>
#include <string.h>
#include <drm_fourcc.h>
#include <wlr/render/wlr_texture.h>
#include "server.h"

#define BUTTON_W 48
#define TILE_GAP 4 // between the tiles
#define TITLE_MIN 120 // the title's tile, as wide as the window lets it be
#define TITLE_MAX 320
#define ICON_PX 22

/* After the title, Close at the far end, as everywhere */
static const enum c8_slatebar_part buttons[] = {
	C8_SLATEBAR_FOLLOW, C8_SLATEBAR_MOVE, C8_SLATEBAR_MINIMISE,
	C8_SLATEBAR_MAXIMISE, C8_SLATEBAR_STEREO, C8_SLATEBAR_CLOSE,
};
#define BUTTON_COUNT (int)(sizeof(buttons) / sizeof(buttons[0]))
#define BUTTONS_W (BUTTON_COUNT * (TILE_GAP + BUTTON_W))

static int title_width(int w) {
	int t = w - BUTTONS_W;
	return t < TITLE_MIN ? TITLE_MIN : t > TITLE_MAX ? TITLE_MAX : t;
}

int c8_slatebar_width(int w) {
	return title_width(w) + BUTTONS_W;
}

/* What is at px, py: pixels from the window's top left on its plane */
enum c8_slatebar_part c8_slatebar_part_at(int w, int h, double px,
		double py) {
	int bottom = h + C8_SLATEBAR_GAP + C8_SLATEBAR_H;
	if (py >= h && py < bottom && px >= 0 && px < c8_slatebar_width(w)) {
		/* The gaps count as the tile before them, and the room above
		 * as the tile under it, so moving down onto one from the
		 * window never falls through */
		int tw = title_width(w);
		if (px < tw + TILE_GAP) {
			return C8_SLATEBAR_TITLE;
		}
		int i = (int)((px - tw - TILE_GAP) / (TILE_GAP + BUTTON_W));
		return buttons[i < BUTTON_COUNT ? i : BUTTON_COUNT - 1];
	}
	int k = C8_SLATEBAR_HANDLE;
	bool right = px >= w && px < w + k && py >= h - k && py < h + k;
	bool below = py >= h && py < h + k && px >= w - k && px < w + k;
	return right || below ? C8_SLATEBAR_CORNER : C8_SLATEBAR_NONE;
}

static void glyph(cairo_t *cr, enum c8_slatebar_part part, double cx,
		double cy, bool on) {
	cairo_set_source_rgb(cr, 1, 1, 1);
	cairo_set_line_width(cr, 1.5);
	switch (part) {
	case C8_SLATEBAR_FOLLOW:
		/* A pin; filled while it follows you */
		cairo_arc(cr, cx, cy - 3, 5, 0, 2 * M_PI);
		if (on) {
			cairo_fill_preserve(cr);
		}
		cairo_move_to(cr, cx, cy + 2);
		cairo_line_to(cr, cx, cy + 9);
		break;
	case C8_SLATEBAR_MOVE:
		/* Arrows four ways */
		for (int i = 0; i < 4; i++) {
			double a = i * M_PI / 2, c = cos(a), s = sin(a);
			cairo_move_to(cr, cx, cy);
			cairo_line_to(cr, cx + 8 * c, cy + 8 * s);
			cairo_move_to(cr, cx + 5 * c - 3 * s, cy + 5 * s + 3 * c);
			cairo_line_to(cr, cx + 8 * c, cy + 8 * s);
			cairo_line_to(cr, cx + 5 * c + 3 * s, cy + 5 * s - 3 * c);
		}
		break;
	case C8_SLATEBAR_MINIMISE:
		/* A line along the bottom, as it always was */
		cairo_move_to(cr, cx - 7, cy + 6);
		cairo_line_to(cr, cx + 7, cy + 6);
		break;
	case C8_SLATEBAR_MAXIMISE:
		/* A box; two overlapping in the theatre, to come back */
		if (on) {
			cairo_rectangle(cr, cx - 6.5, cy - 3.5, 10, 10);
			cairo_move_to(cr, cx - 3.5, cy - 3.5);
			cairo_line_to(cr, cx - 3.5, cy - 6.5);
			cairo_line_to(cr, cx + 6.5, cy - 6.5);
			cairo_line_to(cr, cx + 6.5, cy + 3.5);
			cairo_line_to(cr, cx + 3.5, cy + 3.5);
		} else {
			cairo_rectangle(cr, cx - 6.5, cy - 6.5, 13, 13);
		}
		break;
	case C8_SLATEBAR_CLOSE:
		c8_cross_path(cr, cx, cy, 6);
		break;
	case C8_SLATEBAR_STEREO:
		break; // stereo_glyph: it has three states
	default:
		break;
	}
	cairo_stroke(cr);
}

/* Show as 3D: the window's picture as it is taken apart, one frame
 * flat, two side by side or one over the other */
static void stereo_glyph(cairo_t *cr, const char *font, int layout,
		double cx, double cy) {
	cairo_set_source_rgb(cr, 1, 1, 1);
	cairo_set_line_width(cr, 1.5);
	if (layout == 1) {
		cairo_rectangle(cr, cx - 8.5, cy - 4.5, 8, 9);
		cairo_rectangle(cr, cx + 0.5, cy - 4.5, 8, 9);
		cairo_stroke(cr);
	} else if (layout == 2) {
		cairo_rectangle(cr, cx - 6.5, cy - 8.5, 13, 8);
		cairo_rectangle(cr, cx - 6.5, cy + 0.5, 13, 8);
		cairo_stroke(cr);
	} else {
		/* Flat: says what it does */
		int tw = c8_text_width(cr, font, 14, 2, "3D");
		c8_text(cr, font, 14, 2, "3D", cx - tw / 2.0, cy - 9, tw + 2, 1);
	}
}

/* A tile: the accent on the window you're using, dark on the others;
 * lighter under the ray, more so pressed (Close goes red) */
static void tile(cairo_t *cr, const struct c8_slatebar_look *look,
		int part, double x, double w, double h) {
	c8ui_set_source(cr, look->focused ? look->accent : 0x2b2b2b, 1);
	cairo_rectangle(cr, x, 0, w, h);
	cairo_fill(cr);
	if (look->hover != part) {
		return;
	}
	if (part == C8_SLATEBAR_CLOSE) {
		c8ui_set_source(cr, look->pressed == part ? 0xf1707a : 0xe81123, 1);
	} else {
		cairo_set_source_rgba(cr, 1, 1, 1, look->pressed == part ? 0.35 : 0.2);
	}
	cairo_rectangle(cr, x, 0, w, h);
	cairo_fill(cr);
}

/* w is the window's width: the row's comes from it */
static void draw_bar(cairo_t *cr, int w, int h, struct c8_view *view,
		const struct c8_slatebar_look *look) {
	int tw = title_width(w);
	tile(cr, look, C8_SLATEBAR_TITLE, 0, tw, h);
	int x = 10;
	/* Room for the icon only once there is one (it loads in the
	 * background, and the bar is drawn again when it comes) */
	if (view->style.icon) {
		c8_view_style_draw_icon(view, cr, x, (h - ICON_PX) / 2.0, ICON_PX);
		x += ICON_PX + 10;
	}
	const char *title = c8_view_label(view);
	cairo_set_source_rgb(cr, 1, 1, 1);
	c8_text(cr, view->server->config.font, 15, 0, title ? title : "", x,
		(h - 20) / 2.0, tw - x - 10, 1);
	for (int i = 0; i < BUTTON_COUNT; i++) {
		int b = buttons[i];
		double bx = tw + TILE_GAP + i * (TILE_GAP + BUTTON_W);
		tile(cr, look, b, bx, BUTTON_W, h);
		if (b == C8_SLATEBAR_STEREO) {
			stereo_glyph(cr, view->server->config.font, look->stereo,
				bx + BUTTON_W / 2.0, h / 2.0);
			continue;
		}
		glyph(cr, b, bx + BUTTON_W / 2.0, h / 2.0,
			b == C8_SLATEBAR_FOLLOW ? look->follow :
			b == C8_SLATEBAR_MAXIMISE && look->theatre);
	}
}

static bool look_equal(const struct c8_slatebar_look *a,
		const struct c8_slatebar_look *b) {
	return a->width == b->width && a->hover == b->hover &&
		a->pressed == b->pressed && a->focused == b->focused &&
		a->follow == b->follow && a->theatre == b->theatre &&
		a->stereo == b->stereo &&
		a->accent == b->accent &&
		a->icon == b->icon && strcmp(a->title ? a->title : "",
			b->title ? b->title : "") == 0;
}

/* A cairo drawing as a texture (and the surface is done with) */
static struct wlr_texture *texture_from(struct c8_server *server,
		cairo_surface_t *s) {
	cairo_surface_flush(s);
	struct wlr_texture *texture = wlr_texture_from_pixels(server->renderer,
		DRM_FORMAT_ARGB8888, cairo_image_surface_get_stride(s),
		cairo_image_surface_get_width(s), cairo_image_surface_get_height(s),
		cairo_image_surface_get_data(s));
	cairo_surface_destroy(s);
	return texture;
}

/* The bar's picture as it looks now; made again only when that changed */
struct wlr_texture *c8_slatebar_texture(struct c8_view *view,
		const struct c8_slatebar_look *look) {
	struct c8_server *server = view->server;
	struct c8_slatebar_look want = *look;
	const char *title = c8_view_title(view);
	want.title = (char *)(title ? title : "");
	want.icon = view->style.icon != NULL;
	if (view->holo.bar && look_equal(&view->holo.bar_look, &want)) {
		return view->holo.bar;
	}
	int w = want.width, h = C8_SLATEBAR_H;
	if (w <= 0) {
		return NULL;
	}
	/* Clear between the tiles */
	cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
		c8_slatebar_width(w), h);
	cairo_t *cr = cairo_create(s);
	draw_bar(cr, w, h, view, &want);
	cairo_destroy(cr);
	struct wlr_texture *texture = texture_from(server, s);
	if (texture == NULL) {
		return view->holo.bar; // the old one is better than none
	}
	c8_slatebar_drop(view);
	view->holo.bar = texture;
	view->holo.bar_look = want;
	view->holo.bar_look.title = c8_strdup(want.title);
	return texture;
}

void c8_slatebar_drop(struct c8_view *view) {
	if (view->holo.bar) {
		wlr_texture_destroy(view->holo.bar);
		view->holo.bar = NULL;
	}
	free(view->holo.bar_look.title);
	view->holo.bar_look.title = NULL;
}

/* A window opening out of view: its icon on its tile's colour, as Start
 * shows it, for the edge of the view (glasses.c draws the arrow) */
struct wlr_texture *c8_slatebar_beacon(struct c8_view *view) {
	struct c8_server *server = view->server;
	uint32_t color = view->style.set ? view->style.color :
		server->config.accent;
	/* With its icon, unless one named turned out not to load (known once
	 * the first texture tried it: then made again with the letter) */
	bool icon = view->style.icon_name != NULL &&
		(!view->style.icon_loaded || view->style.icon != NULL);
	if (view->holo.beacon && view->holo.beacon_color == color &&
			view->holo.beacon_icon == icon) {
		return view->holo.beacon;
	}
	int px = C8_BEACON_PX;
	cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, px,
		px);
	cairo_t *cr = cairo_create(s);
	c8ui_set_source(cr, color, 1);
	cairo_paint(cr);
	if (icon) {
		c8_view_style_draw_icon(view, cr, px * 0.2, px * 0.2, px * 0.6);
	}
	if (view->style.icon == NULL) {
		/* No icon: the first letter of its name */
		const char *name = c8_view_label(view);
		char letter[2] = { name && name[0] ? name[0] : '?', '\0' };
		if (letter[0] >= 'a' && letter[0] <= 'z') {
			letter[0] -= 'a' - 'A';
		}
		cairo_set_source_rgb(cr, 1, 1, 1);
		int tw = c8_text_width(cr, server->config.font, 36, 0, letter);
		c8_text(cr, server->config.font, 36, 0, letter, (px - tw) / 2.0,
			(px - 48) / 2.0, px, 1);
	}
	cairo_destroy(cr);
	struct wlr_texture *texture = texture_from(server, s);
	if (texture == NULL) {
		return view->holo.beacon;
	}
	c8_slatebar_beacon_drop(view);
	view->holo.beacon = texture;
	view->holo.beacon_color = color;
	view->holo.beacon_icon = icon;
	return texture;
}

void c8_slatebar_beacon_drop(struct c8_view *view) {
	if (view->holo.beacon) {
		wlr_texture_destroy(view->holo.beacon);
		view->holo.beacon = NULL;
	}
}

#define LABEL_H 32
#define LABEL_PAD 10
#define LABEL_MAX 360

struct wlr_texture *c8_slatebar_label(struct c8_server *server,
		const char *text) {
	if (text == NULL || text[0] == '\0') {
		return NULL;
	}
	if (server->xr.label && strcmp(server->xr.label_text, text) == 0) {
		return server->xr.label;
	}
	const char *font = server->config.font;
	/* As wide as the text, so it has to be measured first */
	cairo_surface_t *s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
	cairo_t *cr = cairo_create(s);
	int tw = c8_text_width(cr, font, 15, 0, text);
	cairo_destroy(cr);
	cairo_surface_destroy(s);
	int w = tw + 2 * LABEL_PAD;
	w = w > LABEL_MAX ? LABEL_MAX : w;
	s = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, LABEL_H);
	cr = cairo_create(s);
	c8ui_set_source(cr, 0x2b2b2b, 1);
	cairo_paint(cr);
	cairo_set_source_rgb(cr, 1, 1, 1);
	c8_text(cr, font, 15, 0, text, LABEL_PAD, (LABEL_H - 20) / 2.0,
		w - 2 * LABEL_PAD, 1);
	cairo_destroy(cr);
	struct wlr_texture *texture = texture_from(server, s);
	if (texture == NULL) {
		return NULL;
	}
	c8_slatebar_label_drop(server);
	server->xr.label = texture;
	server->xr.label_text = c8_strdup(text);
	return texture;
}

void c8_slatebar_label_drop(struct c8_server *server) {
	if (server->xr.label) {
		wlr_texture_destroy(server->xr.label);
		server->xr.label = NULL;
	}
	free(server->xr.label_text);
	server->xr.label_text = NULL;
}
