/*
 * An entry of a list as a card: a post (who, when, the words, pictures, a
 * link's card, a quoted post, and the row to reply, repost and like), a
 * notification's line, a person. One layout says where each part is, for
 * measuring, drawing and finding what a click was on.
 */
#include <math.h>
#include <string.h>
#include "app.h"

#define PAD 14
#define AVATAR 40
#define GUTTER 12
#define INDENT 20 // a reply's, per level: room for a rail in it
#define INDENT_MAX 4
#define ACT_W 78
#define ACT_H 30
#define GAP 4 // between pictures
#define MEDIA_MAX_H 300 // a picture in a list: taller ones are cut to it
#define MEDIA_FOCUS_H 560 // in the post a thread is about
#define LINES 14 // of a post in a list
#define LINK_H 24
#define EXT_THUMB 76
#define QUOTE_PAD 10
#define QUOTE_PICTURE 120
#define FOLLOW_W 96
#define FOLLOW_H 30

struct layout {
	double x, w; // the card in its rect: in from the left for a reply
	double height;
	struct sky_post *p; // drawn as a post, if set
	bool focus, bar; // bar: the pictures are behind a line of words
	double reason_y; // -1: none
	double col_x, col_w; // right of the avatar
	struct c8ui_rect avatar, head, text, links, media, cell[SKY_IMAGES_MAX];
	struct c8ui_rect ext, quote, meta, acts, follow;
	int cells;
	const char *bar_text;
};

static struct c8ui_font body_font(bool focus) {
	static const double sizes[] = { 13.5, 15, 17 };
	double size = sizes[CLAMP(sky_settings.text_size, 0, 2)];
	return (struct c8ui_font){ focus ? size + 3 : size, C8UI_WEIGHT_NORMAL };
}

static const struct c8ui_font strong = { 15, C8UI_WEIGHT_SEMIBOLD };
static const struct c8ui_font small = { 13, C8UI_WEIGHT_NORMAL };
static const struct c8ui_font small_strong = { 13, C8UI_WEIGHT_SEMIBOLD };

static double text_h(const char *family, struct c8ui_font font,
		const char *text, double width, int lines) {
	double w, h;
	if (text == NULL || text[0] == '\0') {
		return 0;
	}
	c8ui_text_size(family, font, text, width, lines, &w, &h);
	return ceil(h);
}

static double text_w(const char *family, struct c8ui_font font,
		const char *text, double width) {
	double w, h;
	c8ui_text_size(family, font, text, width, 1, &w, &h);
	return ceil(w);
}

static bool pictures_shown(struct sky_post *p) {
	return sky_settings.images && (!p->sensitive || sky_settings.sensitive);
}

/* The quoted post's box, inside the column */
static double quote_height(const char *family, struct sky_post *p,
		double width) {
	if (p->quote_gone) {
		return 2 * QUOTE_PAD + text_h(family, small, p->quote_gone,
			width - 2 * QUOTE_PAD, 2);
	}
	struct sky_post *q = p->quote;
	double inner = width - 2 * QUOTE_PAD;
	double h = QUOTE_PAD + 20;
	double th = text_h(family, small, sky_shown_text(q->text), inner, 5);
	h += th ? th + 4 : 0;
	if ((q->n_images || q->video_thumb) && pictures_shown(q)) {
		h += QUOTE_PICTURE + 6;
	} else if (q->n_images || q->video) {
		h += 20;
	}
	if (q->ext_title || q->ext_uri) {
		h += 20;
	}
	return h + QUOTE_PAD - 2;
}

static double ext_height(const char *family, struct sky_post *p,
		double width, bool thumb) {
	double tw = width - 2 * QUOTE_PAD - (thumb ? EXT_THUMB + 10 : 0);
	double h = text_h(family, small_strong, p->ext_title ? p->ext_title :
		p->ext_uri, tw, 2) + 18 + 2 * QUOTE_PAD;
	return thumb && h < EXT_THUMB ? EXT_THUMB : h;
}

