/*
 * c8-charms-demo: a tiny "news" app that brings its own Search and
 * Settings charms, through c8ui's charms helper (c8ui-charms.h).
 *
 * Its app id, org.cont8nuum.NewsDemo, is also its bus name, which is how
 * the shell finds it. Search finds headlines; Settings has an Options
 * page and an About action.
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <glib.h>
#include <c8ui/c8ui.h>
#include <c8ui/c8ui-wayland.h>
#include <c8ui/c8ui-charms.h>

#define APP_ID "org.cont8nuum.NewsDemo"

static const struct {
	const char *id, *headline, *section;
} articles[] = {
	{ "tram", "City votes to bring back the tram", "Local" },
	{ "rain", "Wettest October in forty years", "Weather" },
	{ "kernel", "New kernel brings faster suspend", "Technology" },
	{ "cup", "Underdogs through to the cup final", "Sport" },
	{ "library", "Library opens on Sundays from next month", "Local" },
	{ "wayland", "Desktops finish the move to Wayland", "Technology" },
	{ "frost", "First frost expected this weekend", "Weather" },
};
#define COUNT G_N_ELEMENTS(articles)

static const char *const sizes[] = { "Small", "Medium", "Large" };

static struct {
	struct c8ui_charms *charms;
	struct c8ui_widget *heading, *text;
	char *recent[3];
	/* Options */
	bool images, local_only;
	int size;
	double per_page;
	char *town;
} demo = { .images = true, .size = 1, .per_page = 10 };

static void show(const char *heading, const char *text) {
	c8ui_label_set_text(demo.heading, heading);
	c8ui_label_set_text(demo.text, text);
}

static void remember(const char *query) {
	if (query == NULL || query[0] == '\0') {
		return;
	}
	g_free(demo.recent[2]);
	memmove(&demo.recent[1], &demo.recent[0], 2 * sizeof(char *));
	demo.recent[0] = g_strdup(query);
}

static bool matches(size_t i, const char *query) {
	if (demo.local_only && strcmp(articles[i].section, "Local") != 0) {
		return false;
	}
	char *h = g_utf8_casefold(articles[i].headline, -1);
	char *q = g_utf8_casefold(query, -1);
	bool hit = strstr(h, q) != NULL;
	g_free(h);
	g_free(q);
	return hit;
}

/* Search */

static void search(struct c8ui_search *s, const char *query, void *data) {
	if (query[0] == '\0') {
		/* Nothing typed yet: recent searches */
		for (int i = 0; i < 3 && demo.recent[i]; i++) {
			char *id = g_strconcat("recent:", demo.recent[i], NULL);
			c8ui_search_add(s, id, demo.recent[i], "Recent search",
				"document-open-recent");
			g_free(id);
		}
		return;
	}
	for (size_t i = 0; i < COUNT; i++) {
		if (matches(i, query)) {
			c8ui_search_add(s, articles[i].id, articles[i].headline,
				articles[i].section, "text-x-generic");
		}
	}
}

static void submit(const char *query, void *data) {
	/* Which copy got it, when several are open */
	printf("%d: search for %s\n", (int)getpid(), query);
	fflush(stdout);
	remember(query);
	GString *s = g_string_new(NULL);
	for (size_t i = 0; i < COUNT; i++) {
		if (matches(i, query)) {
			g_string_append_printf(s, "%s\n", articles[i].headline);
		}
	}
	char *heading = g_strdup_printf("Results for \"%s\"", query);
	show(heading, s->len ? s->str : "Nothing found.");
	g_free(heading);
	g_string_free(s, TRUE);
}

static void open_result(const char *id, const char *query, void *data) {
	if (g_str_has_prefix(id, "recent:")) {
		submit(id + strlen("recent:"), NULL);
		return;
	}
	remember(query);
	for (size_t i = 0; i < COUNT; i++) {
		if (strcmp(articles[i].id, id) == 0) {
			show(articles[i].headline, articles[i].section);
		}
	}
}

/* Settings */

static void changed(struct c8ui_setting *s, void *data) {
	const char *id = c8ui_setting_id(s);
	if (strcmp(id, "images") == 0) {
		demo.images = c8ui_setting_get_bool(s);
	} else if (strcmp(id, "local") == 0) {
		demo.local_only = c8ui_setting_get_bool(s);
	} else if (strcmp(id, "size") == 0) {
		demo.size = c8ui_setting_get_int(s);
		/* Many stories per page only with small text */
		c8ui_charms_changed(demo.charms, "options");
	} else if (strcmp(id, "per_page") == 0) {
		demo.per_page = c8ui_setting_get_double(s);
	} else if (strcmp(id, "town") == 0) {
		g_free(demo.town);
		demo.town = g_strdup(c8ui_setting_get_text(s));
	} else if (strcmp(id, "reset") == 0) {
		demo.images = true;
		demo.local_only = false;
		demo.size = 1;
		demo.per_page = 10;
		g_clear_pointer(&demo.town, g_free);
		c8ui_charms_changed(demo.charms, "options");
	}
	printf("set %s\n", id);
	fflush(stdout);
}

static void options(struct c8ui_settings_page *page, void *data) {
	c8ui_page_heading(page, "Reading");
	c8ui_page_toggle(page, "images", "Show pictures", demo.images, changed,
		NULL);
	c8ui_page_choice(page, "size", "Text size", sizes, G_N_ELEMENTS(sizes),
		demo.size, changed, NULL);
	struct c8ui_setting *per = c8ui_page_slider(page, "per_page",
		"Stories per page", 5, 50, 5, demo.per_page, changed, NULL);
	c8ui_setting_set_enabled(per, demo.size == 0);
	c8ui_page_heading(page, "Local news");
	c8ui_page_toggle(page, "local", "Search local news only",
		demo.local_only, changed, NULL);
	struct c8ui_setting *town = c8ui_page_text(page, "town", "Home town",
		demo.town, changed, NULL);
	c8ui_setting_set_placeholder(town, "Where you live");
	c8ui_page_paragraph(page, "Stories from your home town show first.");
	c8ui_page_button(page, "reset", "Reset", changed, NULL);
}

static void about(void *data) {
	show("About", "c8-charms-demo shows how an app brings its own Search "
		"and Settings charms.");
}

int main(void) {
	struct c8ui_app *app = c8ui_app_new(APP_ID);
	if (app == NULL) {
		fprintf(stderr, "c8-charms-demo: no Wayland compositor\n");
		return 1;
	}
	demo.charms = c8ui_charms_new(app);
	c8ui_charms_on_search(demo.charms, search, open_result, submit, NULL);
	c8ui_charms_add_page(demo.charms, "options", "Options", options, NULL);
	c8ui_charms_add_action(demo.charms, "about", "About", about, NULL);

	struct c8ui_window *win = c8ui_window_new(app, "News demo", 800, 560);
	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 16);
	c8ui_box_set_padding(box, 60, 48, 60, 48);
	demo.heading = c8ui_label_new("News demo", C8UI_TEXT_HEADER);
	demo.text = c8ui_label_new("Open the charms: Search finds headlines "
		"here, and Settings has this app's options.", C8UI_TEXT_ITEM);
	c8ui_widget_add(box, demo.heading);
	c8ui_widget_add(box, demo.text);
	c8ui_root_set_child(c8ui_window_root(win), box);
	int ret = c8ui_app_run(app);
	c8ui_charms_destroy(demo.charms);
	c8ui_app_destroy(app);
	return ret;
}
