#!/bin/python
from flask import Flask, request
import libamuse

app = Flask(__name__)

@app.errorhandler(404)
def err_404(e):
	languages = request.headers.get('Accept-Language', 'en-GB')
	mimetype = request.headers.get('Accept', '').split(',')[0]
	return libamuse.ShowErrorPage(404, languages, mimetype)

@app.route('/')
def index():
	languages = request.headers.get('Accept-Language', 'en-GB')
	mimetype = request.headers.get('Accept', '').split(',')[0]
	try:
		page = libamuse.ShowIndex(languages, mimetype)
	except Exception as e:
		try:
			return libamuse.ShowErrorPage(int(str(e.__context__)), languages, mimetype)
		except:
			return f'Fatal error while rendering error {str(e.__context__)}.\nContact admin.', 500
	else:
		return page


@app.route('/about')
def about():
	languages = request.headers.get('Accept-Language', 'en-GB')
	mimetype = request.headers.get('Accept', '').split(',')[0]
	try:
		page = libamuse.ShowAbout(languages, mimetype)
	except Exception as e:
		try:
			return libamuse.ShowErrorPage(int(str(e.__context__)), languages, mimetype)
		except:
			return f'Fatal error while rendering error {str(e.__context__)}.\nContact admin.', 500
	else:
		return page


@app.route('/items/')
def search():
	languages = request.headers.get('Accept-Language', 'en-GB')
	mimetype = request.headers.get('Accept', '').split(',')[0]
	query = request.args.get('q', default='', type=str)
	page = request.args.get('page', default=1, type=int)
	try:
		page = libamuse.PerformSearch(query, languages, mimetype, str(page))
	except Exception as e:
		try:
			return libamuse.ShowErrorPage(int(str(e.__context__)), languages, mimetype)
		except:
			return f'Fatal error while rendering error {str(e.__context__)}.\nContact admin.', 500
	else:
		return page


@app.route('/films/<id>')
def film(id):
	languages = request.headers.get('Accept-Language', 'en-GB')
	mimetype = request.headers.get('Accept', '').split(',')[0]
	etag = request.headers.get('ETag', '')
	try:
		intID = int(id)
	except ValueError:
		return libamuse.ShowErrorPage(400, languages, mimetype)
	try:
		page = libamuse.ShowFilm(id, etag, languages, mimetype)
	except Exception as e:
		try:
			return libamuse.ShowErrorPage(int(str(e.__context__)), languages, mimetype)
		except:
			return f'Fatal error while rendering error {str(e.__context__)}.\nContact admin.', 500
	else:
		return page


@app.route('/series/<id>')
def serie(id):
	languages = request.headers.get('Accept-Language', 'en-GB')
	mimetype = request.headers.get('Accept', '').split(',')[0]
	etag = request.headers.get('ETag', '')
	try:
		intID = int(id)
	except ValueError:
		return libamuse.ShowErrorPage(400, languages, mimetype)
	try:
		page = libamuse.ShowSerie(id, etag, languages, mimetype)
	except Exception as e:
		try:
			return libamuse.ShowErrorPage(int(str(e.__context__)), languages, mimetype)
		except:
			return f'Fatal error while rendering error {str(e.__context__)}.\nContact admin.', 500
	else:
		return page


@app.route('/persons/<id>')
def person(id):
	languages = request.headers.get('Accept-Language', 'en-GB')
	mimetype = request.headers.get('Accept', '').split(',')[0]
	etag = request.headers.get('ETag', '')
	try:
		intID = int(id)
	except ValueError:
		return libamuse.ShowErrorPage(400, languages, mimetype)
	try:
		page = libamuse.ShowPerson(id, etag, languages, mimetype)
	except Exception as e:
		try:
			return libamuse.ShowErrorPage(int(str(e.__context__)), languages, mimetype)
		except:
			return f'Fatal error while rendering error {str(e.__context__)}.\nContact admin.', 500
	else:
		return page

if __name__ == '__main__':
	from gevent import pywsgi
	server = pywsgi.WSGIServer(('127.0.0.1', 5009), app)
	server.serve_forever()