/* A person or a notice: the picture, a line, and words under it */
static void lay_plain(const char *family, struct sky_item *it,
		struct layout *l) {
	double y = PAD;
	l->avatar = (struct c8ui_rect){ l->x + PAD, y, AVATAR, AVATAR };
	l->col_x = l->x + PAD + AVATAR + GUTTER;
	l->col_w = l->w - 2 * PAD - AVATAR - GUTTER;
	double head_w = l->col_w;
	if (it->kind == SKY_ITEM_ACTOR && sky_me && g_strcmp0(it->did,
			sky_me->did) != 0 && l->col_w > FOLLOW_W + 80) {
		l->follow = (struct c8ui_rect){ l->x + l->w - PAD - FOLLOW_W, y + 4,
			FOLLOW_W, FOLLOW_H };
		head_w -= FOLLOW_W + 10;
	}
	double hh = it->kind == SKY_ITEM_ACTOR ? 40 : text_h(family, small,
		"X", head_w, 1) * 2 + 2;
	l->head = (struct c8ui_rect){ l->col_x, y, head_w, hh };
	y += hh + 4;
	const char *words = sky_shown_text(it->kind == SKY_ITEM_NOTICE &&
		it->post ? it->post->text : it->text);
	double th = text_h(family, small, words, l->col_w, 3);
	l->text = (struct c8ui_rect){ l->col_x, y, l->col_w, th };
	y += th;
	l->height = fmax(y, PAD + AVATAR) + PAD;
}

static void lay(const char *family, struct sky_item *it, double width,
		struct layout *l) {
	memset(l, 0, sizeof(*l));
	l->reason_y = -1;
	int depth = it->depth > INDENT_MAX ? INDENT_MAX : it->depth;
	l->x = depth * INDENT;
	l->w = width - l->x;
	if (it->kind == SKY_ITEM_MORE) {
		l->height = 48;
		return;
	}
	if (it->kind == SKY_ITEM_GAP) {
		l->height = 40;
		return;
	}
	bool as_post = it->kind == SKY_ITEM_POST ||
		(it->kind == SKY_ITEM_NOTICE && it->whole && it->post);
	if (!as_post || it->post == NULL) {
		lay_plain(family, it, l);
		return;
	}
	struct sky_post *p = l->p = it->post;
	l->focus = it->focus;
	double y = PAD;
	l->col_x = l->x + PAD + AVATAR + GUTTER;
	l->col_w = l->w - 2 * PAD - AVATAR - GUTTER;
	if (it->reason) {
		l->reason_y = y - 2;
		y += 22;
	}
	l->avatar = (struct c8ui_rect){ l->x + PAD, y, AVATAR, AVATAR };
	l->head = (struct c8ui_rect){ l->col_x, y, l->col_w, 20 };
	y += 24;
	if (l->focus) {
		/* The post the page is about: its words across the card */
		y = l->avatar.y + AVATAR + 12;
		l->col_x = l->x + PAD;
		l->col_w = l->w - 2 * PAD;
		l->head.height = AVATAR;
	}
	double th = text_h(family, body_font(l->focus), sky_shown_text(p->text),
		l->col_w,
		l->focus ? 0 : LINES);
	if (th) {
		l->text = (struct c8ui_rect){ l->col_x, y, l->col_w, th };
		y += th + 10;
	}
	if (l->focus && p->links && p->links->len) {
		l->links = (struct c8ui_rect){ l->col_x, y, l->col_w,
			p->links->len * LINK_H };
		y += l->links.height + 8;
	}
	/* The words sit beside the face; what follows them goes across the
	 * whole card, so no strip under the face is left empty */
	if ((p->n_images || p->video || p->ext_uri || p->quote || p->quote_gone) &&
			!(it->chain_down && !l->focus)) {
		y = fmax(y, l->avatar.y + AVATAR + 10);
	}
	/* Except where a thread's line runs down through the face: then all
	 * of it stays right of the line */
	bool lined = it->chain_down && !l->focus;
	if (!lined) {
		l->col_x = l->x + PAD;
		l->col_w = l->w - 2 * PAD;
	}
	bool media = p->n_images || p->video;
	if (media && !pictures_shown(p)) {
		l->bar = true;
		l->bar_text = p->sensitive && sky_settings.images ?
			"Sensitive pictures. Click to see them." :
			p->video ? "A video" : p->n_images == 1 ? "A picture" : "Pictures";
		l->media = (struct c8ui_rect){ l->col_x, y, l->col_w, 34 };
		y += 34 + 10;
	} else if (p->n_images == 1 || (p->video && p->n_images == 0)) {
		/* As wide as the card; a tall one is cut, not made narrow */
		double aspect = p->n_images ? p->images[0].aspect : p->video_aspect;
		aspect = aspect <= 0 ? 16.0 / 9 : CLAMP(aspect, 0.5, 3);
		double w = l->col_w;
		double h = fmin(floor(w / aspect), l->focus ? MEDIA_FOCUS_H :
			MEDIA_MAX_H);
		l->media = (struct c8ui_rect){ l->col_x, y, w, h };
		l->cell[0] = l->media;
		l->cells = 1;
		y += h + 10;
	} else if (p->n_images > 1) {
		double cw = floor((l->col_w - GAP) / 2);
		double ch = fmin(floor(cw * 0.75), MEDIA_MAX_H);
		int rows = (p->n_images + 1) / 2;
		for (int i = 0; i < p->n_images; i++) {
			bool last_alone = i == p->n_images - 1 && p->n_images % 2;
			l->cell[i] = (struct c8ui_rect){ l->col_x + (i % 2) * (cw + GAP),
				y + (i / 2) * (ch + GAP), last_alone ? l->col_w : cw, ch };
		}
		l->cells = p->n_images;
		l->media = (struct c8ui_rect){ l->col_x, y, l->col_w,
			rows * ch + (rows - 1) * GAP };
		y += l->media.height + 10;
	}
	if (p->ext_uri) {
		bool thumb = p->ext_thumb && sky_settings.images;
		double h = ext_height(family, p, l->col_w, thumb);
		l->ext = (struct c8ui_rect){ l->col_x, y, l->col_w, h };
		y += h + 10;
	}
	if (p->quote || p->quote_gone) {
		double h = quote_height(family, p, l->col_w);
		l->quote = (struct c8ui_rect){ l->col_x, y, l->col_w, h };
		y += h + 10;
	}
	if (l->focus) {
		l->meta = (struct c8ui_rect){ l->col_x, y, l->col_w, 20 };
		y += 26;
	}
	if (!p->partial) {
		/* Under the words when there is nothing wider between */
		double ax = l->avatar.x + AVATAR + GUTTER - 6;
		if (!lined && (l->focus || l->cells || l->bar || l->ext.height > 0 ||
				l->quote.height > 0)) {
			ax = l->col_x - 6;
		}
		l->acts = (struct c8ui_rect){ ax, y - 4, 3 * ACT_W, ACT_H };
		y += ACT_H - 4;
		l->height = fmax(y, l->avatar.y + AVATAR + 8);
		return;
	}
	l->height = fmax(y, l->avatar.y + AVATAR) + PAD - 2;
}

