import os

import flask
import flask_login
from flask_menu import current_menu
import jinja2

import prism
import prism.login
import prism.settings
import prism.helpers

flask_app = prism.flask_app()

@prism.helpers.repeat(0, 60 * 60)
def version_check():
	""" Checks for Prism version updates every hour """

def inject_things():
	""" Adds the site title, the logged in user, and the version
	to each site for use by the base template """

	title = None

	checked_menu = current_menu.active_item
	if checked_menu is not None:
			for child in checked_menu.children:
					checked_menu = child
		title = checked_menu.text

	if title == 'Menu item not initialised':
		title = None

	return dict(title=title, me=prism.login.user(), version=prism.__version__, versioning=prism.settings.PRISM_VERSIONING)

def site_map():
	links = []
	for rule in flask_app.url_map.iter_rules():
		# Filter out rules we can't navigate to in a browser
		# and rules that require parameters
		defaults = rule.defaults if rule.defaults is not None else ()
		arguments = rule.arguments if rule.arguments is not None else ()

		if "GET" in rule.methods and len(defaults) >= len(arguments):
			url = flask.url_for(rule.endpoint, **(rule.defaults or {}))
			links.append(url + ': ' + rule.endpoint)

	return '<pre>' + str('\n'.join(links)) + '</pre>'

def plugin_static(plugin_id, static_file):
	""" Allows plugins to load files from their own static directories """
	plugin = prism.get_plugin(plugin_id)
	if plugin is None:
		return 'Unknown plugin: %s' % plugin_id
	return flask.send_from_directory(plugin.static_folder, static_file)

def permission_denied(e):
	return flask.render_template('other/403.html'), 403

def page_not_found(e):
	return flask.render_template('other/404.html'), 404

def internal_server_error(e):
	return flask.render_template('other/500.html'), 500