/*
 * Open with: "How do you want to open this file?" in a flyout over the
 * item. The apps for its type come first (the default as "Keep using"),
 * More apps lists the rest; the box makes the choice the default.
 */
#include <string.h>
#include "files.h"

#define LIST_H 300

struct open_with {
	struct c8ui_window *win;
	struct c8ui_widget *flyout, *nav, *scroll, *more, *always;
	GPtrArray *apps; // GAppInfo, as in nav
	char *mime;
	char **uris;
};

static void add_app(struct open_with *o, GAppInfo *app, const char *label) {
	for (guint i = 0; i < o->apps->len; i++) {
		if (g_app_info_equal(o->apps->pdata[i], app)) {
			return;
		}
	}
	g_ptr_array_add(o->apps, g_object_ref(app));
	c8ui_nav_add(o->nav, label ? label : g_app_info_get_display_name(app));
}

static void add_list(struct open_with *o, GList *apps) {
	for (GList *l = apps; l; l = l->next) {
		if (g_app_info_should_show(l->data)) {
			add_app(o, l->data, NULL);
		}
	}
	g_list_free_full(apps, g_object_unref);
}

static int by_name(gconstpointer a, gconstpointer b) {
	return g_utf8_collate(g_app_info_get_display_name((GAppInfo *)a),
		g_app_info_get_display_name((GAppInfo *)b));
}

static void chosen(struct c8ui_widget *nav, void *data) {
	struct open_with *o = data;
	int i = c8ui_nav_get(nav);
	if (i < 0 || i >= (int)o->apps->len) {
		return;
	}
	GAppInfo *app = o->apps->pdata[i];
	if (c8ui_checkbox_get(o->always)) {
		GError *error = NULL;
		if (!g_app_info_set_as_default_for_type(app, o->mime, &error)) {
			g_warning("Can't make %s the default: %s",
				g_app_info_get_id(app), error->message);
			g_error_free(error);
		}
	}
	const char *id = g_app_info_get_id(app);
	if (id) {
		c8ui_window_launch(o->win, id, (const char *const *)o->uris);
	}
	c8ui_flyout_close(o->flyout); // closed: freed
}

static void more(struct c8ui_widget *link, void *data) {
	struct open_with *o = data;
	link->hidden = true;
	GList *all = g_list_sort(g_app_info_get_all(), by_name);
	add_list(o, all);
	c8ui_widget_queue_layout(o->nav);
}

static void closed(void *data) {
	struct open_with *o = data;
	g_ptr_array_unref(o->apps);
	g_free(o->mime);
	g_strfreev(o->uris);
	free(o);
}

void open_with_show(struct c8ui_window *win, struct c8ui_root *root,
		const struct c8ui_rect *anchor, char **uris, const char *mime) {
	struct open_with *o = calloc(1, sizeof(*o));
	o->win = win;
	o->mime = g_strdup(mime);
	o->uris = g_strdupv(uris);
	o->apps = g_ptr_array_new_with_free_func(g_object_unref);

	struct c8ui_widget *box = c8ui_box_new(C8UI_VERTICAL, 12);
	box->fixed_width = 360;
	bool several = uris[0] && uris[1];
	c8ui_widget_add(box, c8ui_label_new(several ?
		"How do you want to open these files?" :
		"How do you want to open this file?", C8UI_TEXT_TITLE));

	o->nav = c8ui_nav_new(chosen, o);
	GAppInfo *def = g_app_info_get_default_for_type(mime, FALSE);
	if (def) {
		char *keep = g_strdup_printf("Keep using %s",
			g_app_info_get_display_name(def));
		add_app(o, def, keep);
		g_free(keep);
		g_object_unref(def);
	}
	add_list(o, g_app_info_get_recommended_for_type(mime));
	add_list(o, g_app_info_get_fallback_for_type(mime));
	c8ui_nav_set(o->nav, -1);
	o->scroll = c8ui_scroll_new(o->nav);
	o->scroll->fixed_height = LIST_H;
	o->scroll->fill = true;
	c8ui_widget_add(box, o->scroll);

	o->more = c8ui_link_new("More apps", more, o);
	c8ui_widget_add(box, o->more);
	char *desc = g_content_type_get_description(mime);
	char *always = g_strdup_printf("Use this app for all %s files", desc);
	o->always = c8ui_checkbox_new(always, false, NULL, NULL);
	c8ui_widget_add(box, o->always);
	g_free(always);
	g_free(desc);
	if (o->apps->len == 0) {
		more(o->more, o); // nothing says it can: offer everything
	}
	o->flyout = c8ui_flyout_show(root, anchor, box, closed, o);
}
