/*
 * c8-template: the start of a c8ui app. Copy this folder, rename the app
 * id (APP_ID here, in meson.build and the data/ file names) and build on
 * it. What it shows is what every app gets for free:
 *
 * - one copy runs: launching it again (from Start, from a file opened
 *   with it) hands the files to this one, whose window comes forward
 * - files to open as arguments, and its .desktop file's MimeType= so
 *   other apps open those types with it
 * - Open file… through the file chooser portal, over this window
 * - thumbnails from the shared cache, made in a sandbox
 * - the Search and Settings charms
 * - opening a file in the app that handles it
 * - a narrow layout: snapped to a quarter of the screen it must still be
 *   fully usable, as one column like a phone app (see resized)
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <c8ui/c8ui.h>
#include <c8ui/c8ui-charms.h>
#include <c8ui/c8ui-files.h>
#include <c8ui/c8ui-wayland.h>

#define APP_ID "org.cont8nuum.Template"

static struct {
	struct c8ui_app *app;
	struct c8ui_window *win;
	struct c8ui_widget *page, *files, *empty;
	struct c8ui_charms *charms;
	GPtrArray *uris; // what is shown, for search
	bool big; // Settings charm: thumbnail size
} g;

/* A row per file */

struct row {
	char *uri;
	struct c8ui_widget *image;
	struct c8ui_thumbnail *thumb; // until it comes
};

static void got_thumbnail(cairo_surface_t *thumb, void *data) {
	struct row *row = data;
	row->thumb = NULL;
	if (thumb) {
		c8ui_image_set(row->image, thumb);
		cairo_surface_destroy(thumb);
	}
}

static void row_gone(struct c8ui_widget *w, void *data) {
	struct row *row = data;
	if (row->thumb) {
		c8ui_thumbnail_cancel(row->thumb);
	}
	g_free(row->uri);
	free(row);
}

static void open_row(struct c8ui_widget *w, void *data) {
	struct row *row = data;
	c8ui_window_open_uri(g.win, row->uri);
}

static void add_file(const char *uri) {
	struct row *row = calloc(1, sizeof(*row));
	row->uri = g_strdup(uri);
	double px = g.big ? 160 : 64;

	struct c8ui_widget *box = c8ui_box_new(C8UI_HORIZONTAL, 16);
	box->user_data = row;
	box->on_destroy = row_gone;
	row->image = c8ui_image_new(NULL, px, px);
	c8ui_widget_add(box, row->image);

	struct c8ui_widget *text = c8ui_box_new(C8UI_VERTICAL, 2);
	text->align = C8UI_ALIGN_CENTER;
	char *path = g_filename_from_uri(uri, NULL, NULL);
	char *name = path ? g_path_get_basename(path) : g_strdup(uri);
	char *dir = path ? g_path_get_dirname(path) : NULL;
	c8ui_widget_add(text, c8ui_label_new(name, C8UI_TEXT_ITEM));
	if (dir) {
		struct c8ui_widget *where = c8ui_label_new(dir, C8UI_TEXT_CAPTION);
		c8ui_label_set_color(where, C8UI_COLOR_SECONDARY);
		c8ui_widget_add(text, where);
	}
	c8ui_widget_add(text, c8ui_link_new("Open", open_row, row));
	c8ui_widget_add(box, text);
	c8ui_widget_add(g.files, box);
	g_free(dir);
	g_free(name);
	g_free(path);

	g_ptr_array_add(g.uris, g_strdup(uri));
	g.empty->hidden = true;
	row->thumb = c8ui_thumbnail_load(uri, g.big ? C8UI_THUMBNAIL_LARGE :
		C8UI_THUMBNAIL_NORMAL, got_thumbnail, row);
}

static void picked(char **uris, void *data) {
	for (int i = 0; uris && uris[i]; i++) {
		add_file(uris[i]);
	}
}

static void pick(struct c8ui_widget *w, void *data) {
	static const struct c8ui_file_filter filters[] = {
		{ "All files", "*", NULL },
		{ "Pictures", NULL, "image/png;image/jpeg;image/webp;image/gif" },
		{ "Text", NULL, "text/plain" },
	};
	c8ui_file_open(g.win, &(struct c8ui_file_options){
		.title = "Open files",
		.multiple = true,
		.filters = filters,
		.filter_count = 3,
	}, picked, NULL);
}

