# Copyright (c) 2015, InfluxFramework LLC
# License: MIT. See LICENSE

import influxframework
from influxframework.utils import strip_html_tags

no_cache = 1


def get_context(context):
	message_context = influxframework._dict()
	if hasattr(influxframework.local, "message"):
		message_context["header"] = influxframework.local.message_title
		message_context["title"] = strip_html_tags(influxframework.local.message_title)
		message_context["message"] = influxframework.local.message
		if hasattr(influxframework.local, "message_success"):
			message_context["success"] = influxframework.local.message_success

	elif influxframework.local.form_dict.id:
		message_id = influxframework.local.form_dict.id
		key = f"message_id:{message_id}"
		message = influxframework.cache().get_value(key, expires=True)
		if message:
			message_context.update(message.get("context", {}))
			if message.get("http_status_code"):
				influxframework.local.response["http_status_code"] = message["http_status_code"]

	if not message_context.title:
		message_context.title = influxframework.form_dict.title

	if not message_context.message:
		message_context.message = influxframework.form_dict.message

	return message_context
