{% extends 'opcalendar/base.html' %}
{% load static %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% block title %}
Event Details
{% endblock %}

{% block content %}
    <div class="container">
        <div class="allianceauth-opcalendar">
            <div class="clearfix">
                <a class="btn btn-primary left" href="{% url 'opcalendar:calendar' %}"> Calendar </a>
            </div>
            
            <div class="clearfix">
                <div class="col-lg-12 text-center row">
                <div class="label label-info text-left">
                    <b>{% trans "Current Eve Time:" %} </b>
                </div>
                <strong class="label label-info text-left" id="current-time"></strong>
                <br  />
            </div>
                <h2>{{ event.title }}</h2>
                <p>Created by: <span style="color: gray;">{{ event.eve_character }}</span> Starts in: <span id="countdown{{ event.event_id }}" style="color: gray;">{{ event.eve_character }}</span></p>
                <hr>
                <h4>Event details</h4>
                <table>
                    <tr>
                        <td style="padding-right: 10px;"><b>Type:</b></td>
                        <td><span class="event {{ event.get_html_operation_color }}">Ingame calendar feed</span></td>
                    </tr>
                    <tr>
                        <td style="padding-right: 10px;"><b>Availability:</b></td>
                        <td><span class="event {{ event.get_html_operation_color }}">{{ event.owner_name }}</span></td>
                    </tr>
                    <tr>
                        <td style="padding-right: 10px;"><b>Start time:</b></td>
                        <td>{{ event.event_start_date|date:"D Y-m-d H:i" }} (EVE) / <span id="localtime{{ event.event_id }}"></span> (Local)</td>
                    </tr>

                    <tr>
                        <td style="padding-right: 10px;"><b>Est. end time:</b></td>
                        <td>{{ event.event_end_date|date:"D Y-m-d H:i" }}</td>
                    </tr>
                    <tr>
                        <td style="padding-right: 10px;"><b>Duration:</b></td>
                        <td>{{ event.duration }} minutes</td>
                    </tr>
                </table>
                <p style="padding-top:40px">{{ event.text|striptags|urlize}}</p>   
            </div>
        </div>
    </div>
{% include 'bundles/moment-js.html' with locale=True %}
    <script src="{% static 'js/timers.js' %}"></script>
    <script type="application/javascript">
        // Data
        var timers = [
            {
                'id': {{ event.event_id }},
                'start': moment("{{ event.event_start_date | date:"c" }}"),
                'expired': false
            },
        ];
    </script>
    <script type="application/javascript">

        timedUpdate();
        setAllLocalTimes();

        // Start timed updates
        setInterval(timedUpdate, 1000);

        function timedUpdate() {
            updateClock();
            updateAllTimers();
        }

        function updateAllTimers () {
            var l = timers.length;
            for (var i=0; i < l; ++i) {
                if (timers[i].expired) continue;
                updateTimer(timers[i]);
            }
        }

        /**
         * Update a timer
         * @param timer Timer information
         * @param timer.start Date of the timer
         * @param timer.id Id number of the timer
         * @param timer.expired
         */
        function updateTimer(timer) {
            if (timer.start.isAfter(Date.now())) {
                var duration = moment.duration(timer.start - moment(), 'milliseconds');
                document.getElementById("countdown" + timer.id).innerHTML = getDurationString(duration);
            } else {
                timer.expired = true;
                document.getElementById("countdown" + timer.id).innerHTML = "";
            }
        }

        /**
         * Set all local time fields
         */
        function setAllLocalTimes() {
            var l = timers.length;
            for (var i=0; i < l; ++i) {
                setLocalTime(timers[i]);
            }
        }

        /**
         * Set the local time info for the timer
         * @param timer Timer information
         * @param timer.start Date of the timer
         * @param timer.id Id number of the timer
         */
        function setLocalTime(timer) {
            document.getElementById("localtime" + timer.id).innerHTML = timer.start.format("ddd MM-DD HH:mm");
        }

        function updateClock() {
            document.getElementById("current-time").innerHTML = getCurrentEveTimeString();
        }
    </script>
{% endblock %}
