{% 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' : 'yellow' %}
            {{ 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 ? 'yellow' }}">{{ 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.countdeprecations or collector.countwarnings ? 'warning' }} {{ 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.logs is empty %}
        <div class="empty">
            <p>No log messages available.</p>
        </div>
    {% else %}
        {# sort collected logs in groups #}
        {% set deprecation_logs, debug_logs, info_and_error_logs, silenced_logs = [], [], [], [] %}
        {% set has_error_logs = false %}
        {% for log in collector.logs %}
            {% if log.scream is defined and not log.scream %}
                {% set deprecation_logs = deprecation_logs|merge([log]) %}
            {% elseif log.scream is defined and log.scream %}
                {% set silenced_logs = silenced_logs|merge([log]) %}
            {% elseif log.priorityName == 'DEBUG' %}
                {% set debug_logs = debug_logs|merge([log]) %}
            {% else %}
                {% set info_and_error_logs = info_and_error_logs|merge([log]) %}
                {% if log.priorityName != 'INFO' %}
                    {% set has_error_logs = true %}
                {% endif %}
            {% endif %}
        {% endfor %}

        <div class="sf-tabs">
            <div class="tab {{ has_error_logs ? 'active' }}">
                <h3 class="tab-title">Info. &amp; Errors <span class="badge status-{{ collector.counterrors ? 'error' : collector.countwarnings ? 'warning' }}">{{ collector.counterrors ?: info_and_error_logs|length }}</span></h3>
                <p class="text-muted">Informational and error log messages generated during the execution of the application.</p>

                <div class="tab-content">
                    {% if info_and_error_logs is empty %}
                        <div class="empty">
                            <p>There are no log messages of this level.</p>
                        </div>
                    {% else %}
                        {{ helper.render_table(info_and_error_logs, 'info', true) }}
                    {% endif %}
                </div>
            </div>

            <div class="tab {{ not has_error_logs and collector.countdeprecations > 0 ? 'active' }}">
                {# 'deprecation_logs|length' is not used because deprecations are
                now grouped and the group count doesn't match the message count #}
                <h3 class="tab-title">Deprecations <span class="badge status-{{ collector.countdeprecations ? 'warning' }}">{{ collector.countdeprecations|default(0) }}</span></h3>
                <p class="text-muted">Log messages generated by using features marked as deprecated.</p>

                <div class="tab-content">
                    {% if deprecation_logs is empty %}
                        <div class="empty">
                            <p>There are no log messages about deprecated features.</p>
                        </div>
                    {% else %}
                        {{ helper.render_table(deprecation_logs, 'deprecation', false, true) }}
                    {% endif %}
                </div>
            </div>

            <div class="tab">
                <h3 class="tab-title">Debug <span class="badge">{{ debug_logs|length }}</span></h3>
                <p class="text-muted">Unimportant log messages generated during the execution of the application.</p>

                <div class="tab-content">
                    {% if debug_logs is empty %}
                        <div class="empty">
                            <p>There are no log messages of this level.</p>
                        </div>
                    {% else %}
                        {{ helper.render_table(debug_logs, 'debug') }}
                    {% endif %}
                </div>
            </div>

            <div class="tab">
                <h3 class="tab-title">PHP Notices <span class="badge">{{ collector.countscreams|default(0) }}</span></h3>
                <p class="text-muted">Log messages generated by PHP notices silenced with the @ operator.</p>

                <div class="tab-content">
                    {% if silenced_logs is empty %}
                        <div class="empty">
                            <p>There are no log messages of this level.</p>
                        </div>
                    {% else %}
                        {{ helper.render_table(silenced_logs, 'silenced') }}
                    {% endif %}
                </div>
            </div>

            {% set compilerLogTotal = 0 %}
            {% for logs in collector.compilerLogs %}
                {% set compilerLogTotal = compilerLogTotal + logs|length %}
            {% endfor %}
            <div class="tab">
                <h3 class="tab-title">Container <span class="badge">{{ compilerLogTotal }}</span></h3>
                <p class="text-muted">Log messages generated during the compilation of the service container.</p>

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

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

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

                                             <div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
                                                <ul>
                                                {% for log in logs %}
                                                    <li>{{ profiler_dump_log(log.message) }}</li>
                                                {% endfor %}
                                                </ul>
                                            </div>
                                        </td>
                                        <td class="font-normal text-right">{{ logs|length }}</td>
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    {% endif %}
                </div>
            </div>

        </div>

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

{% macro render_table(logs, category = '', show_level = false, is_deprecation = false) %}
    {% import _self as helper %}
    {% set channel_is_defined = (logs|first).channel is defined %}
    {% set filter = show_level or channel_is_defined %}

    <table class="logs"{% if show_level %} data-filter-level="Emergency,Alert,Critical,Error,Warning,Notice,Info"{% endif %}{% if filter %} data-filters{% endif %}>
        <thead>
            <tr>
                {% if show_level %}<th data-filter="level">Level</th>{% else %}<th>Time</th>{% endif %}
                {% if channel_is_defined %}<th data-filter="channel">Channel</th>{% endif %}
                <th class="full-width">Message</th>
            </tr>
        </thead>

        <tbody>
            {% for log in logs %}
                {% set css_class = is_deprecation ? ''
                    : log.priorityName in ['CRITICAL', 'ERROR', 'ALERT', 'EMERGENCY'] ? 'status-error'
                    : log.priorityName == 'WARNING' ? 'status-warning'
                %}
                <tr class="{{ css_class }}"{% if show_level %} data-filter-level="{{ log.priorityName|lower }}"{% endif %}{% if channel_is_defined %} data-filter-channel="{{ log.channel is not null ? log.channel : '' }}"{% endif %}>
                    <td class="font-normal text-small" nowrap>
                        {% if show_level %}
                            <span class="colored text-bold">{{ log.priorityName }}</span>
                        {% endif %}
                        <time class="text-muted newline" title="{{ log.timestamp|date('r') }}" datetime="{{ log.timestamp|date('c') }}">{{ log.timestamp|date('H:i:s') }}</time>
                    </td>

                    {% if channel_is_defined %}
                        <td class="font-normal text-small text-bold" nowrap>
                            {% if log.channel is null %}<em>n/a</em>{% else %}{{ log.channel }}{% endif %}
                            {% if log.errorCount is defined and log.errorCount > 1 %}
                                <span class="text-muted">({{ log.errorCount }} times)</span>
                            {% endif %}
                        </td>

                    {% endif %}

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

{% 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) }}

        <div class="text-small font-normal">
            {% set context_id = 'context-' ~ category ~ '-' ~ log_index %}
            <a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide context">Show context</a>

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

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

        {% 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 %}
    {% endif %}
{% endmacro %}
