{% extends '@WebProfiler/Profiler/layout.html.twig' %}

{% import _self as helper %}

{% block toolbar %}
    {% if collector.counterrors or collector.countdeprecations or collector.countwarnings %}
        {% set icon %}
            {% set status_color = collector.counterrors ? 'red' : collector.countwarnings ? 'yellow' : 'none' %}
            {{ include('@WebProfiler/Icon/logger.svg') }}
            <span class="sf-toolbar-value">{{ collector.counterrors ?: (collector.countdeprecations + collector.countwarnings) }}</span>
        {% endset %}

        {% set text %}
            <div class="sf-toolbar-info-piece">
                <b>Errors</b>
                <span class="sf-toolbar-status sf-toolbar-status-{{ collector.counterrors ? 'red' }}">{{ collector.counterrors|default(0) }}</span>
            </div>

            <div class="sf-toolbar-info-piece">
                <b>Warnings</b>
                <span class="sf-toolbar-status sf-toolbar-status-{{ collector.countwarnings ? 'yellow' }}">{{ collector.countwarnings|default(0) }}</span>
            </div>

            <div class="sf-toolbar-info-piece">
                <b>Deprecations</b>
                <span class="sf-toolbar-status sf-toolbar-status-{{ collector.countdeprecations ? 'none' }}">{{ collector.countdeprecations|default(0) }}</span>
            </div>
        {% endset %}

        {{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { link: profiler_url, status: status_color }) }}
    {% endif %}
{% endblock %}

{% block menu %}
    <span class="label label-status-{{ collector.counterrors ? 'error' : collector.countwarnings ? 'warning' : 'none' }} {{ collector.logs is empty ? 'disabled' }}">
        <span class="icon">{{ include('@WebProfiler/Icon/logger.svg') }}</span>
        <strong>Logs</strong>
        {% if collector.counterrors or collector.countdeprecations or collector.countwarnings %}
            <span class="count">
                <span>{{ collector.counterrors ?: (collector.countdeprecations + collector.countwarnings) }}</span>
            </span>
        {% endif %}
    </span>
{% endblock %}

