#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# AUTOMATICALLY MADE FILE. DO NOT EDIT.
# This file was generated by {{ author }} on {{ current_date }}.

import logging
import logging.config
import threading
import datetime
import os.path as path
from pathlib import Path, PosixPath

import dash
import dash_core_components as dcc
import dash_html_components as html
from flask_talisman import Talisman
import webviz_config
from webviz_config.common_cache import CACHE
from webviz_config.webviz_store import WEBVIZ_STORAGE
from webviz_config.webviz_assets import WEBVIZ_ASSETS

{% for module in _imports %}
{%- if module is string -%}
import {{ module }}
{%- else -%}
import {{ module[0] }} as {{ module[1] }}
{%- endif %}
{% endfor %}


logging.getLogger("werkzeug").setLevel(logging.WARNING)

logging.config.dictConfig(
    {
        "version": 1,
        "disable_existing_loggers": True,
        "formatters": {
            "default": {
                "format": " webviz log [%(asctime)s] %(levelname)s in %(module)s: %(message)s",
                "datefmt": "%Y-%m-%d %H:%M:%S",
            }
        },
        "handlers": {
            "wsgi": {
                "class": "logging.StreamHandler",
                "stream": "ext://flask.logging.wsgi_errors_stream",
                "formatter": "default",
            }
        },
        "root": {"level": "INFO", "handlers": ["wsgi"]},
    }
)

app = dash.Dash(__name__, external_stylesheets={{ external_stylesheets }})
server = app.server

app.title = "{{ title }}"
app.config.suppress_callback_exceptions = True

app.webviz_settings = {
    "portable": {{ portable }},
    "plotly_layout": {{ plotly_layout }},
}

CACHE.init_app(server)

CSP = {{ csp }}
FEATURE_POLICY = {{ feature_policy }}

Talisman(server, content_security_policy=CSP, feature_policy=FEATURE_POLICY)

WEBVIZ_STORAGE.use_storage = {{portable}}
WEBVIZ_STORAGE.storage_folder = path.join(
    path.dirname(path.realpath(__file__)), "webviz_storage"
)

WEBVIZ_ASSETS.portable = {{ portable }}
{{ "WEBVIZ_ASSETS.register_app(app)" if not portable else ""}}

if {{ not portable }} and not webviz_config.is_reload_process():
    # When Dash/Flask is started on localhost with hot module reload activated,
    # we do not want the main process to call expensive component functions in
    # the layout tree, as the layout tree used on initialization will anyway be called
    # from the child/restart/reload process.
    app.layout = html.Div()
else:
    app.layout = dcc.Tabs(
        parent_className="layoutWrapper",
        content_className="pageWrapper",
        vertical=True,
        children=[
          {% for page in pages %}
            dcc.Tab(
              {%- if loop.first -%}
                id="logo",
                className="styledLogo",
              {%- else -%}
                {%- if loop.last -%}
                id="last_page",
                {%- else -%}
                id="{{page.id}}",
                {%- endif -%}
                label="{{page.title}}",
                selected_className="selectedButton",
                className="styledButton",
              {%- endif -%}
                children=[
                  {% for content in page.content -%}
                  {%- if content is string -%}
                    dcc.Markdown(r"""{{ content }}""")
                  {%- else -%}
                    {{ content._call_signature[0] }}.{{ content._call_signature[1] }}
                  {%- endif -%}
                  {{- "" if loop.last else ","}}
                  {% endfor -%}
                ],
            )  {{- "" if loop.last else "," -}}
        {% endfor %}],
    )

if __name__ == "__main__":
    # This part is ignored when the webviz app is started
    # using Docker container and uwsgi (e.g. when hosted on Azure).
    #
    # It is used only when directly running this script with Python,
    # which will then initialize a localhost server.

    port = webviz_config.utils.get_available_port()

    token = webviz_config.LocalhostToken(app.server, port).one_time_token
    webviz_config.LocalhostOpenBrowser(port, token)

    webviz_config.utils.silence_flask_startup()

    app.run_server(
        host="localhost",
        port=port,
        ssl_context=webviz_config.LocalhostCertificate().ssl_context,
        debug=False,
        use_reloader={{not portable}},
      {% if not portable -%}
        dev_tools_hot_reload=True,
        dev_tools_hot_reload_interval=1.0
      {% endif %}
    )
