/*
 * The settings layout, as PC settings has it: a dark list of pages down
 * the side and the chosen page beside it. Narrow, they take turns
 * filling the window like a phone app: the list, then a page over it
 * with a back button. The app builds a page's body; the title above it,
 * the paddings, the list as a menu and the way back are here, once.
 */
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include "c8ui-private.h"

/* The least a page needs beside the list: narrower than the list and
 * this, they take turns (PC settings' 340 list folds below 800) */
#define PAGES_PAGE_MIN 460

struct pages {
	c8ui_pages_fn build;
	void *data;
	c8ui_action_fn back;
	void *back_data;
	double pane_width;
	struct c8ui_widget *pane, *title, *nav, *scroll, *content;
	char **names;
	int count, current;
	char *sub; // a page over the chosen one, by its title
	bool narrow, in_page; // narrow: the page shows, not the list
	bool sized; // laid out before: a flip, not a window opening narrow
	struct c8ui_widget *back_button; // the page's, shown narrow or sub
};

static void pages_measure(struct c8ui_widget *w, double avail, double *width,
		double *height) {
	c8ui_widget_measure(w->children[0], avail, width, height);
}

static void pages_set(struct c8ui_widget *w, bool narrow);

/* Whether the list and the page take turns: a narrow window, or one
 * too small for both */
static bool pages_folds(struct c8ui_widget *w) {
	struct pages *p = w->data;
	return c8ui_widget_narrow(w) || (w->root &&
		w->root->width < p->pane_width + PAGES_PAGE_MIN);
}

static void pages_layout(struct c8ui_widget *w) {
	struct pages *p = w->data;
	/* Between the fold and narrow the window says nothing: see here */
	if (pages_folds(w) != p->narrow) {
		pages_set(w, !p->narrow);
	}
	p->sized = true;
	c8ui_widget_layout(w->children[0], w->rect);
}

static void pages_narrow(struct c8ui_widget *w, bool narrow) {
	(void)narrow;
	pages_set(w, pages_folds(w));
}

static void pages_destroy(struct c8ui_widget *w) {
	struct pages *p = w->data;
	for (int i = 0; i < p->count; i++) {
		free(p->names[i]);
	}
	free(p->names);
	free(p->sub);
	free(p);
}

static const struct c8ui_widget_impl pages_impl = {
	.name = "pages",
	.measure = pages_measure,
	.layout = pages_layout,
	.destroy = pages_destroy,
	.narrow = pages_narrow,
};

/* Which of the two panes show, and the list's shape */
static void arrange(struct c8ui_widget *w) {
	struct pages *p = w->data;
	bool list = p->narrow && !p->in_page;
	p->pane->fixed_width = p->narrow ? 0 : p->pane_width;
	p->pane->expand = p->narrow;
	c8ui_box_set_padding(p->pane, 0, p->narrow ? 24 : 40, 0, 20);
	c8ui_box_set_padding(p->title, p->narrow ? 20 : 40, 0, 20,
		p->narrow ? 0 : 12);
	c8ui_nav_set_inset(p->nav, p->narrow ? 20 : 40);
	/* A menu when narrow: a page opens only when asked for, not as the
	 * arrows go by */
	c8ui_nav_set_menu(p->nav, p->narrow);
	c8ui_nav_set(p->nav, p->current);
	c8ui_widget_set_hidden(p->pane, p->narrow && p->in_page);
	c8ui_widget_set_hidden(p->scroll, list);
	if (p->back_button) {
		c8ui_widget_set_hidden(p->back_button, !p->sub && !p->narrow);
	}
	if (list) {
		c8ui_widget_grab_focus(p->nav);
	}
	c8ui_widget_queue_layout(w);
}

static void back_pressed(struct c8ui_widget *button, void *data) {
	c8ui_pages_back(data);
}

void c8ui_pages_rebuild(struct c8ui_widget *w, bool enter) {
	struct pages *p = w->data;
	if (p->narrow && !p->in_page) {
		return; // the list shows: built when a page is opened
	}
	if (p->content) {
		c8ui_widget_destroy(p->content);
	}
	/* One page for both widths: what differs goes with the window */
	p->content = c8ui_box_new(C8UI_VERTICAL, 18);
	const char *text = p->sub ? p->sub :
		p->current < p->count ? p->names[p->current] : "";
	c8ui_box_set_padding(p->content, 80, 40, 40, 60);
	c8ui_box_set_narrow_padding(p->content, 20, 24, 20, 40);
	p->content->max_width = 760;
	/* A back button before the title: to the page under this one, or
	 * (narrow) to the list, with a smaller title to leave room */
	struct c8ui_widget *top = c8ui_box_new(C8UI_HORIZONTAL, 16);
	struct c8ui_widget *back = c8ui_back_button_new(back_pressed, w);
	back->align = C8UI_ALIGN_CENTER;
	c8ui_widget_set_hidden(back, !p->sub && !p->narrow);
	c8ui_widget_track(back, &p->back_button);
	c8ui_widget_add(top, back);
	struct c8ui_widget *title = c8ui_label_new(text, C8UI_TEXT_HEADER);
	c8ui_label_set_narrow_style(title, C8UI_TEXT_SUBHEADER);
	c8ui_label_set_max_lines(title, 1);
	title->align = C8UI_ALIGN_CENTER;
	title->expand = true;
	c8ui_widget_add(top, title);
	c8ui_widget_add(p->content, top);
	if (p->build) {
		p->build(w, p->content, p->data);
	}
	c8ui_widget_add(p->scroll, p->content);
	if (enter) {
		c8ui_scroll_to(p->scroll, 0);
		c8ui_widget_enter(p->content);
	}
}

