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

jingrow.provide("jingrow.ui.form.handlers");

window.extend_cscript = (cscript, controller_object) => {
	$.extend(cscript, controller_object);
	if (cscript && controller_object) {
		cscript.__proto__ = controller_object.__proto__;
	}
	return cscript;
};

jingrow.ui.form.get_event_handler_list = function (pagetype, fieldname) {
	if (!jingrow.ui.form.handlers[pagetype]) {
		jingrow.ui.form.handlers[pagetype] = {};
	}
	if (!jingrow.ui.form.handlers[pagetype][fieldname]) {
		jingrow.ui.form.handlers[pagetype][fieldname] = [];
	}
	return jingrow.ui.form.handlers[pagetype][fieldname];
};

jingrow.ui.form.on = jingrow.ui.form.on_change = function (pagetype, fieldname, handler) {
	var add_handler = function (fieldname, handler) {
		var handler_list = jingrow.ui.form.get_event_handler_list(pagetype, fieldname);

		let _handler = (...args) => {
			try {
				return handler(...args);
			} catch (error) {
				console.error(handler);
				throw error;
			}
		};

		handler_list.push(_handler);

		// add last handler to events so it can be called as
		// frm.events.handler(frm)
		if (cur_frm && cur_frm.pagetype === pagetype) {
			cur_frm.events[fieldname] = _handler;
		}
	};

	if (!handler && $.isPlainObject(fieldname)) {
		// a dict of handlers {fieldname: handler, ...}
		for (var key in fieldname) {
			var fn = fieldname[key];
			if (typeof fn === "function") {
				add_handler(key, fn);
			}
		}
	} else {
		add_handler(fieldname, handler);
	}
};

// remove standard event handlers
jingrow.ui.form.off = function (pagetype, fieldname, handler) {
	var handler_list = jingrow.ui.form.get_event_handler_list(pagetype, fieldname);
	if (handler_list.length) {
		jingrow.ui.form.handlers[pagetype][fieldname] = [];
	}

	if (cur_frm && cur_frm.pagetype === pagetype && cur_frm.events[fieldname]) {
		delete cur_frm.events[fieldname];
	}

	if (cur_frm && cur_frm.cscript && cur_frm.cscript[fieldname]) {
		delete cur_frm.cscript[fieldname];
	}
};

jingrow.ui.form.trigger = function (pagetype, fieldname) {
	cur_frm.script_manager.trigger(fieldname, pagetype);
};