double card_height(const char *family, struct sky_item *it, double width) {
	struct layout l;
	lay(family, it, width, &l);
	return ceil(l.height);
}

/* Glyphs */

static void path_bubble(cairo_t *cr, double x, double y, double s) {
	cairo_move_to(cr, x, y);
	cairo_line_to(cr, x + s, y);
	cairo_line_to(cr, x + s, y + s * 0.7);
	cairo_line_to(cr, x + s * 0.48, y + s * 0.7);
	cairo_line_to(cr, x + s * 0.22, y + s * 0.98);
	cairo_line_to(cr, x + s * 0.22, y + s * 0.7);
	cairo_line_to(cr, x, y + s * 0.7);
	cairo_close_path(cr);
}

static void path_heart(cairo_t *cr, double x, double y, double s) {
	double h = s * 0.92;
	cairo_move_to(cr, x + s / 2, y + h);
	cairo_curve_to(cr, x - s * 0.22, y + h * 0.5, x + s * 0.08, y - h * 0.18,
		x + s / 2, y + h * 0.26);
	cairo_curve_to(cr, x + s * 0.92, y - h * 0.18, x + s * 1.22, y + h * 0.5,
		x + s / 2, y + h);
	cairo_close_path(cr);
}

static void path_repost(cairo_t *cr, double x, double y, double s) {
	cairo_move_to(cr, x + s * 0.08, y + s * 0.62);
	cairo_line_to(cr, x + s * 0.08, y + s * 0.22);
	cairo_line_to(cr, x + s * 0.78, y + s * 0.22);
	cairo_move_to(cr, x + s * 0.6, y + s * 0.04);
	cairo_line_to(cr, x + s * 0.8, y + s * 0.22);
	cairo_line_to(cr, x + s * 0.6, y + s * 0.4);
	cairo_move_to(cr, x + s * 0.92, y + s * 0.38);
	cairo_line_to(cr, x + s * 0.92, y + s * 0.78);
	cairo_line_to(cr, x + s * 0.22, y + s * 0.78);
	cairo_move_to(cr, x + s * 0.4, y + s * 0.6);
	cairo_line_to(cr, x + s * 0.2, y + s * 0.78);
	cairo_line_to(cr, x + s * 0.4, y + s * 0.96);
}

