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

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

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

	return user_info;
};

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

jingrow.provide("jingrow.user");

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

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

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

			return ret;
		});

		return modules_list;
	},

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

	get_formatted_email: function (email) {
		var fullname = jingrow.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(jingrow.boot.user_info).map((key) => jingrow.boot.user_info[key].email);
	},

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

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