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

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

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

	return user_info;
};

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

influxframework.provide("influxframework.user");

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

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

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

			return ret;
		});

		return modules_list;
	},

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

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

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

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