static void path_person(cairo_t *cr, double x, double y, double s) {
	cairo_new_sub_path(cr);
	cairo_arc(cr, x + s / 2, y + s * 0.3, s * 0.22, 0, 2 * G_PI);
	cairo_new_sub_path(cr);
	cairo_arc(cr, x + s / 2, y + s * 1.02, s * 0.42, G_PI, 2 * G_PI);
}

/* In the current colour; filled is for what we did (liked) */
void card_glyph(cairo_t *cr, const char *name, double x, double y, double s,
		bool filled) {
	cairo_save(cr);
	cairo_set_line_width(cr, 1.5);
	cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
	cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
	cairo_new_path(cr);
	if (strcmp(name, "reply") == 0 || strcmp(name, "mention") == 0) {
		path_bubble(cr, x, y + s * 0.06, s);
	} else if (strcmp(name, "like") == 0) {
		path_heart(cr, x, y + s * 0.06, s);
	} else if (strcmp(name, "repost") == 0) {
		path_repost(cr, x, y, s);
		filled = false;
		if (cairo_get_line_width(cr) < 2) {
			cairo_set_line_width(cr, 1.7);
		}
	} else if (strcmp(name, "follow") == 0) {
		path_person(cr, x, y, s);
	} else if (strcmp(name, "quote") == 0) {
		/* Two marks */
		for (int i = 0; i < 2; i++) {
			double qx = x + i * s * 0.5;
			cairo_rectangle(cr, qx, y + s * 0.2, s * 0.34, s * 0.34);
			cairo_move_to(cr, qx + s * 0.34, y + s * 0.5);
			cairo_line_to(cr, qx + s * 0.1, y + s * 0.9);
		}
		filled = true;
	} else {
		/* A pin: a dot */
		cairo_arc(cr, x + s / 2, y + s / 2, s * 0.25, 0, 2 * G_PI);
		filled = true;
	}
	if (filled) {
		cairo_fill_preserve(cr);
	}
	cairo_stroke(cr);
	cairo_restore(cr);
}

/* Drawing */

static bool missed; // a picture drawn as a grey box, for now

static void picture(cairo_t *cr, const char *url, struct c8ui_rect r) {
	cairo_surface_t *s = url ? sky_image(url) : NULL;
	if (s) {
		c8ui_draw_surface_cover(cr, s, r.x, r.y, r.width, r.height);
	} else {
		c8ui_set_source(cr, 0xdfe5eb, 1);
		cairo_rectangle(cr, r.x, r.y, r.width, r.height);
		cairo_fill(cr);
	}
}

static void avatar(cairo_t *cr, const char *family, const char *url,
		const char *name, struct c8ui_rect r) {
	cairo_surface_t *s = url ? sky_image(url) : NULL;
	if (s) {
		c8ui_draw_surface_cover(cr, s, r.x, r.y, r.width, r.height);
		return;
	}
	/* The first letter on blue, as tiles without icons have */
	c8ui_set_source(cr, 0x3a8fd9, 1);
	cairo_rectangle(cr, r.x, r.y, r.width, r.height);
	cairo_fill(cr);
	if (name && name[0]) {
		char letter[8] = "";
		g_unichar_to_utf8(g_unichar_toupper(g_utf8_get_char(name)), letter);
		struct c8ui_font font = { r.height * 0.5, C8UI_WEIGHT_LIGHT };
		double w, h;
		c8ui_text_size(family, font, letter, 0, 1, &w, &h);
		cairo_set_source_rgb(cr, 1, 1, 1);
		c8ui_text_draw(cr, family, font, letter, r.x + (r.width - w) / 2,
			r.y + (r.height - h) / 2, 0, 1);
	}
}

static void box(cairo_t *cr, struct c8ui_rect r) {
	c8ui_set_source(cr, INK, 0.16);
	cairo_set_line_width(cr, 1);
	cairo_rectangle(cr, r.x + 0.5, r.y + 0.5, r.width - 1, r.height - 1);
	cairo_stroke(cr);
}

/* "Name  @handle · 2h" on one line, the name strong */
static void name_line(cairo_t *cr, const char *family, const char *name,
		const char *handle, int64_t date, double x, double y, double width,
		struct c8ui_font font) {
	double nw = text_w(family, font, name, width * 0.62);
	c8ui_set_source(cr, INK, 1);
	c8ui_text_draw(cr, family, font, name, x, y, width * 0.62, 1);
	char *age = sky_age(date);
	char *rest = g_strdup_printf("@%s%s%s", handle, age[0] ? " · " : "", age);
	c8ui_set_source(cr, INK, 0.58);
	if (width - nw - 8 > 30) {
		c8ui_text_draw(cr, family, small, rest, x + nw + 8,
			y + (font.size - small.size), width - nw - 8, 1);
	}
	g_free(rest);
	g_free(age);
}