static void window_gone(struct c8ui_widget *w, void *data) {
	g.win = NULL;
	g.page = g.files = g.empty = NULL;
}

static void make_window(void) {
	g.win = c8ui_window_new(g.app, "Template", 800, 600);
	struct c8ui_widget *page = c8ui_box_new(C8UI_VERTICAL, 24);
	page->fill = true;
	page->on_destroy = window_gone;
	/* Every app works snapped to a quarter of the screen. Build a page
	 * once and say how it differs narrow: here, smaller margins. Panes
	 * side by side go one at a time by themselves in c8ui_pages, a split
	 * stacks, a hub becomes a pivot or a column; a box turns with
	 * c8ui_box_set_narrow_orientation. Never build again on the flip;
	 * c8ui_root_on_resize is only for sizes (a list's cells). */
	c8ui_box_set_padding(page, 120, 44, 40, 40);
	c8ui_box_set_narrow_padding(page, 20, 24, 20, 20);
	g.page = page;
	c8ui_widget_add(page, c8ui_label_new("Template", C8UI_TEXT_HEADER));
	struct c8ui_widget *button = c8ui_button_new("Open files", pick, NULL);
	c8ui_widget_add(page, button);
	g.empty = c8ui_label_new("Files you open show up here. Open some from "
		"here, or open this app with files from Files or a terminal.",
		C8UI_TEXT_BODY);
	c8ui_label_set_color(g.empty, C8UI_COLOR_SECONDARY);
	c8ui_widget_add(page, g.empty);
	g.files = c8ui_box_new(C8UI_VERTICAL, 16);
	g.files->fill = true;
	struct c8ui_widget *scroll = c8ui_scroll_new(g.files);
	scroll->expand = true;
	scroll->fill = true;
	c8ui_widget_add(page, scroll);
	struct c8ui_root *root = c8ui_window_root(g.win);
	c8ui_root_set_child(root, page);
}

/* Charms */

static void search(struct c8ui_search *s, const char *query, void *data) {
	for (guint i = 0; query[0] && i < g.uris->len; i++) {
		char *name = g_path_get_basename(g.uris->pdata[i]);
		char *unescaped = g_uri_unescape_string(name, NULL);
		if (unescaped && strcasestr(unescaped, query)) {
			char *id = g_strdup_printf("%u", i);
			c8ui_search_add(s, id, unescaped, NULL, "text-x-generic");
			g_free(id);
		}
		g_free(unescaped);
		g_free(name);
	}
}

static void result(const char *id, const char *query, void *data) {
	guint i = atoi(id);
	if (g.win && i < g.uris->len) {
		c8ui_window_open_uri(g.win, g.uris->pdata[i]);
	}
}

static void big_changed(struct c8ui_setting *s, void *data) {
	g.big = c8ui_setting_get_bool(s);
}

static void options(struct c8ui_settings_page *page, void *data) {
	struct c8ui_setting *s = c8ui_page_toggle(page, "big", "Big thumbnails",
		g.big, big_changed, NULL);
	c8ui_setting_set_description(s, "For files opened from now on");
}

/* The app */

static void startup(struct c8ui_app *app, void *data) {
	g.app = app;
	g.uris = g_ptr_array_new_with_free_func(g_free);
	g.charms = c8ui_charms_new(app);
	if (g.charms) {
		c8ui_charms_on_search(g.charms, search, result, NULL, NULL);
		c8ui_charms_add_page(g.charms, "options", "Options", options, NULL);
	}
}

/* Every launch: the first, and each one handed over by a later copy */
static void open(struct c8ui_app *app, char **uris, void *data) {
	if (g.win == NULL) {
		make_window();
	}
	for (int i = 0; uris[i]; i++) {
		add_file(uris[i]);
	}
	c8ui_window_present(g.win);
}

static void shutdown(struct c8ui_app *app, void *data) {
	c8ui_charms_destroy(g.charms);
	g_ptr_array_unref(g.uris);
}

int main(int argc, char **argv) {
	static const struct c8ui_app_info info = {
		.id = APP_ID,
		.startup = startup,
		.open = open,
		.shutdown = shutdown,
	};
	return c8ui_app_main(&info, argc, argv);
}
