xhiveframework.user_info = function (uid) {
	if (!uid) uid = xhiveframework.session.user;

	let user_info;
	if (!(xhiveframework.boot.user_info && xhiveframework.boot.user_info[uid])) {
		user_info = { fullname: uid || "Unknown" };
	} else {
		user_info = xhiveframework.boot.user_info[uid];
	}

	user_info.abbr = xhiveframework.get_abbr(user_info.fullname);
	user_info.color = xhiveframework.get_palette(user_info.fullname);

	return user_info;
};

xhiveframework.update_user_info = function (user_info) {
	for (let user in user_info) {
		if (xhiveframework.boot.user_info[user]) {
			Object.assign(xhiveframework.boot.user_info[user], user_info[user]);
		} else {
			xhiveframework.boot.user_info[user] = user_info[user];
		}
	}
};

xhiveframework.provide("xhiveframework.user");

$.extend(xhiveframework.user, {
	name: "Guest",
	full_name: function (uid) {
		return uid === xhiveframework.session.user
			? __(
					"You",
					null,
					"Name of the current user. For example: You edited this 5 hours ago."
			  )
			: xhiveframework.user_info(uid).fullname;
	},
	image: function (uid) {
		return xhiveframework.user_info(uid).image;
	},
	abbr: function (uid) {
		return xhiveframework.user_info(uid).abbr;
	},
	has_role: function (rl) {
		if (typeof rl == "string") rl = [rl];
		for (var i in rl) {
			if ((xhiveframework.boot ? xhiveframework.boot.user.roles : ["Guest"]).indexOf(rl[i]) != -1)
				return true;
		}
	},
	get_desktop_items: function () {
		// hide based on permission
		var modules_list = $.map(xhiveframework.boot.allowed_modules, function (icon) {
			var m = icon.module_name;
			var type = xhiveframework.modules[m] && xhiveframework.modules[m].type;

			if (xhiveframework.boot.user.allow_modules.indexOf(m) === -1) return null;

			var ret = null;
			if (type === "module") {
				if (xhiveframework.boot.user.allow_modules.indexOf(m) != -1 || xhiveframework.modules[m].is_help)
					ret = m;
			} else if (type === "page") {
				if (xhiveframework.boot.allowed_pages.indexOf(xhiveframework.modules[m].link) != -1) ret = m;
			} else if (type === "list") {
				if (xhiveframework.model.can_read(xhiveframework.modules[m]._doctype)) ret = m;
			} else if (type === "view") {
				ret = m;
			} else if (type === "setup") {
				if (
					xhiveframework.user.has_role("System Manager") ||
					xhiveframework.user.has_role("Administrator")
				)
					ret = m;
			} else {
				ret = m;
			}

			return ret;
		});

		return modules_list;
	},

	is_report_manager: function () {
		return xhiveframework.user.has_role(["Administrator", "System Manager", "Report Manager"]);
	},

	get_formatted_email: function (email) {
		var fullname = xhiveframework.user.full_name(email);

		if (!fullname) {
			return email;
		} else {
			// to quote or to not
			var quote = "";

			// only if these special characters are found
			// why? To make the output same as that in python!
			if (fullname.search(/[\[\]\\()<>@,:;".]/) !== -1) {
				quote = '"';
			}

			return repl("%(quote)s%(fullname)s%(quote)s <%(email)s>", {
				fullname: fullname,
				email: email,
				quote: quote,
			});
		}
	},

	get_emails: () => {
		return Object.keys(xhiveframework.boot.user_info).map((key) => xhiveframework.boot.user_info[key].email);
	},

	/* Normally xhiveframework.user is an object
	 * having properties and methods.
	 * But in the following case
	 *
	 * if (xhiveframework.user === 'Administrator')
	 *
	 * xhiveframework.user will cast to a string
	 * returning xhiveframework.user.name
	 */
	toString: function () {
		return this.name;
	},
});

xhiveframework.session_alive = true;
$(document).bind("mousemove", function () {
	if (xhiveframework.session_alive === false) {
		$(document).trigger("session_alive");
	}
	xhiveframework.session_alive = true;
	if (xhiveframework.session_alive_timeout) clearTimeout(xhiveframework.session_alive_timeout);
	xhiveframework.session_alive_timeout = setTimeout("xhiveframework.session_alive=false;", 30000);
});