static void draw_quote(cairo_t *cr, const char *family, struct sky_post *p,
		struct c8ui_rect r) {
	box(cr, r);
	double x = r.x + QUOTE_PAD, y = r.y + QUOTE_PAD, w = r.width - 2 * QUOTE_PAD;
	if (p->quote_gone) {
		c8ui_set_source(cr, INK, 0.58);
		c8ui_text_draw(cr, family, small, p->quote_gone, x, y, w, 2);
		return;
	}
	struct sky_post *q = p->quote;
	const char *name = sky_shown_name(q->did, q->name);
	avatar(cr, family, sky_shown_avatar(q->did, q->avatar), name,
		(struct c8ui_rect){ x, y, 18, 18 });
	name_line(cr, family, name, sky_shown_handle(q->did, q->handle), q->date,
		x + 24, y, w - 24, small_strong);
	y += 20;
	if (q->text) {
		c8ui_set_source(cr, INK, 0.9);
		y += c8ui_text_draw(cr, family, small, sky_shown_text(q->text), x, y,
			w, 5) + 4;
	}
	if ((q->n_images || q->video_thumb) && pictures_shown(q)) {
		picture(cr, q->n_images ? q->images[0].thumb : q->video_thumb,
			(struct c8ui_rect){ x, y, w, QUOTE_PICTURE });
		y += QUOTE_PICTURE + 6;
	} else if (q->n_images || q->video) {
		c8ui_set_source(cr, INK, 0.58);
		c8ui_text_draw(cr, family, small, q->video ? "A video" : "Pictures",
			x, y, w, 1);
		y += 20;
	}
	if (q->ext_title || q->ext_uri) {
		c8ui_set_source(cr, BLUE, 1);
		c8ui_text_draw(cr, family, small, q->ext_title ? q->ext_title :
			q->ext_uri, x, y, w, 1);
	}
}

static char *host_of(const char *uri) {
	if (g_str_has_prefix(uri, "at://")) {
		return g_strdup("Bluesky");
	}
	GUri *u = g_uri_parse(uri, G_URI_FLAGS_NONE, NULL);
	char *host = u && g_uri_get_host(u) ? g_strdup(g_uri_get_host(u)) :
		g_strdup(uri);
	if (u) {
		g_uri_unref(u);
	}
	return host;
}

static void draw_ext(cairo_t *cr, const char *family, struct sky_post *p,
		struct c8ui_rect r) {
	bool thumb = p->ext_thumb && sky_settings.images;
	box(cr, r);
	double x = r.x + QUOTE_PAD, w = r.width - 2 * QUOTE_PAD;
	if (thumb) {
		picture(cr, p->ext_thumb, (struct c8ui_rect){ r.x + 1, r.y + 1,
			EXT_THUMB - 1, r.height - 2 });
		x = r.x + EXT_THUMB + 10;
		w = r.x + r.width - QUOTE_PAD - x;
	}
	double y = r.y + QUOTE_PAD - 2;
	c8ui_set_source(cr, INK, 1);
	y += c8ui_text_draw(cr, family, small_strong, p->ext_title ? p->ext_title :
		p->ext_uri, x, y, w, 2) + 2;
	char *host = host_of(p->ext_uri);
	c8ui_set_source(cr, INK, 0.58);
	c8ui_text_draw(cr, family, small, host, x, y, w, 1);
	g_free(host);
}

void card_count(int count, char *text, size_t size) {
	if (count >= 10000000) {
		g_snprintf(text, size, "%.0fM", count / 1e6);
	} else if (count >= 1000000) {
		g_snprintf(text, size, "%.1fM", count / 1e6);
	} else if (count >= 10000) {
		g_snprintf(text, size, "%.0fK", count / 1000.0);
	} else if (count >= 1000) {
		g_snprintf(text, size, "%.1fK", count / 1000.0);
	} else {
		g_snprintf(text, size, "%d", count);
	}
}

static void action(cairo_t *cr, const char *family, const char *glyph,
		int count, uint32_t colour, bool done, double x, double y) {
	c8ui_set_source(cr, done ? colour : INK, done ? 1 : 0.55);
	card_glyph(cr, glyph, x + 6, y + 8, 14, done);
	if (count > 0) {
		char text[24];
		card_count(count, text, sizeof(text));
		c8ui_text_draw(cr, family, small, text, x + 27, y + 6, 0, 1);
	}
}

