"""Frontend view and simple pyramid app configurations."""
from urllib.parse import urlparse
import os

from pyramid.config import Configurator
from pyramid.request import Request
from pyramid.response import FileResponse


def config_view(request):
    """Return the frontend configuration."""
    settings = request.registry.settings or {}
    config = {}
    internal_ws_url = settings.get('adhocracy.ws_url', '')
    if 'adhocracy.frontend.ws_url' in settings:
        public_ws_url = settings.get('adhocracy.frontend.ws_url')
    else:
        public_ws_url = _build_ws_url(request, internal_ws_url)
    config['ws_url'] = public_ws_url
    config['pkg_path'] = settings.get('adhocracy.frontend.pkg_path',
                                      '/frontend_static/js/Packages')
    config['root_path'] = '/' + settings.get('adhocracy.platform_id',
                                             'adhocracy')
    return config


def root_view(request):
    """Return the embeddee HTML."""
    here = os.path.dirname(__file__)
    path = os.path.join(here, 'static', 'root.html')
    return FileResponse(path, request=request)


def _build_ws_url(request: Request, url: str) -> str:
    url_parsed = urlparse(url)
    ws_domain = request.domain
    ws_port = url_parsed.port or '80'
    ws_scheme = 'wss' if url_parsed.scheme == 'https' else 'ws'
    return '{}://{}:{}'.format(ws_scheme, ws_domain, ws_port)


def includeme(config):
    """Add routing and static view to deliver the frontend application."""
    config.add_static_view('frontend_static', 'adhocracy.frontend:static',
                           cache_max_age=0)
    config.add_route('embed', 'embed/{directive}')
    config.add_view(root_view, route_name='embed', renderer='html',
                    http_cache=0)
    config.add_route('register', 'register')
    config.add_view(root_view, route_name='register', renderer='html',
                    http_cache=0)
    config.add_route('config_json', 'frontend_config.json')
    config.add_view(config_view, route_name='config_json', renderer='json',
                    http_cache=0)


def main(global_config, **settings):
    """ Return a Pyramid WSGI application to serve the frontend application."""
    config = Configurator(settings=settings)
    includeme(config)
    return config.make_wsgi_app()

@@ -1646,34 +1646,43 @@
 te('
-embed', 'embed/%7Bdirective%7D
+config_json', 'frontend_config.json
 ')%0A 
@@ -1692,36 +1692,38 @@
 config.add_view(
-root
+config
 _view, route_nam
@@ -1725,21 +1725,27 @@
 e_name='
-embed
+config_json
 ', rende
@@ -1741,36 +1741,36 @@
 son', renderer='
-html
+json
 ',%0A             
@@ -1798,91 +1798,96 @@
     
-config.add_route('register', 'register')%0A    config.add_view(root_view, route_name=
+add_frontend_route(config, 'embed', 'embed/%7Bdirective%7D')%0A    add_frontend_route(config, 
 'reg
@@ -1898,117 +1898,155 @@
 r', 
+'
 re
-nderer='html',%0A                    http_cache=0)%0A    config.add_route('config_json', 'frontend_config.json'
+gister')%0A%0A%0Adef add_frontend_route(config, name, pattern):%0A    %22%22%22Add view and route to adhocracy frontend.%22%22%22%0A    config.add_route(name, pattern
 )%0A  
@@ -2055,38 +2055,36 @@
 config.add_view(
-config
+root
 _view, route_nam
@@ -2089,59 +2089,30 @@
 ame=
-'config_json', renderer='json',%0A                   
+name, renderer='html',
  htt

Simplify adding of frontend routes