jingrow.ui.form.ScriptManager = class ScriptManager {
	constructor(opts) {
		$.extend(this, opts);
	}
	make(ControllerClass) {
		this.frm.cscript = extend_cscript(
			this.frm.cscript,
			new ControllerClass({ frm: this.frm })
		);
	}
	trigger(event_name, pagetype, name) {
		// trigger all the form level events that
		// are bound to this event_name
		let me = this;
		pagetype = pagetype || this.frm.pagetype;
		name = name || this.frm.pagename;

		let tasks = [];
		let handlers = this.get_handlers(event_name, pagetype);

		// helper for child table
		this.frm.selected_pg = jingrow.get_pg(pagetype, name);

		let runner = (_function, is_old_style) => {
			let _promise = null;
			if (is_old_style) {
				// old style arguments (pg, cdt, cdn)
				_promise = me.frm.cscript[_function](me.frm.pg, pagetype, name);
			} else {
				// new style (frm, pagetype, name)
				_promise = _function(me.frm, pagetype, name);
			}

			// if the trigger returns a promise, return it,
			// or use the default promise jingrow.after_ajax
			if (_promise && _promise.then) {
				return _promise;
			} else {
				return jingrow.after_server_call();
			}
		};

		// make list of functions to be run serially
		handlers.new_style.forEach((_function) => {
			if (event_name === "setup") {
				// setup must be called immediately
				runner(_function, false);
			} else {
				tasks.push(() => runner(_function, false));
			}
		});

		handlers.old_style.forEach((_function) => {
			if (event_name === "setup") {
				// setup must be called immediately
				runner(_function, true);
			} else {
				tasks.push(() => runner(_function, true));
			}
		});

		// run them serially
		return jingrow.run_serially(tasks);
	}
	has_handlers(event_name, pagetype) {
		let handlers = this.get_handlers(event_name, pagetype);
		return handlers && (handlers.old_style.length || handlers.new_style.length);
	}
	get_handlers(event_name, pagetype) {
		// returns list of all functions to be called (old style and new style)
		let me = this;
		let handlers = {
			old_style: [],
			new_style: [],
		};
		if (jingrow.ui.form.handlers[pagetype] && jingrow.ui.form.handlers[pagetype][event_name]) {
			$.each(jingrow.ui.form.handlers[pagetype][event_name], function (i, fn) {
				handlers.new_style.push(fn);
			});
		}
		if (this.frm.cscript[event_name]) {
			handlers.old_style.push(event_name);
		}
		if (this.frm.cscript["custom_" + event_name]) {
			handlers.old_style.push("custom_" + event_name);
		}
		return handlers;
	}
	setup() {
		const pagetype = this.frm.meta;
		const me = this;
		let client_script = pagetype.__js;

		// append the custom script for this form's layout
		if (this.frm.pagetype_layout?.client_script) {
			// add a newline to avoid conflict with pagetype JS
			client_script += `\n${this.frm.pagetype_layout.client_script}`;
		}

		if (client_script) {
			new Function(client_script)();
		}

		if (!this.frm.pagetype_layout && pagetype.__custom_js) {
			try {
				new Function(pagetype.__custom_js)();
			} catch (e) {
				jingrow.msgprint({
					title: __("Error in Client Script"),
					indicator: "orange",
					message: '<pre class="small"><code>' + e.stack + "</code></pre>",
				});
			}
		}

		function setup_add_fetch(df) {
			let is_read_only_field =
				[
					"Data",
					"Read Only",
					"Text",
					"Small Text",
					"Currency",
					"Check",
					"Text Editor",
					"Attach Image",
					"Code",
					"Link",
					"Float",
					"Int",
					"Date",
					"Datetime",
					"Select",
					"Duration",
					"Time",
				].includes(df.fieldtype) ||
				df.read_only == 1 ||
				df.is_virtual == 1;

			if (is_read_only_field && df.fetch_from && df.fetch_from.indexOf(".") != -1) {
				var parts = df.fetch_from.split(".");
				me.frm.add_fetch(parts[0], parts[1], df.fieldname, df.parent);
			}
		}

		// setup add fetch
		$.each(this.frm.fields, function (i, field) {
			setup_add_fetch(field.df);
			if (jingrow.model.table_fields.includes(field.df.fieldtype)) {
				$.each(
					jingrow.meta.get_pagefields(field.df.options, me.frm.pagename),
					function (i, df) {
						setup_add_fetch(df);
					}
				);
			}
		});

		// css
		pagetype.__css && jingrow.dom.set_style(pagetype.__css);

		this.trigger("setup");
	}
	log_error(caller, e) {
		jingrow.show_alert({ message: __("Error in Client Script."), indicator: "error" });
		console.group && console.group();
		console.log("----- error in client script -----");
		console.log("method: " + caller);
		console.log(e);
		console.log("error message: " + e.message);
		console.trace && console.trace();
		console.log("----- end of error message -----");
		console.group && console.groupEnd();
	}
	copy_from_first_row(parentfield, current_row, fieldnames) {
		var data = this.frm.pg[parentfield];
		if (data.length === 1 || data[0] === current_row) return;

		if (typeof fieldnames === "string") {
			fieldnames = [fieldnames];
		}

		$.each(fieldnames, function (i, fieldname) {
			jingrow.model.set_value(
				current_row.pagetype,
				current_row.name,
				fieldname,
				data[0][fieldname]
			);
		});
	}
};