static void play_mark(cairo_t *cr, struct c8ui_rect r) {
	double cx = r.x + r.width / 2, cy = r.y + r.height / 2;
	cairo_set_source_rgba(cr, 0, 0, 0, 0.55);
	cairo_arc(cr, cx, cy, 26, 0, 2 * G_PI);
	cairo_fill(cr);
	cairo_set_source_rgb(cr, 1, 1, 1);
	cairo_move_to(cr, cx - 8, cy - 12);
	cairo_line_to(cr, cx + 13, cy);
	cairo_line_to(cr, cx - 8, cy + 12);
	cairo_close_path(cr);
	cairo_fill(cr);
}

static void draw_post(cairo_t *cr, const char *family, struct sky_item *it,
		struct layout *l) {
	struct sky_post *p = l->p;
	if (l->reason_y >= 0) {
		/* A notice that is a post says who did what; a feed, why it's here */
		char *reason = it->kind == SKY_ITEM_NOTICE ?
			g_strdup_printf("%s %s", sky_shown_name(it->did, it->name),
			it->reason) : sky_shown_reason(it);
		c8ui_set_source(cr, INK, 0.58);
		card_glyph(cr, it->reason_glyph ? it->reason_glyph : "pin",
			l->x + PAD + AVATAR - 14, l->reason_y + 4, 12, false);
		c8ui_text_draw(cr, family, small, reason, l->x + PAD + AVATAR + GUTTER,
			l->reason_y, l->w - 2 * PAD - AVATAR - GUTTER, 1);
		g_free(reason);
	}
	const char *name = sky_shown_name(p->did, p->name);
	const char *who = sky_shown_handle(p->did, p->handle);
	avatar(cr, family, sky_shown_avatar(p->did, p->avatar), name, l->avatar);
	if (l->focus) {
		double x = l->avatar.x + AVATAR + GUTTER, w = l->w - PAD - x + l->x;
		c8ui_set_source(cr, INK, 1);
		c8ui_text_draw(cr, family, strong, name, x, l->avatar.y, w, 1);
		char *handle = g_strconcat("@", who, NULL);
		c8ui_set_source(cr, INK, 0.58);
		c8ui_text_draw(cr, family, small, handle, x, l->avatar.y + 21, w, 1);
		g_free(handle);
	} else {
		name_line(cr, family, name, who, p->date, l->head.x,
			l->head.y - 2, l->head.width, strong);
	}
	if (l->text.height > 0) {
		c8ui_set_source(cr, INK, 1);
		c8ui_text_draw(cr, family, body_font(l->focus),
			sky_shown_text(p->text), l->text.x,
			l->text.y, l->text.width, l->focus ? 0 : LINES);
	}
	for (guint i = 0; l->links.height > 0 && i < p->links->len; i++) {
		struct sky_link *link = p->links->pdata[i];
		c8ui_set_source(cr, BLUE, 1);
		c8ui_text_draw(cr, family, small, link->kind == SKY_LINK_WEB ?
			link->target : sky_shown_text(link->label), l->links.x, l->links.y + i * LINK_H + 3,
			l->links.width, 1);
	}
	if (l->bar) {
		c8ui_set_source(cr, INK, 0.06);
		cairo_rectangle(cr, l->media.x, l->media.y, l->media.width,
			l->media.height);
		cairo_fill(cr);
		c8ui_set_source(cr, INK, 0.7);
		c8ui_text_draw(cr, family, small, l->bar_text, l->media.x + 10,
			l->media.y + 8, l->media.width - 20, 1);
	}
	for (int i = 0; i < l->cells; i++) {
		picture(cr, p->n_images ? p->images[i].thumb : p->video_thumb,
			l->cell[i]);
		if (p->n_images == 0) {
			play_mark(cr, l->cell[i]);
		}
	}
	if (l->ext.height > 0) {
		draw_ext(cr, family, p, l->ext);
	}
	if (l->quote.height > 0) {
		draw_quote(cr, family, p, l->quote);
	}
	if (l->meta.height > 0) {
		GDateTime *t = g_date_time_new_from_unix_local(p->date);
		char *when = t ? g_date_time_format(t, "%-e %B %Y, %H:%M") :
			g_strdup("");
		GString *meta = g_string_new(when);
		if (p->reposts) {
			g_string_append_printf(meta, "  ·  %d %s", p->reposts,
				p->reposts == 1 ? "repost" : "reposts");
		}
		if (p->quotes) {
			g_string_append_printf(meta, "  ·  %d %s", p->quotes,
				p->quotes == 1 ? "quote" : "quotes");
		}
		if (p->likes) {
			g_string_append_printf(meta, "  ·  %d %s", p->likes,
				p->likes == 1 ? "like" : "likes");
		}
		c8ui_set_source(cr, INK, 0.58);
		c8ui_text_draw(cr, family, small, meta->str, l->meta.x, l->meta.y,
			l->meta.width, 1);
		g_string_free(meta, TRUE);
		g_free(when);
		if (t) {
			g_date_time_unref(t);
		}
	}
	if (l->acts.height > 0) {
		double x = l->acts.x, y = l->acts.y;
		action(cr, family, "reply", p->replies, BLUE, false, x, y);
		action(cr, family, "repost", p->reposts, REPOSTED,
			p->repost_uri != NULL, x + ACT_W, y);
		action(cr, family, "like", p->likes, LIKED, p->like_uri != NULL,
			x + 2 * ACT_W, y);
	}
}

