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

{% macro operationLine(key, operation, actualOperationName) %}
    <tr>
        <th scope="row"{% if key == actualOperationName %} class="status-success"{% endif %}>{{ key }}</th>
        <td{% if key == actualOperationName %} class="status-success"{% endif %}>{{- profiler_dump(operation, 1) -}}</td>
    </tr>
{% endmacro %}

{% macro operationTable(object, name, actualOperationName) %}
    {% import _self as apiPlatform %}
    <table>
        <thead>
            <tr>
                <th scope="col" class="key">{% if name is defined %}{{ name|capitalize }}{% endif %}Operations</th>
                <th scope="col">Attributes</th>
            </tr>
        </thead>

        <tbody>
        {% for key, itemOperation in object %}
            {{ apiPlatform.operationLine(key, itemOperation, actualOperationName) }}
        {% else %}
            <tr>
                <td colspan="2" class="text-muted">
                    No available {{ name|lower }} operation for this resource.
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
{% endmacro %}

{% macro providerTable(object, name) %}
    {% if object.responses is empty %}
        <div class="empty">
            <p>No calls to {{ name }} have been recorded.</p>
        </div>
    {% else %}
        <table>
            <thead>
            <tr>
                <th>#</th>
                <th>Used</th>
                <th>{{ name|capitalize }}</th>
            </tr>
            </thead>
            <tbody>
            {% for class, used in object.responses %}
                <tr>
                    <td class="font-normal text-small text-muted nowrap">{{ loop.index }}</td>
                    <td class="font-normal">
                        {% if used is same as(true) %}
                            <span class="label status-success same-width">TRUE</span>
                        {% elseif used is same as(false) %}
                            <span class="label status-error same-width">FALSE</span>
                        {% else %}
                            <span class="label status-info same-width">NOT USED</span>
                        {% endif %}
                    </td>
                    <td class="font-normal">{{ class }}</td>
                </tr>
            {% endfor %}
            </tbody>
            {% if object.context is defined %}
                <tfoot>
                    <tr>
                        <td class="font-normal" colspan="2"></td>
                        <td class="font-normal">Context{{ profiler_dump(object.context, 2) }}</td>
                    </tr>
                </tfoot>
            {% endif %}
        </table>
    {% endif %}
{% endmacro %}

{% import _self as apiPlatform %}

{% block toolbar %}
    {% set icon %}
        {% set status_color = collector.counters.ignored_filters|default(false) ? 'yellow' : 'default' %}
        {{ include('@ApiPlatform/DataCollector/api-platform-icon.svg') }}
    {% endset %}

    {% set text %}
        {% if collector.version %}
            <div class="sf-toolbar-info-piece">
                <b>Version</b>
                <span>{{ collector.version }}</span>
            </div>
        {% endif %}
        {% if collector.resources|length == 0 %}
            <div class="sf-toolbar-info-piece">
                <b>Resource Class</b>
                <span>Not an API Platform resource</span>
            </div>
        {% endif %}
        {% for resource in collector.resources %}
            <div class="sf-toolbar-info-piece">
                <b>Resource Class</b>
                <span>{{ resource.resourceClass }}</span>
            </div>
            {% if resource.counters.ignored_filters|default(false) %}
                <div class="sf-toolbar-info-piece">
                    <b>Ignored Filters</b>
                    <span class="sf-toolbar-status sf-toolbar-status-yellow">{{ collector.counters.ignored_filters }}</span>
                </div>
            {% endif %}
        {% endfor %}
    {% endset %}

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

{% block menu %}
    {# This left-hand menu appears when using the full-screen profiler. #}
    <span class="label{{ collector.resources ? '' : ' disabled' }}">
        <span class="icon">
            {{ include('@ApiPlatform/DataCollector/api-platform.svg') }}
        </span>
        <strong>API Platform</strong>
    </span>
{% endblock %}

{% block panel %}
    {% if collector.resources|length == 0 %}
        <div class="metrics">
            <div class="metric">
                <span class="value">Not an API Platform resource</span>
                <span class="label">Resource Class</span>
            </div>
        </div>
    {% endif %}
    {% for dataCollected in collector.resources %}
        <div class="metrics">
            <div class="metric">
                <span class="value">{{ dataCollected.resourceClass|default('Not an API Platform resource') }}</span>
                <span class="label">Resource Class</span>
            </div>
        </div>
        {% if dataCollected.resourceMetadataCollection is not empty %}
            <h3>Resources</h3>
            <div class="tab-content metadata-tab-content">
                <div class="sf-tabs">
                    {% for resourceMetadata in dataCollected.resourceMetadataCollection %}
                        <div class="tab">
                            <h3 class="tab-title">
                                {{ resourceMetadata.resource.uriTemplate ?: resourceMetadata.resource.shortName }}
                            </h3>
                            <div class="tab-content">
                                <table>
                                    <thead>
                                        <tr>
                                            <th scope="col" class="key">Resource</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>{{- profiler_dump(resourceMetadata.resource, 1) -}}</td>
                                        </tr>
                                    </tbody>
                                </table>
                                {{ apiPlatform.operationTable(resourceMetadata.operations, '', dataCollected.requestAttributes.operation_name|default('')) }}
                                <table>
                                    <thead>
                                        <tr>
                                            <th scope="col" class="key">Filters</th>
                                            <th scope="col"></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% if dataCollected.filters and loop.index0 in dataCollected.filters|keys %}
                                            {% for id, filter in dataCollected.filters[loop.index0] %}
                                                {% set ignored_filter = filter is same as(null) %}
                                                <tr{% if ignored_filter %} class="status-warning"{% endif %}>
                                                    <td>
                                                        {{ id }}
                                                        {% if ignored_filter %}
                                                            <span class="newline text-muted">ignored filter</span>
                                                        {% else %}
                                                            {{ dump(filter) }}
                                                        {% endif %}
                                                    </td>
                                                </tr>
                                            {% endfor %}
                                        {% else %}
                                            <tr>
                                                <td class="text-muted" colspan="2">
                                                    No available filter declared for this resource.
                                                </td>
                                            </tr>
                                        {% endif %}
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    {% endfor %}
                </div>
            </div>
        {% endif %}
    {% endfor %}
{% endblock %}

