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

import logging
import datetime
from pathlib import Path, PosixPath, WindowsPath

import dash

import webviz_config
from webviz_config.themes import installed_themes
from webviz_config.webviz_store import WEBVIZ_STORAGE
from webviz_config.webviz_assets import WEBVIZ_ASSETS
from webviz_config.common_cache import CACHE

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

logging.getLogger().setLevel(logging.{{ loglevel }})

theme = webviz_config.WebvizConfigTheme("{{ theme_name }}")
theme.from_json((Path(__file__).resolve().parent / "theme_settings.json").read_text())

app = dash.Dash()
app.config.suppress_callback_exceptions = True

app.webviz_settings = {
    "shared_settings": webviz_config.SHARED_SETTINGS_SUBSCRIPTIONS.transformed_settings(
        {{ shared_settings }}, {{ config_folder }}, False
    ),
    "theme": theme,
}

CACHE.init_app(app.server)

WEBVIZ_STORAGE.storage_folder = Path(__file__).resolve().parent / "webviz_storage"

# The lines below can be simplified when assignment
# expressions become available in Python 3.8
# (https://www.python.org/dev/peps/pep-0572)

plugins = []

{% for page in pages %}
{% for content in page.content %}
{% if content is not string %}
plugins.append({{ content._call_signature[0] }})
{% endif %}
{% endfor %}
{% endfor %}

for plugin in plugins:
    if hasattr(plugin, "add_webvizstore"):
        WEBVIZ_STORAGE.register_function_arguments(plugin.add_webvizstore())

WEBVIZ_ASSETS.make_portable(Path(__file__).resolve().parent / "assets")

WEBVIZ_STORAGE.build_store()