static void draw_plain(cairo_t *cr, const char *family, struct sky_item *it,
		struct layout *l) {
	const char *name = sky_shown_name(it->did, it->name);
	avatar(cr, family, sky_shown_avatar(it->did, it->avatar), name, l->avatar);
	double x = l->head.x, y = l->head.y - 2, w = l->head.width;
	if (it->kind == SKY_ITEM_ACTOR) {
		c8ui_set_source(cr, INK, 1);
		c8ui_text_draw(cr, family, strong, name, x, y, w, 1);
		char *handle = g_strdup_printf("@%s%s", sky_shown_handle(it->did,
			it->handle), it->follows_us ?
			"  ·  Follows you" : "");
		c8ui_set_source(cr, INK, 0.58);
		c8ui_text_draw(cr, family, small, handle, x, y + 21, w, 1);
		g_free(handle);
		if (l->follow.width > 0) {
			bool following = it->follow_uri != NULL;
			struct c8ui_rect f = l->follow;
			c8ui_set_source(cr, BLUE, 1);
			cairo_rectangle(cr, f.x + 0.5, f.y + 0.5, f.width - 1, f.height - 1);
			if (following) {
				cairo_set_line_width(cr, 1);
				cairo_stroke(cr);
			} else {
				cairo_fill(cr);
			}
			const char *label = following ? "Following" : "Follow";
			double tw = text_w(family, small_strong, label, f.width);
			c8ui_set_source(cr, following ? BLUE : 0xffffff, 1);
			c8ui_text_draw(cr, family, small_strong, label,
				f.x + (f.width - tw) / 2, f.y + 6, 0, 1);
		}
	} else {
		/* "Name liked your post", and when */
		char *age = sky_age(it->date);
		char *line = g_strdup_printf("%s %s%s%s", name, it->reason,
			age[0] ? "  ·  " : "", age);
		c8ui_set_source(cr, INK, 1);
		c8ui_text_draw(cr, family, small, line, x + 20, y + 2, w - 20, 2);
		c8ui_set_source(cr, strcmp(it->reason_glyph, "like") == 0 ? LIKED :
			strcmp(it->reason_glyph, "repost") == 0 ? REPOSTED : BLUE, 1);
		card_glyph(cr, it->reason_glyph, x, y + 5, 13,
			strcmp(it->reason_glyph, "like") == 0);
		g_free(line);
		g_free(age);
	}
	const char *words = sky_shown_text(it->kind == SKY_ITEM_NOTICE &&
		it->post ? it->post->text : it->text);
	if (l->text.height > 0) {
		c8ui_set_source(cr, INK, 0.62);
		c8ui_text_draw(cr, family, small, words, l->text.x, l->text.y,
			l->text.width, 3);
	}
}

/* A thread's lines. Left of a reply, a rail for each post it is under,
 * so a post's answers and theirs hang together down the column, and a
 * tick from the nearest rail to the card. Through the face, a line to
 * the post it answers above and to the one answering it below. */
static void draw_lines(cairo_t *cr, struct sky_item *it, struct layout *l,
		double height) {
	int depth = it->depth > INDENT_MAX ? INDENT_MAX : it->depth;
	c8ui_set_source(cr, INK, 0.28);
	cairo_set_line_width(cr, 2);
	for (int k = 0; k < depth; k++) {
		double x = k * INDENT + INDENT / 2;
		cairo_move_to(cr, x, 0);
		cairo_line_to(cr, x, height);
	}
	if (depth > 0) {
		double y = l->avatar.height > 0 ? floor(l->avatar.y +
			l->avatar.height / 2) + 1 : 21;
		cairo_move_to(cr, (depth - 1) * INDENT + INDENT / 2, y);
		cairo_line_to(cr, l->x, y);
	}
	cairo_stroke(cr);
	if (l->p == NULL || l->avatar.height <= 0) {
		return;
	}
	double cx = floor(l->avatar.x + l->avatar.width / 2) + 1;
	if (it->chain_up) {
		cairo_move_to(cr, cx, 0);
		cairo_line_to(cr, cx, l->avatar.y - 4);
	}
	if (it->chain_down) {
		cairo_move_to(cr, cx, l->avatar.y + l->avatar.height + 4);
		cairo_line_to(cr, cx, height);
	}
	cairo_stroke(cr);
}

