// Copyright (c) 2015, JINGROW and Contributors

jingrow.has_indicator = function (pagetype) {
	// returns true if indicator is present
	if (jingrow.model.is_submittable(pagetype)) {
		return true;
	} else if (
		(jingrow.listview_settings[pagetype] || {}).get_indicator ||
		jingrow.workflow.get_state_fieldname(pagetype)
	) {
		return true;
	} else if (
		jingrow.meta.has_field(pagetype, "enabled") ||
		jingrow.meta.has_field(pagetype, "disabled")
	) {
		return true;
	} else if (
		jingrow.meta.has_field(pagetype, "status") &&
		jingrow.get_meta(pagetype).states.length
	) {
		return true;
	}
	return false;
};

jingrow.get_indicator = function (pg, pagetype, show_workflow_state) {
	if (pg.__unsaved) {
		return [__("Not Saved"), "orange"];
	}

	if (!pagetype) pagetype = pg.pagetype;

	let meta = jingrow.get_meta(pagetype);
	var workflow = jingrow.workflow.workflows[pagetype];
	var without_workflow = workflow ? workflow["override_status"] : true;

	var settings = jingrow.listview_settings[pagetype] || {};

	var is_submittable = jingrow.model.is_submittable(pagetype);
	let workflow_fieldname = jingrow.workflow.get_state_fieldname(pagetype);

	let avoid_status_override = (jingrow.workflow.avoid_status_override[pagetype] || []).includes(
		pg[workflow_fieldname]
	);
	// workflow
	if (
		workflow_fieldname &&
		(!without_workflow || show_workflow_state) &&
		!avoid_status_override
	) {
		var value = pg[workflow_fieldname];
		if (value) {
			let colour = "";

			if (locals["Workflow State"][value] && locals["Workflow State"][value].style) {
				colour = {
					Success: "green",
					Warning: "orange",
					Danger: "red",
					Primary: "blue",
					Inverse: "black",
					Info: "light-blue",
				}[locals["Workflow State"][value].style];
			}
			if (!colour) colour = "gray";

			return [__(value), colour, workflow_fieldname + ",=," + value];
		}
	}

	// draft if document is submittable
	if (is_submittable && pg.pagestatus == 0 && !settings.has_indicator_for_draft) {
		return [__("Draft"), "red", "pagestatus,=,0"];
	}

	// cancelled
	if (is_submittable && pg.pagestatus == 2 && !settings.has_indicator_for_cancelled) {
		return [__("Cancelled"), "red", "pagestatus,=,2"];
	}

	// based on document state
	if (pg.status && meta && meta.states && meta.states.find((d) => d.title === pg.status)) {
		let state = meta.states.find((d) => d.title === pg.status);
		let color_class = jingrow.scrub(state.color, "-");
		return [__(pg.status), color_class, "status,=," + pg.status];
	}

	if (settings.get_indicator) {
		var indicator = settings.get_indicator(pg);
		if (indicator) return indicator;
	}

	// if submittable
	if (is_submittable && pg.pagestatus == 1) {
		return [__("Submitted"), "blue", "pagestatus,=,1"];
	}

	// based on status
	if (pg.status) {
		return [__(pg.status), jingrow.utils.guess_colour(pg.status), "status,=," + pg.status];
	}

	// based on enabled
	if (jingrow.meta.has_field(pagetype, "enabled")) {
		if (pg.enabled) {
			return [__("Enabled"), "blue", "enabled,=,1"];
		} else {
			return [__("Disabled"), "grey", "enabled,=,0"];
		}
	}

	// based on disabled
	if (jingrow.meta.has_field(pagetype, "disabled")) {
		if (pg.disabled) {
			return [__("Disabled"), "grey", "disabled,=,1"];
		} else {
			return [__("Enabled"), "blue", "disabled,=,0"];
		}
	}
};
