// Copyright (c) 2015, JINGROW and Contributors
// MIT License. See license.txt

jingrow.provide("jingrow.pages");
jingrow.provide("jingrow.views");

jingrow.views.Factory = class Factory {
	constructor(opts) {
		$.extend(this, opts);
	}

	show() {
		this.route = jingrow.get_route();
		this.page_name = jingrow.get_route_str();

		if (this.before_show && this.before_show() === false) return;

		if (jingrow.pages[this.page_name]) {
			jingrow.container.change_to(this.page_name);
			if (this.on_show) {
				this.on_show();
			}
		} else {
			if (this.route[1]) {
				this.make(this.route);
			} else {
				jingrow.show_not_found(this.route);
			}
		}
	}

	make_page(double_column, page_name, hide_sidebar) {
		return jingrow.make_page(double_column, page_name, hide_sidebar);
	}
};

jingrow.make_page = function (double_column, page_name, disable_sidebar_toggle) {
	if (!page_name) {
		page_name = jingrow.get_route_str();
	}

	const page = jingrow.container.add_page(page_name);

	jingrow.ui.make_app_page({
		parent: page,
		single_column: !double_column,
		disable_sidebar_toggle: disable_sidebar_toggle,
	});

	jingrow.container.change_to(page_name);
	return page;
};
