/*
 * Stealth mode (config.ini's `[stealth] on`): what is drawn of the
 * accounts signed in here is the persona's instead (c8-base's
 * stealth.h). Only the showing changes: the structs keep the real DIDs
 * and handles, which every call goes on using. Other people stay as
 * they are.
 */
#include <string.h>
#include "stealth.h"
#include "sky.h"

bool sky_mine(const char *did) {
	return did && did[0] && sky_accounts && c8_stealth() &&
		sky_account_find(did) != NULL;
}

const char *sky_shown_name(const char *did, const char *name) {
	return sky_mine(did) ? c8_stealth_name() : name;
}

const char *sky_shown_handle(const char *did, const char *handle) {
	return sky_mine(did) ? c8_stealth_handle() : handle;
}

const char *sky_shown_avatar(const char *did, const char *url) {
	return sky_mine(did) ? NULL : url;
}

static bool handle_char(char c) {
	return g_ascii_isalnum(c) || c == '-' || c == '.';
}

/* Where "@handle" is in text as a whole mention, from at; NULL: nowhere */
static const char *mention_at(const char *text, const char *handle) {
	size_t n = strlen(handle);
	for (const char *at = strchr(text, '@'); at; at = strchr(at + 1, '@')) {
		if (g_ascii_strncasecmp(at + 1, handle, n) != 0) {
			continue;
		}
		const char *end = at + 1 + n;
		/* A full stop after it ends the sentence, not the handle */
		if (!handle_char(*end) || (*end == '.' && !handle_char(end[1]))) {
			return at;
		}
	}
	return NULL;
}

/* Texts that needed changing, the same one each time it's asked: few
 * (only what mentions the user), so they're kept */
static GHashTable *texts;
static char *texts_for; // the persona's handle they were made with

const char *sky_shown_text(const char *text) {
	if (text == NULL || !c8_stealth() || sky_accounts == NULL) {
		return text;
	}
	bool any = false;
	for (guint i = 0; i < sky_accounts->len && !any; i++) {
		struct sky_account *a = sky_accounts->pdata[i];
		any = a->handle && a->handle[0] && mention_at(text, a->handle);
	}
	if (!any) {
		return text;
	}
	const char *persona = c8_stealth_handle();
	if (texts == NULL || g_strcmp0(texts_for, persona) != 0) {
		g_clear_pointer(&texts, g_hash_table_unref);
		texts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
		g_free(texts_for);
		texts_for = g_strdup(persona);
	}
	const char *kept = g_hash_table_lookup(texts, text);
	if (kept) {
		return kept;
	}
	char *out = g_strdup(text);
	for (guint i = 0; i < sky_accounts->len; i++) {
		struct sky_account *a = sky_accounts->pdata[i];
		if (a->handle == NULL || a->handle[0] == '\0') {
			continue;
		}
		GString *s = g_string_new(NULL);
		const char *from = out, *at;
		while ((at = mention_at(from, a->handle))) {
			g_string_append_len(s, from, at - from);
			g_string_append_c(s, '@');
			g_string_append(s, persona);
			from = at + 1 + strlen(a->handle);
		}
		g_string_append(s, from);
		g_free(out);
		out = g_string_free(s, FALSE);
	}
	g_hash_table_insert(texts, g_strdup(text), out);
	return out;
}

char *sky_shown_reason(const struct sky_item *it) {
	if (it->reason == NULL) {
		return NULL;
	}
	if (it->by_name && sky_mine(it->by_did) &&
			g_str_has_suffix(it->reason, it->by_name)) {
		size_t keep = strlen(it->reason) - strlen(it->by_name);
		return g_strdup_printf("%.*s%s", (int)keep, it->reason,
			c8_stealth_name());
	}
	return g_strdup(it->reason);
}