{% block panel %}
    <h2>Log Messages</h2>

    {% if collector.processedLogs is empty %}
        <div class="empty">
            <p>No log messages available.</p>
        </div>
    {% else %}
        {% set has_error_logs = collector.processedLogs|column('type')|filter(type => 'error' == type)|length > 0 %}
        {% set has_deprecation_logs = collector.processedLogs|column('type')|filter(type => 'deprecation' == type)|length > 0 %}

        {% set filters = collector.filters %}
        <div class="log-filters">
            <div id="log-filter-type" class="log-filter">
                <ul class="tab-navigation">
                    <li class="{{ not has_error_logs and not has_deprecation_logs ? 'active' }}">
                        <input {{ not has_error_logs and not has_deprecation_logs ? 'checked' }} type="radio" id="filter-log-type-all" name="filter-log-type" value="all">
                        <label for="filter-log-type-all">All messages</label>
                    </li>

                    <li class="{{ has_error_logs ? 'active' }}">
                        <input {{ has_error_logs ? 'checked' }} type="radio" id="filter-log-type-error" name="filter-log-type" value="error">
                        <label for="filter-log-type-error">
                            Errors
                            <span class="badge status-{{ collector.counterrors ? 'error' }}">{{ collector.counterrors|default(0) }}</span>
                        </label>
                    </li>

                    <li class="{{ not has_error_logs and has_deprecation_logs ? 'active' }}">
                        <input {{ not has_error_logs and has_deprecation_logs ? 'checked' }} type="radio" id="filter-log-type-deprecation" name="filter-log-type" value="deprecation">
                        <label for="filter-log-type-deprecation">
                            Deprecations
                            <span class="badge status-{{ collector.countdeprecations ? 'warning' }}">{{ collector.countdeprecations|default(0) }}</span>
                        </label>
                    </li>
                </ul>
            </div>

            <details id="log-filter-priority" class="log-filter">
                <summary>
                    <span class="icon">{{ include('@WebProfiler/Icon/filter.svg') }}</span>
                    Level (<span class="filter-active-num">{{ filters.priority|length - 1 }}</span>)
                </summary>

                <div class="log-filter-content">
                    <div class="filter-select-all-or-none">
                        <button type="button" class="btn btn-link select-all">Select All</button>
                        <button type="button" class="btn btn-link select-none">Select None</button>
                    </div>

                    {% for label, value in filters.priority %}
                        <div class="log-filter-option">
                            <input {{ 'debug' != value ? 'checked' }} type="checkbox" id="filter-log-level-{{ value }}" name="filter-log-level-{{ value }}" value="{{ value }}">
                            <label for="filter-log-level-{{ value }}">{{ label }}</label>
                        </div>
                    {% endfor %}
                </div>
            </details>

            <details id="log-filter-channel" class="log-filter">
                <summary>
                    <span class="icon">{{ include('@WebProfiler/Icon/filter.svg') }}</span>
                    Channel (<span class="filter-active-num">{{ filters.channel|length - 1 }}</span>)
                </summary>

                <div class="log-filter-content">
                    <div class="filter-select-all-or-none">
                        <button type="button" class="btn btn-link select-all">Select All</button>
                        <button type="button" class="btn btn-link select-none">Select None</button>
                    </div>

                    {% for value in filters.channel %}
                        <div class="log-filter-option">
                            <input {{ 'event' != value ? 'checked' }} type="checkbox" id="filter-log-channel-{{ value }}" name="filter-log-channel-{{ value }}" value="{{ value }}">
                            <label for="filter-log-channel-{{ value }}">{{ value|title }}</label>
                        </div>
                    {% endfor %}
                </div>
            </details>
        </div>

        <table class="logs">
            <colgroup>
                <col width="140px">
                <col>
            </colgroup>

            <thead>
                <th>Time</th>
                <th>Message</th>
            </thead>

            <tbody>
                {% for log in collector.processedLogs %}
                    {% set css_class = 'error' == log.type ? 'error'
                        : (log.priorityName == 'WARNING' or 'deprecation' == log.type) ? 'warning'
                        : 'silenced' == log.type ? 'silenced'
                    %}
                    <tr class="log-status-{{ css_class }}" data-type="{{ log.type }}" data-priority="{{ log.priority }}" data-channel="{{ log.channel }}" style="{{ 'event' == log.channel or 'DEBUG' == log.priorityName ? 'display: none' }}">
                        <td class="log-timestamp">
                            <time title="{{ log.timestamp|date('r') }}" datetime="{{ log.timestamp|date('c') }}">
                                {{ log.timestamp|date('H:i:s.v') }}
                            </time>

                            {% if log.type in ['error', 'deprecation', 'silenced'] or 'WARNING' == log.priorityName %}
                                <span class="log-type-badge badge badge-{{ css_class }}">
                                    {% if 'error' == log.type or 'WARNING' == log.priorityName %}
                                        {{ log.priorityName|lower }}
                                    {% else %}
                                        {{ log.type|lower }}
                                    {% endif %}
                                </span>
                            {% else %}
                                <span class="log-type-badge badge badge-{{ css_class }}">
                                    {{ log.priorityName|lower }}
                                </span>
                            {% endif %}
                        </td>

                        <td class="font-normal">
                            {{ helper.render_log_message('debug', loop.index, log) }}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>

        <div class="no-logs-message empty">
            <p>There are no log messages.</p>
        </div>

        <script>Sfjs.initializeLogsTable();</script>
    {% endif %}

    {% set compilerLogTotal = 0 %}
    {% for logs in collector.compilerLogs %}
        {% set compilerLogTotal = compilerLogTotal + logs|length %}
    {% endfor %}

    <details class="container-compilation-logs">
        <summary>
            <h4>Container Compilation Logs <span class="text-muted">({{ compilerLogTotal }})</span></h4>
            <p class="text-muted">Log messages generated during the compilation of the service container.</p>
        </summary>

        {% if collector.compilerLogs is empty %}
            <div class="empty">
                <p>There are no compiler log messages.</p>
            </div>
        {% else %}
            <table class="container-logs">
                <thead>
                <tr>
                    <th>Messages</th>
                    <th class="full-width">Class</th>
                </tr>
                </thead>

                <tbody>
                {% for class, logs in collector.compilerLogs %}
                    <tr>
                        <td class="font-normal text-right">{{ logs|length }}</td>
                        <td class="font-normal">
                            {% set context_id = 'context-compiler-' ~ loop.index %}

                            <button type="button" class="btn btn-link sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="{{ class }}">{{ class }}</button>

                            <div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
                                <ul class="break-long-words">
                                    {% for log in logs %}
                                        <li>{{ profiler_dump_log(log.message) }}</li>
                                    {% endfor %}
                                </ul>
                            </div>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        {% endif %}
    </details>
{% endblock %}

{% macro render_log_message(category, log_index, log) %}
    {% set has_context = log.context is defined and log.context is not empty %}
    {% set has_trace = log.context.exception.trace is defined %}

    {% if not has_context %}
        {{ profiler_dump_log(log.message) }}
    {% else %}
        {{ profiler_dump_log(log.message, log.context) }}
    {% endif %}

    <div class="log-metadata">
        {% if log.channel %}
            <span class="badge">{{ log.channel }}</span>
        {% endif %}

        {% if log.errorCount is defined and log.errorCount > 1 %}
            <span class="log-num-occurrences">{{ log.errorCount }} times</span>
        {% endif %}

        {% if has_context %}
            {% set context_id = 'context-' ~ category ~ '-' ~ log_index %}
            <span><button type="button" class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide context">Show context</button></span>
        {% endif %}

        {% if has_trace %}
            {% set trace_id = 'trace-' ~ category ~ '-' ~ log_index %}
            <span><button type="button" class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ trace_id }}" data-toggle-alt-content="Hide trace">Show trace</button></span>
        {% endif %}

        {% if has_context %}
            <div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
                {{ profiler_dump(log.context, maxDepth=1) }}
            </div>
        {% endif %}

        {% if has_trace %}
            <div id="{{ trace_id }}" class="context sf-toggle-content sf-toggle-hidden">
                {{ profiler_dump(log.context.exception.trace, maxDepth=1) }}
            </div>
        {% endif %}
    </div>
{% endmacro %}
