import influxframework
from influxframework.website.page_renderers.error_page import ErrorPage
from influxframework.website.page_renderers.not_found_page import NotFoundPage
from influxframework.website.page_renderers.not_permitted_page import NotPermittedPage
from influxframework.website.page_renderers.redirect_page import RedirectPage
from influxframework.website.path_resolver import PathResolver


def get_response(path=None, http_status_code=200):
	"""Resolves path and renders page"""
	response = None
	path = path or influxframework.local.request.path
	endpoint = path

	try:
		path_resolver = PathResolver(path)
		endpoint, renderer_instance = path_resolver.resolve()
		response = renderer_instance.render()
	except influxframework.Redirect:
		return RedirectPage(endpoint or path, http_status_code).render()
	except influxframework.PermissionError as e:
		response = NotPermittedPage(endpoint, http_status_code, exception=e).render()
	except influxframework.PageDoesNotExistError:
		response = NotFoundPage(endpoint, http_status_code).render()
	except Exception as e:
		influxframework.log_error(f"{path} failed")
		response = ErrorPage(exception=e).render()

	return response


def get_response_content(path=None, http_status_code=200):
	response = get_response(path, http_status_code)
	return str(response.data, "utf-8")
