{% extends 'mesh/base.html' %}
{% load i18n mesh_node %}

{% block heading %}{% trans 'Mesh Node' %} {{ node }}{% endblock %}

{% block subcontent %}
    <div class="columns">
        <div>
            <h4>Connection</h4>
            <p>
                <strong>Address:</strong> {{ node.address }}<br>
            </p>
            <p>
                <strong>Uplink:</strong>
                {% with uplink=node.get_uplink %}
                    {% if uplink %}
                        {% mesh_node uplink.node %}<br>
                        {% if uplink.node == node %}
                            {% trans '(is an uplink)' %}
                        {% elif node.upstream %}
                            {% trans 'via:' %} {% mesh_node node.upstream %}
                        {% else %}
                            {% trans '(direct)' %}
                        {% endif %}
                    {% else %}
                        <em>offline</em>
                    {% endif %}
                {% endwith %}
            </p>
            <p>
                <strong>Last signin:</strong>
                {{ node.last_signin.date }} {{ node.last_signin.time|date:"H:i:s" }}
                ({% blocktrans trimmed with timesince=node.last_signin|timesince %}
                    {{ timesince }} ago
                {% endblocktrans %})
                <br>

                <strong>Last Message:</strong>
                {{ node.last_messages.any.datetime.date }} {{ node.last_messages.any.datetime|date:"H:i:s" }}
                ({% blocktrans trimmed with timesince=node.last_messages.any.datetime|timesince %}
                    {{ timesince }} ago
                {% endblocktrans %})
                <br>
            </p>
            <p>
                <a class="button" href="{% url "mesh.messages" %}?src_nodes={{ node.address }}">
                    {% trans 'View messages' %}
                </a>
                <a class="button" href="{% url "mesh.send" msg_type="MESH_ROUTE_REQUEST" %}?address={{ node.address }}" >
                    {% trans 'Find route' %}
                </a>
            </p>


            <h4>Hardware</h4>
            <p>
                <strong>Board:</strong> {{ node.last_messages.CONFIG_BOARD.parsed.content.board_config.board.pretty_name }}
                (with {{ node.last_messages.CONFIG_HARDWARE.parsed.content.chip.pretty_name }} rev{{ node.last_messages.CONFIG_HARDWARE.parsed.content.revision_major }}.{{ node.last_messages.CONFIG_HARDWARE.parsed.content.revision_minor }})

                {% if node.last_messages.CONFIG_BOARD.parsed.content.led %}
                <br>
                <strong>Status LED:</strong> {{ node.last_messages.CONFIG_BOARD.parsed.content.led }}
                {% endif %}

                {% if node.last_messages.CONFIG_BOARD.parsed.content.uwb %}
                <br>
                <strong>UWB:</strong> {{ node.last_messages.CONFIG_BOARD.parsed.content.led }}
                {% endif %}
            </p>
            <p>
                <a class="button" href="{% url "mesh.send" recipient=node.address msg_type="CONFIG_BOARD" %}">
                    {% trans 'Change board settings' %}
                </a>
            </p>

            <h4>Firmware</h4>
            <p>
                {% if node.firmware_description.build %}
                    <strong>Firmware:</strong> <a href="{% url "mesh.firmwares.detail" pk=node.firmware_description.build.version.pk %}">{{ node.firmware_description.project_name }} {{ node.firmware_description.version }}</a><br>
                    <strong>Build:</strong> <a href="{% url "mesh.firmwares.build.detail" pk=node.firmware_description.build.pk %}">{{ node.firmware_description.build.variant }}</a><br>
                    <strong>Created:</strong> {{ node.firmware_description.created }}<br>
                {% else %}
                    <strong>Firmware:</strong> {{ node.firmware_description.project_name }} {{ node.firmware_description.version }}<br>
                    <strong>First seen:</strong> {{ node.firmware_description.created }}<br>
                    <strong>SHA256:</strong> <small>{{ node.firmware_description.sha256_hash }}</small><br>
                {% endif %}
            </p>

            <h4>Uplink configuration</h4>
            <p>
                <strong>Enabled:</strong> {{ node.last_messages.CONFIG_UPLINK.parsed.content.enabled }},
                <strong>SSID:</strong> {{ node.last_messages.CONFIG_UPLINK.parsed.content.ssid }},
                <strong>Channel:</strong> {{ node.last_messages.CONFIG_UPLINK.parsed.content.channel }}<br>
                <strong>Host:</strong> {{ node.last_messages.CONFIG_UPLINK.parsed.content.host }},
                <strong>Port:</strong> {{ node.last_messages.CONFIG_UPLINK.parsed.content.port }},
                <strong>UDP:</strong> {{ node.last_messages.CONFIG_UPLINK.parsed.content.udp }},
                <strong>SSL:</strong> {{ node.last_messages.CONFIG_UPLINK.parsed.content.ssl }}<br>
            </p>
            <p>
                <a class="button" href="{% url "mesh.send" recipient=node.address msg_type="CONFIG_UPLINK" %}">
                    {% trans 'Change' %}
                </a>
            </p>
        </div>
        <div>
            <h4>Node configuration</h4>
            <p>
                <strong>Number:</strong> {{ node.last_messages.CONFIG_NODE.parsed.content.number }},
                <strong>Name:</strong> {{ node.last_messages.CONFIG_NODE.parsed.content.name }}
            </p>
            <p>
                <a class="button" href="{% url "mesh.send" recipient=node.address msg_type="CONFIG_NODE" %}">
                    {% trans 'Change' %}
                </a>
            </p>

            <h4>Position configuration</h4>
            <p>
                <strong>On Device:</strong>
                X={{ node.last_messages.CONFIG_POSITION.parsed.content.x_pos | cm_to_m }}
                Y={{ node.last_messages.CONFIG_POSITION.parsed.content.y_pos | cm_to_m }}
                Z={{ node.last_messages.CONFIG_POSITION.parsed.content.z_pos | cm_to_m }}
            </p>
            {% with locator_xyz=node.get_locator_xyz %}
                {% if locator_xyz %}
                    <p>
                        <strong>In Map:</strong>
                        X={{ locator_xyz.0 | cm_to_m }}
                        Y={{ locator_xyz.1 | cm_to_m }}
                        Z={{ locator_xyz.2 | cm_to_m }}
                        {% if node.ranging_beacon %}
                            <br>
                            <a href="{% url "editor.ranging_beacons.edit" space=node.ranging_beacon.space_id pk=node.ranging_beacon.pk %}">Ranging Beacon #{{ node.ranging_beacon.pk }}</a>
                            {% if node.ranging_beacon.comment %}({{ node.ranging_beacon.comment }}){% endif %}
                            in {{ node.ranging_beacon.space.title }}
                        {% endif %}
                    </p>
                {% else %}
                    <p>
                        <em>(not known in map)</em>
                    </p>
                    <p>
                        <a class="button" href="{% url "mesh.send" recipient=node.address msg_type="CONFIG_POSITION" %}">
                            {% trans 'Change' %}
                        </a>
                    </p>
                {% endif %}
            {% endwith %}

            <h4>iBeacon configuration</h4>
            <p>
                <strong>UUID:</strong> {{ node.last_messages.CONFIG_IBEACON.parsed.content.uuid }}<br>
                <strong>Major:</strong> {{ node.last_messages.CONFIG_IBEACON.parsed.content.major }},
                <strong>Minor:</strong> {{ node.last_messages.CONFIG_IBEACON.parsed.content.minor }}
            </p>
            <p>
                <a class="button" href="{% url "mesh.send" recipient=node.address msg_type="CONFIG_IBEACON" %}">
                    {% trans 'Change' %}
                </a>
            </p>
        </div>
    </div>
{% endblock %}