static void nav_changed(struct c8ui_widget *nav, void *data) {
	if (c8ui_nav_get(nav) >= 0) {
		c8ui_pages_open(data, c8ui_nav_get(nav));
	}
}

struct c8ui_widget *c8ui_pages_new(const char *title, double pane_width,
		c8ui_pages_fn build, void *data) {
	struct pages *p = calloc(1, sizeof(*p));
	p->build = build;
	p->data = data;
	p->pane_width = pane_width;
	struct c8ui_widget *w = c8ui_widget_new(&pages_impl, p);

	struct c8ui_widget *top = c8ui_box_new(C8UI_HORIZONTAL, 0);
	p->pane = c8ui_box_new(C8UI_VERTICAL, 24);
	p->pane->fill = true;
	c8ui_widget_set_scheme(p->pane, C8UI_SCHEME_DARK);
	p->title = c8ui_box_new(C8UI_VERTICAL, 0);
	c8ui_widget_add(p->title, c8ui_label_new(title, C8UI_TEXT_HEADER));
	c8ui_widget_add(p->pane, p->title);
	p->nav = c8ui_nav_new(nav_changed, w);
	/* In a scroll: narrow and short, the list can be longer than the
	 * window */
	struct c8ui_widget *list = c8ui_scroll_new(p->nav);
	list->expand = true;
	list->fill = true;
	c8ui_widget_add(p->pane, list);
	c8ui_widget_add(top, p->pane);

	p->scroll = c8ui_scroll_new(NULL);
	p->scroll->expand = true;
	p->scroll->fill = true;
	c8ui_widget_set_scheme(p->scroll, C8UI_SCHEME_LIGHT);
	c8ui_widget_set_background(p->scroll, 0xffffff);
	c8ui_widget_add(top, p->scroll);

	c8ui_widget_add(w, top);
	arrange(w);
	return w;
}

void c8ui_pages_add(struct c8ui_widget *w, const char *name) {
	struct pages *p = w->data;
	p->names = realloc(p->names, (p->count + 1) * sizeof(char *));
	p->names[p->count++] = strdup(name);
	c8ui_nav_add(p->nav, name);
	c8ui_nav_set(p->nav, p->current);
}

int c8ui_pages_get(struct c8ui_widget *w) {
	return ((struct pages *)w->data)->current;
}

void c8ui_pages_open(struct c8ui_widget *w, int page) {
	struct pages *p = w->data;
	if (page < 0 || page >= p->count) {
		return;
	}
	p->current = page;
	p->in_page = true;
	g_clear_pointer(&p->sub, free);
	arrange(w);
	c8ui_pages_rebuild(w, true);
}

void c8ui_pages_set_sub(struct c8ui_widget *w, const char *title) {
	struct pages *p = w->data;
	free(p->sub);
	p->sub = c8ui_dup_or_null(title);
	if (p->sub) {
		p->in_page = true;
		arrange(w);
	}
}

const char *c8ui_pages_sub(struct c8ui_widget *w) {
	return ((struct pages *)w->data)->sub;
}

void c8ui_pages_on_back(struct c8ui_widget *w, c8ui_action_fn fn,
		void *data) {
	struct pages *p = w->data;
	p->back = fn;
	p->back_data = data;
}

bool c8ui_pages_back(struct c8ui_widget *w) {
	struct pages *p = w->data;
	if (p->sub) {
		g_clear_pointer(&p->sub, free);
		if (p->back) {
			p->back(w, p->back_data);
		}
		c8ui_pages_rebuild(w, true);
		return true;
	}
	if (p->narrow && p->in_page) {
		p->in_page = false;
		/* Gone, not hidden: a page may be doing things while it shows
		 * (looking for devices), and it's built anew when opened */
		g_clear_pointer(&p->content, c8ui_widget_destroy);
		arrange(w);
		c8ui_widget_enter(p->pane);
		return true;
	}
	return false;
}

/* One at a time or side by side: the window crossed the line, or the
 * pages came into one */
static void pages_set(struct c8ui_widget *w, bool narrow) {
	struct pages *p = w->data;
	if (narrow == p->narrow && p->content) {
		return;
	}
	/* Snapped from a full window, the page that was showing stays; a
	 * window opening narrow starts on the list unless a page was asked
	 * for (c8ui_pages_open) */
	if (narrow && p->sized && narrow != p->narrow) {
		p->in_page = true;
	}
	p->narrow = narrow;
	arrange(w);
	/* The page itself follows the width; it is only built when there
	 * is none (widened from the list) */
	if (!p->content && (!narrow || p->in_page)) {
		c8ui_pages_rebuild(w, false);
	}
}

bool c8ui_pages_narrow(struct c8ui_widget *w) {
	return ((struct pages *)w->data)->narrow;
}

bool c8ui_pages_showing(struct c8ui_widget *w) {
	struct pages *p = w->data;
	return !p->narrow || p->in_page;
}

struct c8ui_widget *c8ui_pages_pane(struct c8ui_widget *w) {
	return ((struct pages *)w->data)->pane;
}
