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

jingrow.defaults = {
	get_user_default: function (key) {
		let defaults = jingrow.boot.user.defaults;
		let d = defaults[key];
		if (!d && jingrow.defaults.is_a_user_permission_key(key)) {
			d = defaults[jingrow.model.scrub(key)];
			// Check for default user permission values
			let user_default = this.get_user_permission_default(key, defaults);
			if (user_default) d = user_default;
		}
		if ($.isArray(d)) d = d[0];

		if (!jingrow.defaults.in_user_permission(key, d)) {
			return;
		}

		return d;
	},

	get_user_permission_default: function (key, defaults) {
		let permissions = this.get_user_permissions();
		let user_default = null;
		if (permissions[key]) {
			permissions[key].forEach((item) => {
				if (defaults[key] == item.pg) {
					user_default = item.pg;
				}
			});

			permissions[key].forEach((item) => {
				if (item.is_default) {
					user_default = item.pg;
				}
			});
		}

		return user_default;
	},

	get_user_defaults: function (key) {
		var defaults = jingrow.boot.user.defaults;
		var d = defaults[key];

		if (jingrow.defaults.is_a_user_permission_key(key)) {
			if (d && $.isArray(d) && d.length === 1) {
				// Use User Permission value when only when it has a single value
				d = d[0];
			} else {
				d = defaults[key] || defaults[jingrow.model.scrub(key)];
			}
		}
		if (!$.isArray(d)) d = [d];

		// filter out values which are not permitted to the user
		d.filter((item) => {
			if (jingrow.defaults.in_user_permission(key, item)) {
				return item;
			}
		});
		return d;
	},
	get_global_default: function (key) {
		var d = jingrow.sys_defaults[key];
		if ($.isArray(d)) d = d[0];
		return d;
	},
	get_global_defaults: function (key) {
		var d = jingrow.sys_defaults[key];
		if (!$.isArray(d)) d = [d];
		return d;
	},
	set_user_default_local: function (key, value) {
		jingrow.boot.user.defaults[key] = value;
	},
	get_default: function (key) {
		var defaults = jingrow.boot.user.defaults;
		var value = defaults[key];
		if (jingrow.defaults.is_a_user_permission_key(key)) {
			if (value && $.isArray(value) && value.length === 1) {
				value = value[0];
			} else {
				value = defaults[jingrow.model.scrub(key)];
			}
		}

		if (!jingrow.defaults.in_user_permission(key, value)) {
			return;
		}

		if (value) {
			try {
				return JSON.parse(value);
			} catch (e) {
				return value;
			}
		}
	},

	is_a_user_permission_key: function (key) {
		return key.indexOf(":") === -1 && key !== jingrow.model.scrub(key);
	},

	in_user_permission: function (key, value) {
		let user_permission = this.get_user_permissions()[jingrow.model.unscrub(key)];

		if (user_permission && user_permission.length) {
			return user_permission.some((perm) => {
				return perm.pg === value;
			});
		} else {
			// there is no user permission for this pagetype
			// so we can allow this pg i.e., value
			return true;
		}
	},

	get_user_permissions: function () {
		return this._user_permissions || {};
	},

	update_user_permissions: function () {
		const method = "jingrow.core.pagetype.user_permission.user_permission.get_user_permissions";
		jingrow.call(method).then((r) => {
			if (r.message) {
				this._user_permissions = Object.assign({}, r.message);
			}
		});
	},

	load_user_permission_from_boot: function () {
		if (jingrow.boot.user.user_permissions) {
			this._user_permissions = Object.assign({}, jingrow.boot.user.user_permissions);
		} else {
			jingrow.defaults.update_user_permissions();
		}
	},
};