bool card_draw(cairo_t *cr, const char *family, struct sky_item *it,
		struct c8ui_rect r, const struct c8ui_list_item *state) {
	struct layout l;
	lay(family, it, r.width, &l);
	bool was = sky_image_missed;
	sky_image_missed = false;
	cairo_save(cr);
	cairo_translate(cr, r.x, r.y);
	if (it->kind == SKY_ITEM_MORE || it->kind == SKY_ITEM_GAP) {
		c8ui_set_source(cr, INK, 0.55);
		const char *text = it->kind == SKY_ITEM_MORE ? "Loading more…" :
			it->text;
		c8ui_text_draw(cr, family, small, text, l.x + PAD, 12, l.w - 2 * PAD, 1);
		draw_lines(cr, it, &l, r.height);
		cairo_restore(cr);
		sky_image_missed = was;
		return true;
	}
	bool lit = state && (state->hovered || state->cursor);
	c8ui_set_source(cr, it->unread ? 0xeef6ff : lit ? CARD_HOVER : CARD, 1);
	cairo_rectangle(cr, l.x, 0, l.w, r.height);
	cairo_fill(cr);
	if (it->focus || it->unread) {
		c8ui_set_source(cr, BLUE, 1);
		cairo_rectangle(cr, l.x, 0, 3, r.height);
		cairo_fill(cr);
	}
	draw_lines(cr, it, &l, r.height);
	if (l.p) {
		draw_post(cr, family, it, &l);
	} else {
		draw_plain(cr, family, it, &l);
	}
	if (state && state->cursor) {
		c8ui_set_source(cr, INK, 0.5);
		cairo_set_line_width(cr, 1);
		static const double dash[] = { 1, 2 };
		cairo_set_dash(cr, dash, 2, 0);
		cairo_rectangle(cr, l.x + 1.5, 1.5, l.w - 3, r.height - 3);
		cairo_stroke(cr);
	}
	cairo_restore(cr);
	missed = sky_image_missed;
	sky_image_missed = was || missed;
	return !missed;
}

static bool in(const struct c8ui_rect *r, double x, double y) {
	return r->width > 0 && r->height > 0 && c8ui_rect_contains(r, x, y);
}

enum card_part card_hit(const char *family, struct sky_item *it,
		struct c8ui_rect r, double x, double y, int *sub) {
	struct layout l;
	lay(family, it, r.width, &l);
	x -= r.x;
	y -= r.y;
	*sub = 0;
	if (it->kind == SKY_ITEM_MORE || it->kind == SKY_ITEM_GAP) {
		return PART_NONE;
	}
	if (l.p == NULL) {
		if (in(&l.follow, x, y)) {
			return PART_FOLLOW;
		}
		if (in(&l.avatar, x, y) || in(&l.head, x, y)) {
			return PART_AUTHOR;
		}
		return PART_BODY;
	}
	if (in(&l.acts, x, y)) {
		int which = (int)((x - l.acts.x) / ACT_W);
		return which == 0 ? PART_REPLY : which == 1 ? PART_REPOST : PART_LIKE;
	}
	if (in(&l.avatar, x, y) || in(&l.head, x, y)) {
		return PART_AUTHOR;
	}
	if (in(&l.links, x, y)) {
		*sub = (int)((y - l.links.y) / LINK_H);
		return PART_LINK;
	}
	if (l.bar && in(&l.media, x, y)) {
		return l.p->n_images ? PART_MEDIA : PART_VIDEO;
	}
	for (int i = 0; i < l.cells; i++) {
		if (in(&l.cell[i], x, y)) {
			*sub = i;
			return l.p->n_images ? PART_MEDIA : PART_VIDEO;
		}
	}
	if (in(&l.ext, x, y)) {
		return PART_EXTERNAL;
	}
	if (in(&l.quote, x, y)) {
		return l.p->quote ? PART_QUOTE : PART_NONE;
	}
	return PART_BODY;
}
