import { io } from "socket.io-client";
xhiveframework.socketio = {
	open_tasks: {},
	open_docs: [],
	emit_queue: [],

	init: function (port = 3000) {
		if (xhiveframework.boot.disable_async) {
			return;
		}

		if (xhiveframework.socketio.socket) {
			return;
		}

		// Enable secure option when using HTTPS
		if (window.location.protocol == "https:") {
			xhiveframework.socketio.socket = io.connect(xhiveframework.socketio.get_host(port), {
				secure: true,
				withCredentials: true,
				reconnectionAttempts: 3,
			});
		} else if (window.location.protocol == "http:") {
			xhiveframework.socketio.socket = io.connect(xhiveframework.socketio.get_host(port), {
				withCredentials: true,
				reconnectionAttempts: 3,
			});
		}

		if (!xhiveframework.socketio.socket) {
			console.log("Unable to connect to " + xhiveframework.socketio.get_host(port));
			return;
		}

		xhiveframework.socketio.socket.on("msgprint", function (message) {
			xhiveframework.msgprint(message);
		});

		xhiveframework.socketio.socket.on("progress", function (data) {
			if (data.progress) {
				data.percent = (flt(data.progress[0]) / data.progress[1]) * 100;
			}
			if (data.percent) {
				xhiveframework.show_progress(
					data.title || __("Progress"),
					data.percent,
					100,
					data.description,
					true
				);
			}
		});

		xhiveframework.socketio.setup_listeners();
		xhiveframework.socketio.setup_reconnect();

		$(document).on("form-load form-rename", function (e, frm) {
			if (!frm.doc || frm.is_new()) {
				return;
			}

			for (var i = 0, l = xhiveframework.socketio.open_docs.length; i < l; i++) {
				var d = xhiveframework.socketio.open_docs[i];
				if (frm.doctype == d.doctype && frm.docname == d.name) {
					// already subscribed
					return false;
				}
			}

			xhiveframework.socketio.doc_subscribe(frm.doctype, frm.docname);
		});

		$(document).on("form-refresh", function (e, frm) {
			if (!frm.doc || frm.is_new()) {
				return;
			}

			xhiveframework.socketio.doc_open(frm.doctype, frm.docname);
		});

		$(document).on("form-unload", function (e, frm) {
			if (!frm.doc || frm.is_new()) {
				return;
			}

			// xhiveframework.socketio.doc_unsubscribe(frm.doctype, frm.docname);
			xhiveframework.socketio.doc_close(frm.doctype, frm.docname);
		});

		$(document).on("form-typing", function (e, frm) {
			xhiveframework.socketio.form_typing(frm.doctype, frm.docname);
		});

		$(document).on("form-stopped-typing", function (e, frm) {
			xhiveframework.socketio.form_stopped_typing(frm.doctype, frm.docname);
		});

		window.addEventListener("beforeunload", () => {
			if (!cur_frm || !cur_frm.doc || cur_frm.is_new()) {
				return;
			}

			xhiveframework.socketio.doc_close(cur_frm.doctype, cur_frm.docname);
		});
	},
	get_host: function (port = 3000) {
		var host = window.location.origin;
		if (window.dev_server) {
			var parts = host.split(":");
			port = xhiveframework.boot.socketio_port || port.toString() || "3000";
			if (parts.length > 2) {
				host = parts[0] + ":" + parts[1];
			}
			host = host + ":" + port;
		}
		return host;
	},
	subscribe: function (task_id, opts) {
		// TODO DEPRECATE

		xhiveframework.socketio.socket.emit("task_subscribe", task_id);
		xhiveframework.socketio.socket.emit("progress_subscribe", task_id);

		xhiveframework.socketio.open_tasks[task_id] = opts;
	},
	task_subscribe: function (task_id) {
		xhiveframework.socketio.socket.emit("task_subscribe", task_id);
	},
	task_unsubscribe: function (task_id) {
		xhiveframework.socketio.socket.emit("task_unsubscribe", task_id);
	},
	doctype_subscribe: function (doctype) {
		xhiveframework.socketio.socket.emit("doctype_subscribe", doctype);
	},
	doc_subscribe: function (doctype, docname) {
		if (xhiveframework.flags.doc_subscribe) {
			console.log("throttled");
			return;
		}

		xhiveframework.flags.doc_subscribe = true;

		// throttle to 1 per sec
		setTimeout(function () {
			xhiveframework.flags.doc_subscribe = false;
		}, 1000);

		xhiveframework.socketio.socket.emit("doc_subscribe", doctype, docname);
		xhiveframework.socketio.open_docs.push({ doctype: doctype, docname: docname });
	},
	doc_unsubscribe: function (doctype, docname) {
		xhiveframework.socketio.socket.emit("doc_unsubscribe", doctype, docname);
		xhiveframework.socketio.open_docs = $.filter(xhiveframework.socketio.open_docs, function (d) {
			if (d.doctype === doctype && d.name === docname) {
				return null;
			} else {
				return d;
			}
		});
	},
	doc_open: function (doctype, docname) {
		// notify that the user has opened this doc, if not already notified
		if (
			!xhiveframework.socketio.last_doc ||
			xhiveframework.socketio.last_doc[0] != doctype ||
			xhiveframework.socketio.last_doc[1] != docname
		) {
			xhiveframework.socketio.socket.emit("doc_open", doctype, docname);

			xhiveframework.socketio.last_doc &&
				xhiveframework.socketio.doc_close(
					xhiveframework.socketio.last_doc[0],
					xhiveframework.socketio.last_doc[1]
				);
		}
		xhiveframework.socketio.last_doc = [doctype, docname];
	},
	doc_close: function (doctype, docname) {
		// notify that the user has closed this doc
		xhiveframework.socketio.socket.emit("doc_close", doctype, docname);

		// if the doc is closed the user has also stopped typing
		xhiveframework.socketio.socket.emit("doc_typing_stopped", doctype, docname);
	},
	form_typing: function (doctype, docname) {
		// notifiy that the user is typing on the doc
		xhiveframework.socketio.socket.emit("doc_typing", doctype, docname);
	},
	form_stopped_typing: function (doctype, docname) {
		// notifiy that the user has stopped typing
		xhiveframework.socketio.socket.emit("doc_typing_stopped", doctype, docname);
	},
	setup_listeners: function () {
		xhiveframework.socketio.socket.on("task_status_change", function (data) {
			xhiveframework.socketio.process_response(data, data.status.toLowerCase());
		});
		xhiveframework.socketio.socket.on("task_progress", function (data) {
			xhiveframework.socketio.process_response(data, "progress");
		});
	},
	setup_reconnect: function () {
		// subscribe again to open_tasks
		xhiveframework.socketio.socket.on("connect", function () {
			// wait for 5 seconds before subscribing again
			// because it takes more time to start python server than nodejs server
			// and we use validation requests to python server for subscribing
			setTimeout(function () {
				$.each(xhiveframework.socketio.open_tasks, function (task_id, opts) {
					xhiveframework.socketio.subscribe(task_id, opts);
				});

				// re-connect open docs
				$.each(xhiveframework.socketio.open_docs, function (d) {
					if (locals[d.doctype] && locals[d.doctype][d.name]) {
						xhiveframework.socketio.doc_subscribe(d.doctype, d.name);
					}
				});

				if (cur_frm && cur_frm.doc && !cur_frm.is_new()) {
					xhiveframework.socketio.doc_open(cur_frm.doc.doctype, cur_frm.doc.name);
				}
			}, 5000);
		});
	},
	process_response: function (data, method) {
		if (!data) {
			return;
		}

		// success
		var opts = xhiveframework.socketio.open_tasks[data.task_id];
		if (opts[method]) {
			opts[method](data);
		}

		// "callback" is std xhiveframework term
		if (method === "success") {
			if (opts.callback) opts.callback(data);
		}

		// always
		xhiveframework.request.cleanup(opts, data);
		if (opts.always) {
			opts.always(data);
		}

		// error
		if (data.status_code && data.status_code > 400 && opts.error) {
			opts.error(data);
		}
	},
};

xhiveframework.provide("xhiveframework.realtime");
xhiveframework.realtime.on = function (event, callback) {
	xhiveframework.socketio.socket && xhiveframework.socketio.socket.on(event, callback);
};

xhiveframework.realtime.off = function (event, callback) {
	xhiveframework.socketio.socket && xhiveframework.socketio.socket.off(event, callback);
};

xhiveframework.realtime.publish = function (event, message) {
	if (xhiveframework.socketio.socket) {
		xhiveframework.socketio.socket.emit(event, message);
	}
};
