// Copyright (c) 2015, XhiveFramework Technologies Pvt. Ltd. and Contributors
// MIT License. See license.txt

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

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

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

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

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

	make_page(double_column, page_name) {
		return xhiveframework.make_page(double_column, page_name);
	}
};

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

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

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

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