{% 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">{{- name|capitalize }} 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>Answer</th>
                <th>{{ name|capitalize }}</th>
            </tr>
            </thead>
            <tbody>
            {% for class, answer in object.responses %}
                <tr>
                    <td class="font-normal text-small text-muted nowrap">{{ loop.index }}</td>
                    <td class="font-normal">
                        {% if answer is same as(true) %}
                            <span class="label status-success same-width">TRUE</span>
                        {% elseif answer 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 %}
        <div class="sf-toolbar-info-piece">
            <b>Resource Class</b>
            <span>{{ collector.resourceClass|default('Not an API Platform resource') }}</span>
        </div>
        {% if collector.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 %}
    {% 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.resourceClass ? '' : ' disabled' }}">
        <span class="icon">
            {{ include('@ApiPlatform/DataCollector/api-platform.svg') }}
        </span>
        <strong>API Platform</strong>
    </span>
{% endblock %}

{% block panel %}
    <div class="metrics">
        <div class="metric">
            <span class="value">{{ collector.resourceClass|default('Not an API Platform resource') }}</span>
            <span class="label">Resource class</span>
        </div>
    </div>

    {% if collector.resourceMetadata %}
        <div class="sf-tabs">
            <div class="tab">
                <h3 class="tab-title">Resource Metadata</h3>
                <div class="tab-content">
                    <h3>Short name: "{{ collector.resourceMetadata.shortName }}"</h3>
                    {{ apiPlatform.operationTable(collector.resourceMetadata.itemOperations, 'item', collector.requestAttributes.item_operation_name|default('')) }}
                    {{ apiPlatform.operationTable(collector.resourceMetadata.collectionOperations, 'collection', collector.requestAttributes.collection_operation_name|default('')) }}
                    <table>
                        <thead>
                            <tr>
                                <th scope="col" class="key">Filters</th>
                                <th scope="col"></th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for id, filter in collector.filters %}
                                {% 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>
                            {% else %}
                                <tr>
                                    <td class="text-muted">
                                        No available filter declared for this resource.
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>

                    <table>
                        <thead>
                            <tr>
                                <th scope="col" class="key">Attributes</th>
                                <th scope="col"></th>
                            </tr>
                        </thead>

                        <tbody>
                            {% for key, value in collector.resourceMetadata.attributes %}
                                {% if key != 'filters' %}
                                    <tr>
                                        <th scope="row">{{ key }}</th>
                                        <td>{{- profiler_dump(value, 2) -}}</td>
                                    </tr>
                                {% endif %}
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="tab">
                <h3 class="tab-title">Data Providers</h3>
                <div class="tab-content">
                    {{ apiPlatform.providerTable(collector.collectionDataProviders, 'collection data provider') }}
                    {{ apiPlatform.providerTable(collector.itemDataProviders, 'item data provider') }}
                    {{ apiPlatform.providerTable(collector.subresourceDataProviders, 'subresource data provider') }}
                </div>
            </div>
            <div class="tab">
                <h3 class="tab-title">Data Persisters</h3>
                <div class="tab-content">
                    {{ apiPlatform.providerTable(collector.dataPersisters, 'data persister') }}
                </div>
            </div>
        </div>
    {% endif %}
{% endblock %}
