{% extends 'pageWrapper.html' %}
{% import "macros.html" as macros %}
{% block head %}
    <title>Multiworld {{ room.id|suuid }}</title>
    {% if should_refresh %}<meta http-equiv="refresh" content="2">{% endif %}
    <meta name="og:site_name" content="Archipelago">
    <meta property="og:title" content="Multiworld {{ room.id|suuid }}">
    <meta property="og:type" content="website" />
    {% if room.seed.slots|length < 2 %}
    <meta property="og:description" content="{{ room.seed.slots|length }} Player World
    {% if room.last_port != -1 %}running on {{ config['HOST_ADDRESS'] }} with port {{ room.last_port }}{% endif %}">
    {% else %}
    <meta property="og:description" content="{{ room.seed.slots|length }} Players Multiworld
    {% if room.last_port != -1 %}running on {{ config['HOST_ADDRESS'] }} with port {{ room.last_port }}{% endif %}">
    {% endif %}
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename="styles/hostRoom.css") }}"/>
{% endblock %}

{% block body %}
    {% include 'header/grassHeader.html' %}
    <div id="host-room">
        {% if room.owner == session["_id"] %}
            Room created from <a href="{{ url_for("view_seed", seed=room.seed.id) }}">Seed #{{ room.seed.id|suuid }}</a>
            <br />
        {% endif %}
        {% if room.tracker %}
            This room has a <a href="{{ url_for("get_multiworld_tracker", tracker=room.tracker) }}">Multiworld Tracker</a> enabled.
            <br />
        {% endif %}
        The server for this room will be paused after {{ room.timeout//60//60 }} hours of inactivity.
        Should you wish to continue later,
        anyone can simply refresh this page and the server will resume.<br>
        {% if room.last_port == -1 %}
            There was an error hosting this Room. Another attempt will be made on refreshing this page.
            The most likely failure reason is that the multiworld is too old to be loaded now.
        {% elif room.last_port %}
            You can connect to this room by using <span class="interactive"
            data-tooltip="This means address/ip is {{ config['HOST_ADDRESS'] }} and port is {{ room.last_port }}.">
            '/connect {{ config['HOST_ADDRESS'] }}:{{ room.last_port }}'
            </span>
            in the <a href="{{ url_for("tutorial_landing")}}">client</a>.<br>
        {% endif %}
        {{ macros.list_patches_room(room) }}
        {% if room.owner == session["_id"] %}
            <div style="display: flex; align-items: center;">
                <form method=post style="flex-grow: 1; margin-right: 1em;">
                    <div class="form-group">
                        <label for="cmd"></label>
                        <input class="form-control" type="text" id="cmd" name="cmd"
                               placeholder="Server Command. /help to list them, list gets appended to log.">
                    </div>
                </form>
                <a href="{{ url_for("display_log", room=room.id) }}">
                    Open Log File...
                </a>
            </div>
        <div id="logger"></div>
        <script type="application/ecmascript">
            let xmlhttp = new XMLHttpRequest();
            let url = '{{ url_for('display_log', room = room.id) }}';

            xmlhttp.onreadystatechange = function () {
                if (this.readyState === 4 && this.status === 200) {
                    document.getElementById("logger").innerText = this.responseText;
                }
            };

            function request_new() {
                xmlhttp.open("GET", url, true);
                xmlhttp.send();
            }

            window.setTimeout(request_new, 1000);
            window.setInterval(request_new, 10000);
        </script>
        {% endif %}
    </div>
{% endblock %}
