// Copyright (c) 2015, InfluxFramework LLC
// MIT License. See license.txt

influxframework.provide("influxframework.views.formview");

influxframework.views.FormFactory = class FormFactory extends influxframework.views.Factory {
	make(route) {
		var doctype = route[1],
			doctype_layout = influxframework.router.doctype_layout || doctype;

		if (!influxframework.views.formview[doctype_layout]) {
			influxframework.model.with_doctype(doctype, () => {
				this.page = influxframework.container.add_page(doctype_layout);
				influxframework.views.formview[doctype_layout] = this.page;
				this.make_and_show(doctype, route);
			});
		} else {
			this.show_doc(route);
		}

		this.setup_events();
	}

	make_and_show(doctype, route) {
		if (influxframework.router.doctype_layout) {
			influxframework.model.with_doc("DocType Layout", influxframework.router.doctype_layout, () => {
				this.make_form(doctype);
				this.show_doc(route);
			});
		} else {
			this.make_form(doctype);
			this.show_doc(route);
		}
	}

	make_form(doctype) {
		this.page.frm = new influxframework.ui.form.Form(
			doctype,
			this.page,
			true,
			influxframework.router.doctype_layout
		);
	}

	setup_events() {
		if (!this.initialized) {
			$(document).on("page-change", function () {
				influxframework.ui.form.close_grid_form();
			});

			influxframework.realtime.on("doc_viewers", function (data) {
				// set users that currently viewing the form
				influxframework.ui.form.FormViewers.set_users(data, "viewers");
			});

			influxframework.realtime.on("doc_typers", function (data) {
				// set users that currently typing on the form
				influxframework.ui.form.FormViewers.set_users(data, "typers");
			});
		}
		this.initialized = true;
	}

	show_doc(route) {
		var doctype = route[1],
			doctype_layout = influxframework.router.doctype_layout || doctype,
			name = route.slice(2).join("/");

		if (influxframework.model.new_names[name]) {
			// document has been renamed, reroute
			name = influxframework.model.new_names[name];
			influxframework.set_route("Form", doctype_layout, name);
			return;
		}

		const doc = influxframework.get_doc(doctype, name);
		if (
			doc &&
			influxframework.model.get_docinfo(doctype, name) &&
			(doc.__islocal || influxframework.model.is_fresh(doc))
		) {
			// is document available and recent?
			this.render(doctype_layout, name);
		} else {
			this.fetch_and_render(doctype, name, doctype_layout);
		}
	}

	fetch_and_render(doctype, name, doctype_layout) {
		influxframework.model.with_doc(doctype, name, (name, r) => {
			if (r && r["403"]) return; // not permitted

			if (!(locals[doctype] && locals[doctype][name])) {
				if (name && name.substr(0, 3) === "new") {
					this.render_new_doc(doctype, name, doctype_layout);
				} else {
					influxframework.show_not_found();
				}
				return;
			}
			this.render(doctype_layout, name);
		});
	}

	render_new_doc(doctype, name, doctype_layout) {
		const new_name = influxframework.model.make_new_doc_and_get_name(doctype, true);
		if (new_name === name) {
			this.render(doctype_layout, name);
		} else {
			influxframework.route_flags.replace_route = true;
			influxframework.set_route("Form", doctype_layout, new_name);
		}
	}

	render(doctype_layout, name) {
		influxframework.container.change_to(doctype_layout);
		influxframework.views.formview[doctype_layout].frm.refresh(name);
	}
};
